-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_command.go
463 lines (383 loc) · 11.1 KB
/
exec_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package main
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"gopkg.in/ini.v1"
)
const (
refName = "refs/onedev/tod"
execSection = "exec"
projectKey = "project"
workdirKey = "workdir"
tokenKey = "token"
paramPrefix = "param."
)
type ExecCommand struct {
}
type ParamMap map[string][]string
func (p ParamMap) String() string {
str := "{"
for key, values := range p {
str += fmt.Sprintf("%s: %v, ", key, values)
}
str += "}"
return str
}
func (p ParamMap) Set(value string) error {
parts := strings.Split(value, "=")
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
valuesString := strings.TrimSpace(parts[1])
if len(valuesString) == 0 {
delete(p, key)
} else {
values := strings.Split(valuesString, ",")
for i := range values {
values[i] = strings.TrimSpace(values[i])
}
p[key] = values
}
return nil
} else {
return fmt.Errorf("invalid parameter format: %s", value)
}
}
type FilteredWriter struct {
}
func (fw *FilteredWriter) Write(p []byte) (n int, err error) {
scanner := bufio.NewScanner(strings.NewReader(string(p)))
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "To ") && !strings.Contains(line, "->") && !strings.Contains(line, "Everything up-to-date") {
fmt.Fprintln(os.Stderr, line)
}
}
if err := scanner.Err(); err != nil {
return 0, err
}
return len(p), nil
}
var buildFinished bool
var mutex sync.Mutex
func (execCommand ExecCommand) Execute(args []string) {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Fprintln(os.Stderr, "Error getting user home:", err)
os.Exit(1)
}
// Define path to the INI configuration file
configFilePath := filepath.Join(homeDir, ".tod/config")
project := ""
workdir := ""
token := ""
params := make(ParamMap)
if _, err := os.Stat(configFilePath); !os.IsNotExist(err) {
cfg, err := ini.Load(configFilePath)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading config file:", err)
os.Exit(1)
}
section := cfg.Section(execSection)
project = section.Key(projectKey).String()
workdir = section.Key(workdirKey).String()
token = section.Key(tokenKey).String()
for _, key := range section.Keys() {
if strings.HasPrefix(key.Name(), paramPrefix) {
paramName := strings.TrimPrefix(key.Name(), paramPrefix)
paramValues := strings.Split(key.Value(), ",")
params[paramName] = paramValues
}
}
}
fs := flag.NewFlagSet("exec", flag.ExitOnError)
fs.StringVar(&project, "project", project, "Specify project url, for instance: https://onedev.example.com/your/project")
fs.StringVar(&workdir, "workdir", workdir, "Specify working directory to run job against")
fs.StringVar(&token, "token", token, "Specify access token with permission to run specified job")
fs.Var(¶ms, "param", "Specify job parameters in form of key=value")
fs.Parse(args)
if project == "" {
fmt.Fprintln(os.Stderr, "Missing project url. Check https://code.onedev.io/onedev/tod for details")
os.Exit(1)
}
if token == "" {
fmt.Fprintln(os.Stderr, "Missing access token. Check https://code.onedev.io/onedev/tod for details")
os.Exit(1)
}
if fs.NArg() < 1 {
fmt.Fprintln(os.Stderr, "Missing job name. Check https://code.onedev.io/onedev/tod for details")
os.Exit(1)
}
jobName := fs.Arg(0)
if workdir == "" {
workdir = "."
}
absoluteWorkdir, err := filepath.Abs(workdir)
if err != nil {
fmt.Fprintln(os.Stderr, "Error getting absolute path:", err)
os.Exit(1)
}
gitDir := filepath.Join(absoluteWorkdir, ".git")
if _, err := os.Stat(gitDir); os.IsNotExist(err) {
fmt.Fprintln(os.Stderr, "Invalid workdir: not a git working directory:", absoluteWorkdir)
os.Exit(1)
}
buildSpecFile := filepath.Join(absoluteWorkdir, ".onedev-buildspec.yml")
if _, err := os.Stat(buildSpecFile); os.IsNotExist(err) {
fmt.Fprintln(os.Stderr, "Invalid workdir: OneDev build spec not found:", absoluteWorkdir)
os.Exit(1)
}
hostIndex := strings.Index(project, "://") + 3
if hostIndex == -1 {
fmt.Fprintln(os.Stderr, "Invalid project url:", project)
os.Exit(1)
}
pathIndex := strings.Index(project[hostIndex:], "/") + 1
if pathIndex == 0 {
fmt.Fprintln(os.Stderr, "Invalid project url:", project)
os.Exit(1)
}
serverUrl := project[:hostIndex+pathIndex-1]
projectPath := project[hostIndex+pathIndex:]
if projectPath == "" {
fmt.Fprintln(os.Stderr, "Invalid project url:", project)
os.Exit(1)
}
err = checkVersion(serverUrl, token)
if err != nil {
fmt.Fprintln(os.Stderr, "Error checking version:", err)
os.Exit(1)
}
client := http.Client{}
query := url.Values{}
query.Set("query", fmt.Sprintf("\"Path\" is \"%s\"", projectPath))
query.Set("offset", "0")
query.Set("count", "1")
targetUrl, err := url.Parse(serverUrl)
if err != nil {
fmt.Fprintln(os.Stderr, "Error parsing server url", err)
os.Exit(1)
}
targetUrl.Path = "~api/projects"
targetUrl.RawQuery = query.Encode()
req, err := http.NewRequest("GET", targetUrl.String(), nil)
if err != nil {
fmt.Fprintln(os.Stderr, "Error querying project", err)
os.Exit(1)
}
req.Header.Set("Authorization", "Bearer "+token)
resp, err := client.Do(req)
if err != nil {
fmt.Fprintln(os.Stderr, "Error querying project", err)
os.Exit(1)
}
defer resp.Body.Close()
err = checkResponse(resp)
if err != nil {
fmt.Fprintln(os.Stderr, "Error querying project:", err)
os.Exit(1)
}
var projects []map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&projects)
if err != nil {
fmt.Fprintln(os.Stderr, "Error decoding project query response:", err)
os.Exit(1)
}
if len(projects) == 0 {
fmt.Fprintln(os.Stderr, "Project not found on server:", projectPath)
os.Exit(1)
}
projectId := projects[0]["id"]
fmt.Println("Collecting local changes...")
cmd := exec.Command("git", "stash", "create")
cmd.Dir = absoluteWorkdir
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
fmt.Fprintln(os.Stderr, "Error executing command:", err)
os.Exit(1)
}
runCommit := strings.TrimSpace(string(out))
if runCommit == "" {
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = absoluteWorkdir
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
fmt.Fprintln(os.Stderr, "Error executing command:", err)
os.Exit(1)
}
runCommit = strings.TrimSpace(string(out))
}
fmt.Println("Sending local changes to server...")
cmd = exec.Command("git", "-c", "http.extraHeader=Authorization: Bearer "+token, "push", "-f", project, runCommit+":"+refName)
cmd.Dir = absoluteWorkdir
cmd.Stderr = &FilteredWriter{}
_, err = cmd.Output()
if err != nil {
fmt.Fprintln(os.Stderr, "Error executing command:", err)
os.Exit(1)
}
targetUrl.Path = "~api/job-runs"
targetUrl.RawQuery = ""
jobRunData := map[string]interface{}{
"@type": "JobRunOnCommit",
"projectId": projectId,
"commitHash": runCommit,
"refName": refName,
"jobName": jobName,
"params": params,
"reason": "Submitted via tod",
}
jsonData, _ := json.Marshal(jobRunData)
req, err = http.NewRequest("POST", targetUrl.String(), bytes.NewBuffer(jsonData))
if err != nil {
fmt.Fprintln(os.Stderr, "Error requesting build:", err)
os.Exit(1)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
resp, err = client.Do(req)
if err != nil {
fmt.Fprintln(os.Stderr, "Error requesting build:", err)
os.Exit(1)
}
defer resp.Body.Close()
err = checkResponse(resp)
if err != nil {
fmt.Fprintln(os.Stderr, "Error requesting build:", err)
os.Exit(1)
}
var buildId interface{}
err = json.NewDecoder(resp.Body).Decode(&buildId)
if err != nil {
fmt.Fprintln(os.Stderr, "Error decoding build request response:", err)
os.Exit(1)
}
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChannel
mutex.Lock()
defer mutex.Unlock()
if !buildFinished {
fmt.Println("Cancelling build...")
client := &http.Client{}
targetUrl := fmt.Sprintf("%s/~api/job-runs/%v", serverUrl, buildId)
req, err := http.NewRequest("DELETE", targetUrl, nil)
if err != nil {
fmt.Fprintln(os.Stderr, "Error cancelling build:", err)
return
}
req.Header.Set("Authorization", "Bearer "+token)
resp, err := client.Do(req)
if err != nil {
fmt.Fprintln(os.Stderr, "Error cancelling build:", err)
return
}
defer resp.Body.Close()
err = checkResponse(resp)
if err != nil {
fmt.Fprintln(os.Stderr, "Error cancelling build:", err)
os.Exit(1)
}
}
}()
targetUrl.Path = fmt.Sprintf("~api/streaming/build-logs/%v", buildId)
req, err = http.NewRequest("GET", targetUrl.String(), nil)
if err != nil {
fmt.Fprintln(os.Stderr, "Error streaming build log:", err)
os.Exit(1)
}
req.Header.Set("Authorization", "Bearer "+token)
resp, err = client.Do(req)
if err != nil {
fmt.Println("Error streaming build log:", err)
os.Exit(1)
}
defer resp.Body.Close()
err = checkResponse(resp)
if err != nil {
fmt.Fprintln(os.Stderr, "Error streaming build log:", err)
os.Exit(1)
}
for {
var len int32
err := binary.Read(resp.Body, binary.BigEndian, &len)
if err != nil {
fmt.Fprintln(os.Stderr, "Error streaming build log:", err)
os.Exit(1)
}
if len > 0 {
logEntryBytes := make([]byte, len)
_, err := io.ReadFull(resp.Body, logEntryBytes)
if err != nil {
fmt.Fprintln(os.Stderr, "Error streaming build log:", resp.Status)
os.Exit(1)
}
var logEntry map[string]interface{}
err = json.Unmarshal(logEntryBytes, &logEntry)
if err != nil {
fmt.Fprintln(os.Stderr, "Error decoding log entry json:", err)
os.Exit(1)
}
line := ""
messages := logEntry["messages"].([]interface{})
for _, messageNode := range messages {
message := messageNode.(map[string]interface{})["text"].(string)
styleNode := messageNode.(map[string]interface{})["style"].(map[string]interface{})
fgColor := styleNode["color"].(string)
if fgColor != "fg-default" {
message = wrapWithColor(message, fgColor)
}
bgColor := styleNode["backgroundColor"].(string)
if bgColor != "bg-default" {
message = wrapWithColor(message, bgColor)
}
bold := styleNode["bold"].(bool)
if bold {
message = wrapWithBold(message)
}
line += message
}
fmt.Println(line)
} else if len < 0 {
buildStatusBytes := make([]byte, -len)
_, err := io.ReadFull(resp.Body, buildStatusBytes)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading build status:", err)
os.Exit(1)
}
buildStatus := string(buildStatusBytes)
var message = "Build is " + strings.ToLower(buildStatus)
if buildStatus == "SUCCESSFUL" {
mutex.Lock()
buildFinished = true
mutex.Unlock()
fmt.Println(wrapWithBold(wrapWithGreen(message)))
break
} else if buildStatus == "FAILED" || buildStatus == "CANCELLED" || buildStatus == "TIMED_OUT" {
mutex.Lock()
buildFinished = true
mutex.Unlock()
fmt.Println(wrapWithBold(wrapWithRed(message)))
break
} else {
fmt.Println(wrapWithBold(message))
}
}
}
}