-
Notifications
You must be signed in to change notification settings - Fork 20
/
dns_list.go
79 lines (67 loc) · 1.34 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
package cmd
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/exoscale/cli/table"
"github.com/spf13/cobra"
)
// dnsListCmd represents the list command
var dnsListCmd = &cobra.Command{
Use: "list [domain name | id]*",
Short: "List domains",
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
t := table.NewTable(os.Stdout)
err := listDomains(t, args)
if err == nil {
t.Render()
}
return err
},
}
func listDomains(t *table.Table, filters []string) error {
domains, err := csDNS.GetDomains(gContext)
if err != nil {
return err
}
data := make([][]string, 0)
for _, d := range domains {
keep := true
if len(filters) > 0 {
keep = false
s := strings.ToLower(
fmt.Sprintf("%d#%s#%s", d.ID, d.Name, d.UnicodeName))
for _, filter := range filters {
substr := strings.ToLower(filter)
if strings.Contains(s, substr) {
keep = true
break
}
}
}
if !keep {
continue
}
unicodeName := ""
if d.Name != d.UnicodeName {
unicodeName = d.UnicodeName
}
data = append(data, []string{
d.Name, unicodeName, strconv.FormatInt(d.ID, 10),
})
}
headers := []string{"Name", "Unicode", "ID"}
if len(data) > 0 {
t.SetHeader(headers)
}
t.AppendBulk(data)
if len(data) > 10 {
t.SetFooter(headers)
}
return nil
}
func init() {
dnsCmd.AddCommand(dnsListCmd)
}