-
Notifications
You must be signed in to change notification settings - Fork 18
/
dbaas_type_list.go
91 lines (72 loc) · 2.22 KB
/
dbaas_type_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
83
84
85
86
87
88
89
90
91
package cmd
import (
"fmt"
"os"
"strings"
"github.com/exoscale/cli/table"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type dbaasTypeListItemOutput struct {
Name string `json:"name"`
AvailableVersions []string `json:"available_versions"`
DefaultVersion string `json:"default_version"`
}
type dbaasTypeListOutput []dbaasTypeListItemOutput
func (o *dbaasTypeListOutput) toJSON() { outputJSON(o) }
func (o *dbaasTypeListOutput) toText() { outputText(o) }
func (o *dbaasTypeListOutput) toTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Name", "Available Versions", "Default Version"})
defer t.Render()
for _, dbType := range *o {
t.Append([]string{
dbType.Name,
strings.Join(dbType.AvailableVersions, ", "),
dbType.DefaultVersion,
})
}
}
type dbaasTypeListCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"list"`
}
func (c *dbaasTypeListCmd) cmdAliases() []string { return nil }
func (c *dbaasTypeListCmd) cmdShort() string { return "List Database Service types" }
func (c *dbaasTypeListCmd) cmdLong() string {
return fmt.Sprintf(`This command lists available Database Service types.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&dbaasTypeListItemOutput{}), ", "))
}
func (c *dbaasTypeListCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *dbaasTypeListCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(gCurrentAccount.Environment, gCurrentAccount.DefaultZone),
)
dbTypes, err := cs.ListDatabaseServiceTypes(ctx, gCurrentAccount.DefaultZone)
if err != nil {
return err
}
out := make(dbaasTypeListOutput, 0)
for _, t := range dbTypes {
out = append(out, dbaasTypeListItemOutput{
Name: *t.Name,
DefaultVersion: defaultString(t.DefaultVersion, "-"),
AvailableVersions: func() (v []string) {
if t.AvailableVersions != nil {
v = *t.AvailableVersions
}
return
}(),
})
}
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(dbaasTypeCmd, &dbaasTypeListCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}