forked from buildkite/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_local_run.go
110 lines (93 loc) · 2.1 KB
/
cmd_local_run.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
package cli
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strings"
"github.com/buildkite/cli/v2/local"
)
type LocalRunCommandContext struct {
TerminalContext
ConfigContext
Debug bool
DebugHTTP bool
File *os.File
Env []string
Metadata map[string]string
Command string
StepFilterRegex *regexp.Regexp
Prompt bool
DryRun bool
ListenPort int
}
func LocalRunCommand(ctx LocalRunCommandContext) error {
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
if ctx.Debug {
local.Debug = true
}
cancelCtx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
<-quit
fmt.Printf("\n>>> Gracefully shutting down...\n")
cancel()
}()
wd, err := os.Getwd()
if err != nil {
return NewExitError(err, 1)
}
commit, err := gitCommit()
if err != nil {
log.Printf("Error getting git commit: %v", err)
commit = "no_commit_found"
}
branch, err := gitBranch()
if err != nil {
log.Printf("Error getting git branch: %v", err)
branch = "master"
}
command := ctx.Command
if ctx.File != nil {
command = fmt.Sprintf("buildkite-agent pipeline upload %q", ctx.File.Name())
}
if err := local.Run(cancelCtx, local.RunParams{
Env: ctx.Env,
Metadata: ctx.Metadata,
DryRun: ctx.DryRun,
Command: command,
Dir: wd,
Prompt: ctx.Prompt,
StepFilter: ctx.StepFilterRegex,
ListenPort: ctx.ListenPort,
JobTemplate: local.Job{
Commit: commit,
Branch: branch,
Repository: wd,
OrganizationSlug: "local",
PipelineSlug: filepath.Base(wd),
},
}); err != nil {
return NewExitError(err, 1)
}
return nil
}
func gitBranch() (string, error) {
out, err := exec.Command(`git`, `rev-parse`, `--abbrev-ref`, `HEAD`).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
func gitCommit() (string, error) {
out, err := exec.Command(`git`, `rev-parse`, `HEAD`).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}