-
Notifications
You must be signed in to change notification settings - Fork 786
/
get_pipeline.go
202 lines (172 loc) · 3.94 KB
/
get_pipeline.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package cmd
import (
"errors"
"io"
"sort"
"github.com/jenkins-x/jx/pkg/prow"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"strings"
"time"
"github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/table"
)
// GetPipelineOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type GetPipelineOptions struct {
GetOptions
ProwOptions prow.Options
}
var (
get_pipeline_long = templates.LongDesc(`
Display one or more pipelines.
`)
get_pipeline_example = templates.Examples(`
# List all pipelines
jx get pipeline
`)
)
// NewCmdGetPipeline creates the command
func NewCmdGetPipeline(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &GetPipelineOptions{
GetOptions: GetOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "pipelines [flags]",
Short: "Display one or more Pipelines",
Long: get_pipeline_long,
Example: get_pipeline_example,
Aliases: []string{"pipe", "pipes", "pipeline"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addGetFlags(cmd)
return cmd
}
// Run implements this command
func (o *GetPipelineOptions) Run() error {
_, _, err := o.JXClient()
if err != nil {
return err
}
client, err := o.KubeClient()
if err != nil {
return err
}
isProw, err := o.isProw()
if err != nil {
return err
}
if isProw {
o.ProwOptions = prow.Options{
KubeClient: client,
NS: o.currentNamespace,
}
names, err := o.ProwOptions.GetReleaseJobs()
if err != nil {
return err
}
if len(names) == 0 {
return errors.New("no pipelines found")
}
sort.Strings(names)
if len(names) == 0 {
return outputEmptyListWarning(o.Out)
}
if o.Output != "" {
return o.renderResult(names, o.Output)
}
table := createTable(o)
for _, j := range names {
if err != nil {
return err
}
table.AddRow(j, "N/A", "N/A", "N/A", "N/A")
}
table.Render()
} else {
jenkins, err := o.JenkinsClient()
if err != nil {
return err
}
jobs, err := jenkins.GetJobs()
if err != nil {
return err
}
if len(jobs) == 0 {
return outputEmptyListWarning(o.Out)
}
if o.Output != "" {
return o.renderResult(jobs, o.Output)
}
table := createTable(o)
for _, j := range jobs {
job, err := jenkins.GetJob(j.Name)
if err != nil {
return err
}
o.dump(jenkins, job.Name, &table)
}
table.Render()
}
return nil
}
func createTable(o *GetPipelineOptions) table.Table {
table := o.createTable()
table.AddRow("Name", "URL", "LAST_BUILD", "STATUS", "DURATION")
return table
}
func (o *GetPipelineOptions) dump(jenkins gojenkins.JenkinsClient, name string, table *table.Table) error {
job, err := jenkins.GetJob(name)
if err != nil {
return err
}
if job.Jobs != nil {
for _, child := range job.Jobs {
o.dump(jenkins, job.FullName+"/"+child.Name, table)
}
} else {
last, err := jenkins.GetLastBuild(job)
if err != nil {
if jenkins.IsErrNotFound(err) {
if o.matchesFilter(&job) {
table.AddRow(job.FullName, job.Url, "", "Never Built", "")
}
}
return nil
}
if o.matchesFilter(&job) {
if last.Building {
table.AddRow(job.FullName, job.Url, "#"+last.Id, "Building", time.Duration(last.EstimatedDuration).String()+"(est.)")
} else {
table.AddRow(job.FullName, job.Url, "#"+last.Id, last.Result, time.Duration(last.Duration).String())
}
}
}
return nil
}
func (o *GetPipelineOptions) matchesFilter(job *gojenkins.Job) bool {
args := o.Args
if len(args) == 0 {
return true
}
name := job.FullName
for _, arg := range args {
if strings.Contains(name, arg) {
return true
}
}
return false
}