forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_pipeline.go
122 lines (105 loc) · 2.65 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
package cmd
import (
"io"
"github.com/spf13/cobra"
"github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx/pkg/jx/cmd/table"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"time"
)
// 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
}
var (
get_pipeline_long = templates.LongDesc(`
Display one or many pipelines.
`)
get_pipeline_example = templates.Examples(`
# List all pipelines
jx get pipeline
`)
)
// NewCmdGetPipeline creates the command
func NewCmdGetPipeline(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &GetPipelineOptions{
GetOptions: GetOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "pipelines [flags]",
Short: "Display one or many 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()
cmdutil.CheckErr(err)
},
}
options.addGetFlags(cmd)
return cmd
}
// Run implements this command
func (o *GetPipelineOptions) Run() error {
f := o.Factory
jenkins, err := f.GetJenkinsClient()
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 := o.CreateTable()
table.AddRow("Name", "URL", "lastBuild", "status", "duration")
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 (o *GetPipelineOptions) dump(jenkins *gojenkins.Jenkins, 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) {
table.AddRow(job.FullName, job.Url, "", "Never Built", "")
}
return nil
}
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
}