-
Notifications
You must be signed in to change notification settings - Fork 7
/
list.go
82 lines (75 loc) · 2.36 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package router
import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/spf13/pflag"
)
// ListCommand creates the "router list" command
func ListCommand() commands.Command {
return &listCommand{
BaseCommand: commands.New(
"list",
"List routers",
"upctl router list",
"upctl router list --all",
),
}
}
type listCommand struct {
*commands.BaseCommand
allRouters config.OptionalBoolean
normalRouters config.OptionalBoolean
serviceRouters config.OptionalBoolean
}
// InitCommand implements Command.InitCommand
func (s *listCommand) InitCommand() {
// s.header = table.Row{"UUID", "Name", "Type"}
flags := &pflag.FlagSet{}
config.AddToggleFlag(flags, &s.allRouters, "all", false, "Show all routers.")
config.AddToggleFlag(flags, &s.normalRouters, "normal", true, "Show normal routers.")
config.AddToggleFlag(flags, &s.serviceRouters, "service", false, "Show service routers.")
// TODO: reimplement
// s.AddVisibleColumnsFlag(flags, &s.visibleColumns, s.columnKeys, s.visibleColumns)
s.AddFlags(flags)
}
// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
routers, err := exec.Network().GetRouters(exec.Context())
if err != nil {
return nil, err
}
if s.serviceRouters.Value() {
s.normalRouters = config.False
}
filtered := make([]upcloud.Router, 0)
if s.allRouters.Value() {
filtered = routers.Routers
} else {
for _, r := range routers.Routers {
if s.normalRouters.Value() && r.Type == "normal" {
filtered = append(filtered, r)
}
if s.serviceRouters.Value() && r.Type == "service" {
filtered = append(filtered, r)
}
}
}
rows := make([]output.TableRow, len(filtered))
for i, router := range filtered {
rows[i] = output.TableRow{router.UUID, router.Name, router.Type}
}
return output.MarshaledWithHumanOutput{
Value: upcloud.Routers{Routers: filtered},
Output: output.Table{
Columns: []output.TableColumn{
{Header: "UUID", Key: "uuid", Colour: ui.DefaultUUUIDColours},
{Header: "Name", Key: "name"},
{Header: "Type", Key: "type"},
},
Rows: rows,
},
}, nil
}