This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
builds.go
205 lines (175 loc) · 5.09 KB
/
builds.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
203
204
205
package commands
import (
"errors"
"fmt"
"os"
"strconv"
"time"
"github.com/concourse/atc"
"github.com/concourse/fly/commands/internal/displayhelpers"
"github.com/concourse/fly/commands/internal/flaghelpers"
"github.com/concourse/fly/rc"
"github.com/concourse/fly/ui"
"github.com/concourse/go-concourse/concourse"
"github.com/fatih/color"
)
const timeDateLayout = "2006-01-02@15:04:05-0700"
type BuildsCommand struct {
Count int `short:"c" long:"count" default:"50" description:"Number of builds you want to limit the return to"`
Job flaghelpers.JobFlag `short:"j" long:"job" value-name:"PIPELINE/JOB" description:"Name of a job to get builds for"`
Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" description:"Name of a pipeline to get builds for"`
Team bool `short:"t" long:"team" description:"Only show builds for the currently targeted team"`
Json bool `long:"json" description:"Print command result as JSON"`
}
func (command *BuildsCommand) Execute([]string) error {
target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
if err != nil {
return err
}
err = target.Validate()
if err != nil {
return err
}
page := concourse.Page{Limit: command.Count}
team := target.Team()
client := target.Client()
var builds []atc.Build
if command.pipelineFlag() && command.jobFlag() {
return errors.New("Cannot specify both --pipeline and --job")
} else if command.pipelineFlag() {
err = command.Pipeline.Validate()
if err != nil {
return err
}
var found bool
builds, _, found, err = team.PipelineBuilds(
string(command.Pipeline),
page,
)
if err != nil {
return err
}
if !found {
displayhelpers.Failf("pipeline not found")
}
} else if command.jobFlag() {
var found bool
builds, _, found, err = team.JobBuilds(
command.Job.PipelineName,
command.Job.JobName,
page,
)
if err != nil {
return err
}
if !found {
displayhelpers.Failf("pipeline/job not found")
}
} else if command.Team {
builds, _, err = team.Builds(page)
if err != nil {
return err
}
} else {
builds, _, err = client.Builds(page)
if err != nil {
return err
}
}
if command.Json {
err = displayhelpers.JsonPrint(builds)
if err != nil {
return err
}
return nil
}
table := ui.Table{
Headers: ui.TableRow{
{Contents: "id", Color: color.New(color.Bold)},
{Contents: "pipeline/job", Color: color.New(color.Bold)},
{Contents: "build", Color: color.New(color.Bold)},
{Contents: "status", Color: color.New(color.Bold)},
{Contents: "start", Color: color.New(color.Bold)},
{Contents: "end", Color: color.New(color.Bold)},
{Contents: "duration", Color: color.New(color.Bold)},
},
}
var rangeUntil int
if command.Count < len(builds) {
rangeUntil = command.Count
} else {
rangeUntil = len(builds)
}
for _, b := range builds[:rangeUntil] {
startTimeCell, endTimeCell, durationCell := populateTimeCells(time.Unix(b.StartTime, 0), time.Unix(b.EndTime, 0))
var pipelineJobCell, buildCell ui.TableCell
if b.PipelineName == "" {
pipelineJobCell.Contents = "one-off"
buildCell.Contents = "n/a"
} else {
pipelineJobCell.Contents = fmt.Sprintf("%s/%s", b.PipelineName, b.JobName)
buildCell.Contents = b.Name
}
var statusCell ui.TableCell
statusCell.Contents = b.Status
switch b.Status {
case "pending":
statusCell.Color = ui.PendingColor
case "started":
statusCell.Color = ui.StartedColor
case "succeeded":
statusCell.Color = ui.SucceededColor
case "failed":
statusCell.Color = ui.FailedColor
case "errored":
statusCell.Color = ui.ErroredColor
case "aborted":
statusCell.Color = ui.AbortedColor
case "paused":
statusCell.Color = ui.PausedColor
}
table.Data = append(table.Data, []ui.TableCell{
{Contents: strconv.Itoa(b.ID)},
pipelineJobCell,
buildCell,
statusCell,
startTimeCell,
endTimeCell,
durationCell,
})
}
return table.Render(os.Stdout, Fly.PrintTableHeaders)
}
func populateTimeCells(startTime time.Time, endTime time.Time) (ui.TableCell, ui.TableCell, ui.TableCell) {
var startTimeCell ui.TableCell
var endTimeCell ui.TableCell
var durationCell ui.TableCell
startTime = startTime.Local()
endTime = endTime.Local()
zeroTime := time.Unix(0, 0)
if startTime == zeroTime {
startTimeCell.Contents = "n/a"
} else {
startTimeCell.Contents = startTime.Format(timeDateLayout)
}
if endTime == zeroTime {
endTimeCell.Contents = "n/a"
durationCell.Contents = fmt.Sprintf("%v+", roundSecondsOffDuration(time.Since(startTime)))
} else {
endTimeCell.Contents = endTime.Format(timeDateLayout)
durationCell.Contents = endTime.Sub(startTime).String()
}
if startTime == zeroTime && endTime == zeroTime {
durationCell.Contents = "n/a"
}
return startTimeCell, endTimeCell, durationCell
}
func roundSecondsOffDuration(d time.Duration) time.Duration {
return d - (d % time.Second)
}
func (command *BuildsCommand) jobFlag() bool {
return command.Job.PipelineName != "" && command.Job.JobName != ""
}
func (command *BuildsCommand) pipelineFlag() bool {
return command.Pipeline != ""
}