forked from buildkite/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_browse.go
74 lines (58 loc) · 1.21 KB
/
cmd_browse.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
package cli
import (
"path/filepath"
"github.com/buildkite/cli/git"
"github.com/skratchdot/open-golang/open"
)
type BrowseCommandContext struct {
TerminalContext
KeyringContext
Debug bool
DebugHTTP bool
Dir string
Branch string
}
func BrowseCommand(ctx BrowseCommandContext) error {
dir, err := filepath.Abs(ctx.Dir)
if err != nil {
return NewExitError(err, 1)
}
branch := ctx.Branch
if branch == "" {
var gitErr error
branch, gitErr = git.Branch(dir)
if gitErr != nil {
return NewExitError(err, 1)
}
}
gitRemote, err := git.Remote(dir)
if err != nil {
return NewExitError(err, 1)
}
bk, err := ctx.BuildkiteGraphQLClient()
if err != nil {
return NewExitError(err, 1)
}
allPipelines, err := listPipelines(bk)
if err != nil {
return NewExitError(err, 1)
}
ps := pipelineSelect{
Pipelines: allPipelines,
Filter: func(p pipeline) bool {
return git.MatchRemotes(p.RepositoryURL, gitRemote)
},
}
pipeline, err := ps.Run()
if err != nil {
return NewExitError(err, 1)
}
pipelineURL := pipeline.URL
if pipelineURL != "" {
pipelineURL += "/builds?branch=" + branch
}
if err := open.Run(pipelineURL); err != nil {
return NewExitError(err, 1)
}
return nil
}