forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_flag.go
57 lines (44 loc) · 1.16 KB
/
pipeline_flag.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
package flaghelpers
import (
"os"
"strings"
"github.com/jessevdk/go-flags"
"github.com/concourse/fly/rc"
)
type flyCommand struct {
Target rc.TargetName `short:"t" long:"target" description:"Concourse target name"`
}
var fly flyCommand
type PipelineFlag string
func (flag *PipelineFlag) Complete(match string) []flags.Completion {
// Prevent go-flags from recursing
goFlagsCompletion, hasCompletion := os.LookupEnv("GO_FLAGS_COMPLETION")
os.Unsetenv("GO_FLAGS_COMPLETION")
defer func() {
if hasCompletion {
os.Setenv("GO_FLAGS_COMPLETION", goFlagsCompletion)
}
}()
parser := flags.NewParser(&fly, flags.HelpFlag|flags.PassDoubleDash)
parser.NamespaceDelimiter = "-"
parser.Parse()
target, err := rc.LoadTarget(fly.Target)
if err != nil {
return []flags.Completion{}
}
err = target.Validate()
if err != nil {
return []flags.Completion{}
}
pipelines, err := target.Team().ListPipelines()
if err != nil {
return []flags.Completion{}
}
comps := []flags.Completion{}
for _, pipeline := range pipelines {
if strings.HasPrefix(pipeline.Name, match) {
comps = append(comps, flags.Completion{Item: pipeline.Name})
}
}
return comps
}