forked from buildkite/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_pipeline_list.go
88 lines (71 loc) · 1.48 KB
/
cmd_pipeline_list.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
package cli
import (
"fmt"
"github.com/sahilm/fuzzy"
)
type PipelineListCommandContext struct {
TerminalContext
ConfigContext
Debug bool
DebugHTTP bool
Fuzzy string
Limit int
ShowURL bool
}
func PipelineListCommand(ctx PipelineListCommandContext) error {
bk, err := ctx.BuildkiteGraphQLClient()
if err != nil {
return NewExitError(err, 1)
}
pipelines, err := listPipelines(bk)
if err != nil {
return NewExitError(err, 1)
}
var counter int
var pipelineStrings []string
for _, pipeline := range pipelines {
pipelineStrings = append(pipelineStrings,
fmt.Sprintf("%s/%s", pipeline.Org, pipeline.Slug))
}
if ctx.Fuzzy != "" {
const bold = "\033[1m%s\033[0m"
matches := fuzzy.Find(ctx.Fuzzy, pipelineStrings)
for _, match := range matches {
counter++
if ctx.Limit > 0 && counter > ctx.Limit {
break
}
if ctx.ShowURL {
ctx.Printf(`https://buildkite.com/`)
}
for i := 0; i < len(match.Str); i++ {
if contains(i, match.MatchedIndexes) {
fmt.Print(fmt.Sprintf(bold, string(match.Str[i])))
} else {
fmt.Print(string(match.Str[i]))
}
}
fmt.Println()
}
return nil
}
for _, p := range pipelineStrings {
counter++
if ctx.Limit > 0 && counter > ctx.Limit {
break
}
if ctx.ShowURL {
ctx.Printf(`https://buildkite.com/`)
}
ctx.Println(p)
}
return nil
}
func contains(needle int, haystack []int) bool {
for _, i := range haystack {
if needle == i {
return true
}
}
return false
}