forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checklist.go
91 lines (72 loc) · 2.05 KB
/
checklist.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
89
90
91
package commands
import (
"fmt"
"sort"
"github.com/concourse/atc"
"github.com/concourse/fly/commands/internal/flaghelpers"
"github.com/concourse/fly/rc"
)
type ChecklistCommand struct {
Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" required:"true" description:"The pipeline from which to generate the Checkfile"`
}
func (command *ChecklistCommand) Validate() error {
return command.Pipeline.Validate()
}
func (command *ChecklistCommand) Execute([]string) error {
err := command.Validate()
if err != nil {
return err
}
target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
if err != nil {
return err
}
err = target.Validate()
if err != nil {
return err
}
pipelineName := string(command.Pipeline)
config, _, _, _, err := target.Team().PipelineConfig(pipelineName)
if err != nil {
return err
}
printCheckfile(target.Team().Name(), pipelineName, config, target.Client().URL())
return nil
}
func printCheckfile(teamName, pipelineName string, config atc.Config, url string) {
orphanHeaderName := "misc"
if len(config.Groups) == 0 {
orphanHeaderName = pipelineName
}
for _, group := range config.Groups {
printGroup(teamName, pipelineName, group, url)
}
miscJobs := orphanedJobs(config)
if len(miscJobs) > 0 {
printGroup(teamName, pipelineName, atc.GroupConfig{Name: orphanHeaderName, Jobs: miscJobs}, url)
}
}
func printGroup(teamName, pipelineName string, group atc.GroupConfig, url string) {
fmt.Printf("#- %s\n", group.Name)
for _, job := range group.Jobs {
fmt.Printf("%s: concourse.check %s %s %s %s\n", job, url, teamName, pipelineName, job)
}
fmt.Println("")
}
func orphanedJobs(config atc.Config) []string {
allJobNames := map[string]struct{}{}
for _, jobConfig := range config.Jobs {
allJobNames[jobConfig.Name] = struct{}{}
}
for _, group := range config.Groups {
for _, job := range group.Jobs {
delete(allJobNames, job)
}
}
result := make([]string, 0, len(config.Jobs))
for job := range allJobNames {
result = append(result, job)
}
sort.Sort(sort.StringSlice(result))
return result
}