forked from buildkite/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
51 lines (41 loc) · 968 Bytes
/
ui.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
package cli
import (
"github.com/manifoldco/promptui"
)
type pipelineSelect struct {
Pipelines []pipeline
Filter func(pipeline) bool
}
func (ps *pipelineSelect) Run() (pipeline, error) {
var filtered []pipeline
if ps.Filter != nil {
for _, p := range ps.Pipelines {
if ps.Filter(p) {
// debugf("Filter matched %#v", p)
filtered = append(filtered, p)
}
}
} else {
filtered = ps.Pipelines
}
// debugf("Filtered %d pipelines down to %d", len(ps.Pipelines), len(filtered))
if len(filtered) == 1 {
return filtered[0], nil
} else if len(filtered) == 0 {
return pipeline{}, errPipelineDoesntExist
}
var labels []string
for _, p := range filtered {
labels = append(labels, p.URL)
}
prompt := promptui.Select{
Label: "Select pipeline",
Items: labels,
}
offset, _, err := prompt.Run()
if err != nil {
return pipeline{}, err
}
// debugf("Selected %d %#v", offset, filtered[offset])
return filtered[offset], nil
}