forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
containers.go
97 lines (80 loc) · 2.21 KB
/
containers.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
package commands
import (
"os"
"sort"
"strconv"
"github.com/concourse/fly/rc"
"github.com/concourse/fly/ui"
"github.com/fatih/color"
)
type ContainersCommand struct{}
func (command *ContainersCommand) Execute([]string) error {
target, err := rc.LoadTarget(Fly.Target)
if err != nil {
return err
}
err = target.Validate()
if err != nil {
return err
}
containers, err := target.Client().ListContainers(map[string]string{})
if err != nil {
return err
}
table := ui.Table{
Headers: ui.TableRow{
{Contents: "handle", Color: color.New(color.Bold)},
{Contents: "ttl", Color: color.New(color.Bold)},
{Contents: "validity", Color: color.New(color.Bold)},
{Contents: "worker", Color: color.New(color.Bold)},
{Contents: "pipeline", Color: color.New(color.Bold)},
{Contents: "job", Color: color.New(color.Bold)},
{Contents: "build #", Color: color.New(color.Bold)},
{Contents: "build id", Color: color.New(color.Bold)},
{Contents: "type", Color: color.New(color.Bold)},
{Contents: "name", Color: color.New(color.Bold)},
{Contents: "attempt", Color: color.New(color.Bold)},
},
}
for _, c := range containers {
row := ui.TableRow{
{Contents: c.ID},
{Contents: formatTTL(c.TTLInSeconds)},
{Contents: formatTTL(c.ValidityInSeconds)},
{Contents: c.WorkerName},
stringOrDefault(c.PipelineName),
stringOrDefault(c.JobName),
stringOrDefault(c.BuildName),
buildIDOrNone(c.BuildID),
stringOrDefault(c.StepType, "check"),
{Contents: (c.StepName + c.ResourceName)},
stringOrDefault(SliceItoa(c.Attempts), "n/a"),
}
table.Data = append(table.Data, row)
}
sort.Sort(table.Data)
return table.Render(os.Stdout)
}
func buildIDOrNone(id int) ui.TableCell {
var column ui.TableCell
if id == 0 {
column.Contents = "none"
column.Color = color.New(color.Faint)
} else {
column.Contents = strconv.Itoa(id)
}
return column
}
func stringOrDefault(containerType string, def ...string) ui.TableCell {
var column ui.TableCell
column.Contents = containerType
if column.Contents == "" || column.Contents == "[]" {
if len(def) == 0 {
column.Contents = "none"
column.Color = color.New(color.Faint)
} else {
column.Contents = def[0]
}
}
return column
}