-
Notifications
You must be signed in to change notification settings - Fork 18
/
db_service_list.go
93 lines (72 loc) · 2.2 KB
/
db_service_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
92
93
package cmd
import (
"fmt"
"os"
"strings"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type dbServiceListItemOutput struct {
Name string `json:"name"`
Type string `json:"type"`
Plan string `json:"plan"`
Zone string `json:"zone"`
}
type dbServiceListOutput []dbServiceListItemOutput
func (o *dbServiceListOutput) toJSON() { outputJSON(o) }
func (o *dbServiceListOutput) toText() { outputText(o) }
func (o *dbServiceListOutput) toTable() { outputTable(o) }
type dbServiceListCmd struct {
_ bool `cli-cmd:"list"`
Zone string `cli-short:"z" cli-usage:"zone to filter results to"`
}
func (c *dbServiceListCmd) cmdAliases() []string { return gListAlias }
func (c *dbServiceListCmd) cmdShort() string { return "List Database Services" }
func (c *dbServiceListCmd) cmdLong() string {
return fmt.Sprintf(`This command lists Database Services.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&dbServiceListItemOutput{}), ", "))
}
func (c *dbServiceListCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *dbServiceListCmd) cmdRun(_ *cobra.Command, _ []string) error {
var zones []string
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
}
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
out := make(dbServiceListOutput, 0)
res := make(chan dbServiceListItemOutput)
defer close(res)
go func() {
for dbService := range res {
out = append(out, dbService)
}
}()
err := forEachZone(zones, func(zone string) error {
list, err := cs.ListDatabaseServices(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list Database Services in zone %s: %v", zone, err)
}
for _, dbService := range list {
res <- dbServiceListItemOutput{
Name: dbService.Name,
Type: dbService.Type,
Plan: dbService.Plan,
Zone: zone,
}
}
return nil
})
if err != nil {
_, _ = fmt.Fprintf(os.Stderr,
"warning: errors during listing, results might be incomplete.\n%s\n", err) // nolint:golint
}
return output(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(dbCmd, &dbServiceListCmd{}))
}