-
Notifications
You must be signed in to change notification settings - Fork 6
/
list.go
72 lines (64 loc) · 1.74 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
package database
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"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/paging"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
)
// ListCommand creates the "database list" command
func ListCommand() commands.Command {
return &listCommand{
BaseCommand: commands.New("list", "List current databases", "upctl database list"),
}
}
type listCommand struct {
*commands.BaseCommand
paging.PageParameters
}
func (s *listCommand) InitCommand() {
fs := &pflag.FlagSet{}
s.ConfigureFlags(fs)
s.AddFlags(fs)
}
// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
svc := exec.All()
databases, err := svc.GetManagedDatabases(exec.Context(), &request.GetManagedDatabasesRequest{
Page: s.Page(),
})
if err != nil {
return nil, err
}
rows := []output.TableRow{}
for _, db := range databases {
title := db.Title
if title == "" {
title = db.Name
}
rows = append(rows, output.TableRow{
db.UUID,
title,
db.Type,
db.Plan,
db.Zone,
db.State,
})
}
return output.MarshaledWithHumanOutput{
Value: databases,
Output: output.Table{
Columns: []output.TableColumn{
{Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours},
{Key: "title", Header: "Title"},
{Key: "type", Header: "Type"},
{Key: "plan", Header: "Plan"},
{Key: "zone", Header: "Zone"},
{Key: "state", Header: "State", Format: format.DatabaseState},
},
Rows: rows,
},
}, nil
}