forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_flag.go
82 lines (64 loc) · 1.49 KB
/
job_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
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
package flaghelpers
import (
"errors"
"fmt"
"strings"
"github.com/concourse/go-concourse/concourse"
"github.com/jessevdk/go-flags"
"github.com/concourse/fly/rc"
)
type JobFlag struct {
PipelineName string
JobName string
}
func (job *JobFlag) UnmarshalFlag(value string) error {
vs := strings.SplitN(value, "/", 2)
if len(vs) != 2 {
return errors.New("argument format should be <pipeline>/<job>")
}
if vs[0] == "" {
return concourse.NameRequiredError("pipeline")
}
if vs[1] == "" {
return concourse.NameRequiredError("job")
}
job.PipelineName = vs[0]
job.JobName = vs[1]
return nil
}
func (flag *JobFlag) Complete(match string) []flags.Completion {
fly := parseFlags()
target, err := rc.LoadTarget(fly.Target)
if err != nil {
return []flags.Completion{}
}
err = target.Validate()
if err != nil {
return []flags.Completion{}
}
team := target.Team()
comps := []flags.Completion{}
vs := strings.SplitN(match, "/", 2)
if len(vs) == 1 {
pipelines, err := team.ListPipelines()
if err != nil {
return comps
}
for _, pipeline := range pipelines {
if strings.HasPrefix(pipeline.Name, vs[0]) {
comps = append(comps, flags.Completion{Item: pipeline.Name + "/"})
}
}
} else if len(vs) == 2 {
jobs, err := team.ListJobs(vs[0])
if err != nil {
return comps
}
for _, job := range jobs {
if strings.HasPrefix(job.Name, vs[1]) {
comps = append(comps, flags.Completion{Item: fmt.Sprintf("%s/%s", vs[0], job.Name)})
}
}
}
return comps
}