-
Notifications
You must be signed in to change notification settings - Fork 786
/
get_workflow.go
133 lines (116 loc) · 2.94 KB
/
get_workflow.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package cmd
import (
"strings"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/workflow"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetWorkflowOptions containers the CLI options
type GetWorkflowOptions struct {
GetOptions
Name string
}
var (
getWorkflowLong = templates.LongDesc(`
Display either all the workflows or a specific workflow
`)
getWorkflowExample = templates.Examples(`
# List all the available workflows
jx get workflow
# Display a specific workflow
jx get workflow -n default
`)
)
// NewCmdGetWorkflow creates the new command for: jx get env
func NewCmdGetWorkflow(commonOpts *CommonOptions) *cobra.Command {
options := &GetWorkflowOptions{
GetOptions: GetOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "workflows",
Short: "Display either all the available workflows or a specific workflow",
Aliases: []string{"workflow"},
Long: getWorkflowLong,
Example: getWorkflowExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Name, "name", "n", "", "The name of the workflow to display")
options.addGetFlags(cmd)
return cmd
}
// Run implements this command
func (o *GetWorkflowOptions) Run() error {
jxClient, ns, err := o.JXClientAndAdminNamespace()
if err != nil {
return err
}
name := o.Name
if name == "" {
if len(o.Args) > 0 {
name = o.Args[0]
}
}
if name != "" {
return o.getWorkflow(name, jxClient, ns)
}
workflows, err := jxClient.JenkinsV1().Workflows(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
table := o.createTable()
table.AddRow("WORKFLOW")
for _, workflow := range workflows.Items {
table.AddRow(workflow.Name)
}
table.Render()
return nil
}
func (o *GetWorkflowOptions) getWorkflow(name string, jxClient versioned.Interface, ns string) error {
workflow, err := workflow.GetWorkflow(name, jxClient, ns)
if err != nil {
return err
}
log.Infof("Workflow: %s\n", workflow.Name)
lines := []*StepSummary{}
var lastSummary *StepSummary
for _, step := range workflow.Spec.Steps {
promote := step.Promote
if promote != nil {
if len(step.Preconditions.Environments) > 0 {
lastSummary = nil
}
if lastSummary == nil {
lastSummary = &StepSummary{
Action: "promote",
// Resources: []string{},
}
lines = append(lines, lastSummary)
}
lastSummary.Resources = append(lastSummary.Resources, promote.Environment)
if len(step.Preconditions.Environments) > 0 {
lastSummary = nil
}
}
}
for i, summary := range lines {
if i > 0 {
log.Info(" |\n")
}
log.Infof("%s to %s\n", summary.Action, strings.Join(summary.Resources, " + "))
}
return nil
}
type StepSummary struct {
Action string
Resources []string
}