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
/
volumes.go
147 lines (121 loc) · 3.51 KB
/
volumes.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
package commands
import (
"fmt"
"os"
"sort"
"strings"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/concourse/atc"
"github.com/concourse/fly/commands/internal/displayhelpers"
"github.com/concourse/fly/rc"
"github.com/concourse/fly/ui"
"github.com/fatih/color"
)
type VolumesCommand struct {
Details bool `short:"d" long:"details" description:"Print additional information for each volume"`
Json bool `long:"json" description:"Print command result as JSON"`
}
func (command *VolumesCommand) Execute([]string) error {
target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
if err != nil {
return err
}
err = target.Validate()
if err != nil {
return err
}
volumes, err := target.Team().ListVolumes()
if err != nil {
return err
}
if command.Json {
err = displayhelpers.JsonPrint(volumes)
if err != nil {
return err
}
return nil
}
table := ui.Table{
Headers: ui.TableRow{
{Contents: "handle", Color: color.New(color.Bold)},
{Contents: "worker", Color: color.New(color.Bold)},
{Contents: "type", Color: color.New(color.Bold)},
{Contents: "identifier", Color: color.New(color.Bold)},
},
}
sort.Sort(volumesByWorkerAndHandle(volumes))
for _, c := range volumes {
row := ui.TableRow{
{Contents: c.ID},
{Contents: c.WorkerName},
{Contents: c.Type},
{Contents: command.volumeIdentifier(c)},
}
table.Data = append(table.Data, row)
}
return table.Render(os.Stdout, Fly.PrintTableHeaders)
}
func (command *VolumesCommand) volumeIdentifier(volume atc.Volume) string {
switch volume.Type {
case "container":
if command.Details {
identifier := fmt.Sprintf("container:%s,path:%s", volume.ContainerHandle, volume.Path)
if volume.ParentHandle != "" {
identifier = fmt.Sprintf("%s,parent:%s", identifier, volume.ParentHandle)
}
return identifier
}
return volume.ContainerHandle
case "task-cache":
return fmt.Sprintf("%s/%s/%s", volume.PipelineName, volume.JobName, volume.StepName)
case "resource":
if command.Details {
return presentResourceType(volume.ResourceType)
}
return presentMap(volume.ResourceType.Version)
case "resource-type":
if command.Details {
return presentMap(volume.BaseResourceType)
}
return volume.BaseResourceType.Name
}
return "n/a"
}
func presentMap(version interface{}) string {
marshalled, _ := yaml.Marshal(version)
lines := strings.Split(strings.TrimSpace(string(marshalled)), "\n")
return strings.Replace(strings.Join(lines, ","), " ", "", -1)
}
func presentResourceType(resourceType *atc.VolumeResourceType) string {
if resourceType.BaseResourceType != nil {
return presentMap(resourceType.BaseResourceType)
}
if resourceType.ResourceType != nil {
innerResourceType := presentResourceType(resourceType.ResourceType)
version := presentMap(resourceType.Version)
return fmt.Sprintf("type:resource(%s),version:%s", innerResourceType, version)
}
return ""
}
type volumesByWorkerAndHandle []atc.Volume
func (cs volumesByWorkerAndHandle) Len() int { return len(cs) }
func (cs volumesByWorkerAndHandle) Swap(i int, j int) { cs[i], cs[j] = cs[j], cs[i] }
func (cs volumesByWorkerAndHandle) Less(i int, j int) bool {
if cs[i].WorkerName == cs[j].WorkerName {
return cs[i].ID < cs[j].ID
}
return cs[i].WorkerName < cs[j].WorkerName
}
func formatTTL(ttlInSeconds int64) string {
if ttlInSeconds == 0 {
return "indefinite"
}
duration := time.Duration(ttlInSeconds) * time.Second
return fmt.Sprintf(
"%0.2d:%0.2d:%0.2d",
int64(duration.Hours()),
int64(duration.Minutes())%60,
ttlInSeconds%60,
)
}