Skip to content

Commit 5c0eadf

Browse files
committed
feat(database): add database list command
1 parent 533f484 commit 5c0eadf

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

internal/commands/all/all.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package all
33
import (
44
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
55
"github.com/UpCloudLtd/upcloud-cli/internal/commands/account"
6+
"github.com/UpCloudLtd/upcloud-cli/internal/commands/database"
67
"github.com/UpCloudLtd/upcloud-cli/internal/commands/ipaddress"
78
"github.com/UpCloudLtd/upcloud-cli/internal/commands/network"
89
"github.com/UpCloudLtd/upcloud-cli/internal/commands/networkinterface"
@@ -66,7 +67,7 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
6667
commands.BuildCommand(storage.CreateBackupCommand(), backupCommand.Cobra(), conf)
6768
commands.BuildCommand(storage.RestoreBackupCommand(), backupCommand.Cobra(), conf)
6869

69-
// // IP Addresses
70+
// IP Addresses
7071
ipAddressCommand := commands.BuildCommand(ipaddress.BaseIPAddressCommand(), rootCmd, conf)
7172
commands.BuildCommand(ipaddress.ListCommand(), ipAddressCommand.Cobra(), conf)
7273
commands.BuildCommand(ipaddress.ShowCommand(), ipAddressCommand.Cobra(), conf)
@@ -98,6 +99,10 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
9899
zoneCommand := commands.BuildCommand(zone.BaseZoneCommand(), rootCmd, conf)
99100
commands.BuildCommand(zone.ListCommand(), zoneCommand.Cobra(), conf)
100101

102+
// Databases
103+
databaseCommand := commands.BuildCommand(database.BaseDatabaseCommand(), rootCmd, conf)
104+
commands.BuildCommand(database.ListCommand(), databaseCommand.Cobra(), conf)
105+
101106
// Misc
102107
commands.BuildCommand(
103108
&root.CompletionCommand{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package database
2+
3+
import (
4+
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
5+
)
6+
7+
// BaseZoneCommand creates the base "zone" command
8+
func BaseDatabaseCommand() commands.Command {
9+
return &zoneCommand{
10+
commands.New("database", "Manage databases"),
11+
}
12+
}
13+
14+
type zoneCommand struct {
15+
*commands.BaseCommand
16+
}

internal/commands/database/list.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package database
2+
3+
import (
4+
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
5+
"github.com/UpCloudLtd/upcloud-cli/internal/output"
6+
"github.com/UpCloudLtd/upcloud-cli/internal/ui"
7+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
8+
)
9+
10+
// ListCommand creates the "database list" command
11+
func ListCommand() commands.Command {
12+
return &listCommand{
13+
BaseCommand: commands.New("list", "List current databases", "upctl database list"),
14+
}
15+
}
16+
17+
type listCommand struct {
18+
*commands.BaseCommand
19+
}
20+
21+
// ExecuteWithoutArguments implements commands.NoArgumentCommand
22+
func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
23+
svc := exec.All()
24+
databases, err := svc.GetManagedDatabases(&request.GetManagedDatabasesRequest{})
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
rows := []output.TableRow{}
30+
for _, db := range databases {
31+
rows = append(rows, output.TableRow{
32+
db.UUID,
33+
db.Title,
34+
db.Type,
35+
db.Plan,
36+
db.Zone,
37+
db.State,
38+
})
39+
}
40+
41+
return output.Table{
42+
Columns: []output.TableColumn{
43+
{Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours},
44+
{Key: "title", Header: "Title"},
45+
{Key: "type", Header: "Type"},
46+
{Key: "plan", Header: "Plan"},
47+
{Key: "zone", Header: "Zone"},
48+
{Key: "state", Header: "State"},
49+
},
50+
Rows: rows,
51+
}, nil
52+
}

0 commit comments

Comments
 (0)