-
Notifications
You must be signed in to change notification settings - Fork 18
/
dns_list.go
87 lines (68 loc) · 1.78 KB
/
dns_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
package cmd
import (
"fmt"
"os"
"strings"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/cli/table"
"github.com/spf13/cobra"
)
type dnsListItemOutput struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
}
type dnsListOutput []dnsListItemOutput
func (o *dnsListOutput) toJSON() { outputJSON(o) }
func (o *dnsListOutput) toText() { outputText(o) }
func (o *dnsListOutput) toTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"ID", "Name"})
for _, i := range *o {
t.Append([]string{
i.ID,
i.Name,
})
}
t.Render()
}
func init() {
dnsCmd.AddCommand(&cobra.Command{
Use: "list [FILTER]...",
Short: "List domains",
Long: fmt.Sprintf(`This command lists existing DNS Domains.
Optional patterns can be provided to filter results by ID, or name.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&dnsListOutput{}), ", ")),
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
return output(listDomains(args))
},
})
}
func listDomains(filters []string) (outputter, error) {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, gCurrentAccount.DefaultZone))
domains, err := cs.ListDNSDomains(ctx, gCurrentAccount.DefaultZone)
if err != nil {
return nil, err
}
out := dnsListOutput{}
for _, d := range domains {
o := dnsListItemOutput{
ID: StrPtrFormatOutput(d.ID),
Name: StrPtrFormatOutput(d.UnicodeName),
}
if len(filters) == 0 {
out = append(out, o)
continue
}
s := strings.ToLower(fmt.Sprintf("%s#%s", o.ID, o.Name))
for _, filter := range filters {
substr := strings.ToLower(filter)
if strings.Contains(s, substr) {
out = append(out, o)
break
}
}
}
return &out, nil
}