forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builds.go
160 lines (135 loc) · 3.94 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
package commands
import (
"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"`
}
func (command *BuildsCommand) Execute([]string) error {
target, err := rc.LoadTarget(Fly.Target)
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.Job.PipelineName != "" && command.Job.JobName != "" {
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 {
builds, _, err = client.Builds(page)
if err != nil {
return err
}
}
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)
}
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)
}