-
Notifications
You must be signed in to change notification settings - Fork 6
/
list.go
60 lines (51 loc) · 1.35 KB
/
list.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
package zone
import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
)
// ListCommand creates the "zone list" command
func ListCommand() commands.Command {
return &listCommand{
BaseCommand: commands.New("list", "List available zones", "upctl zone list"),
}
}
type listCommand struct {
*commands.BaseCommand
}
// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
svc := exec.All()
zones, err := svc.GetZones(exec.Context())
if err != nil {
return nil, err
}
hasParentZones := false
rows := []output.TableRow{}
for _, z := range zones.Zones {
if len(z.ParentZone) > 0 {
hasParentZones = true
}
rows = append(rows, output.TableRow{
z.ID,
z.Description,
z.Public,
z.ParentZone,
})
}
columns := []output.TableColumn{
{Key: "id", Header: "ID"},
{Key: "description", Header: "Description"},
{Key: "public", Header: "Public", Format: format.Boolean},
}
if hasParentZones {
columns = append(columns, output.TableColumn{Key: "parent_zone", Header: "Parent zone"})
}
return output.MarshaledWithHumanOutput{
Value: zones,
Output: output.Table{
Columns: columns,
Rows: rows,
},
}, nil
}