-
Notifications
You must be signed in to change notification settings - Fork 20
/
dns_show.go
88 lines (72 loc) · 2.13 KB
/
dns_show.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
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
)
type dnsShowItemOutput struct {
ID int64 `json:"id"`
DomainID int64 `json:"domain_id" output:"-"`
Name string `json:"name"`
RecordType string `json:"record_type"`
Content string `json:"content"`
Prio int `json:"prio,omitempty"`
TTL int `json:"ttl,omitempty"`
CreatedAt string `json:"created_at,omitempty" output:"-"`
UpdatedAt string `json:"updated_at,omitempty" output:"-"`
}
type dnsShowOutput []dnsShowItemOutput
func (o *dnsShowOutput) toJSON() { outputJSON(o) }
func (o *dnsShowOutput) toText() { outputText(o) }
func (o *dnsShowOutput) toTable() { outputTable(o) }
func init() {
var dnsShowCmd = &cobra.Command{
Use: "show <domain name | id> [type ...]",
Short: "Show the domain records",
Long: fmt.Sprintf(`This command shows a DNS Domain records.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&dnsShowOutput{}), ", ")),
RunE: func(cmd *cobra.Command, args []string) error {
var types []string
if len(args) < 1 {
return errors.New("show expects one DNS domain by name or id")
}
if len(args) > 1 {
types = make([]string, len(args)-1)
copy(types, args[1:])
} else {
types = []string{""}
}
name, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
return output(showDNS(args[0], name, types))
},
}
dnsCmd.AddCommand(dnsShowCmd)
dnsShowCmd.Flags().StringP("name", "n", "", "List records by name")
}
func showDNS(domain, name string, types []string) (outputter, error) {
out := dnsShowOutput{}
for _, recordType := range types {
records, err := csDNS.GetRecordsWithFilters(gContext, domain, name, recordType)
if err != nil {
return nil, err
}
for _, record := range records {
out = append(out, dnsShowItemOutput{
ID: record.ID,
Name: record.Name,
RecordType: record.RecordType,
Content: record.Content,
TTL: record.TTL,
Prio: record.Prio,
CreatedAt: record.CreatedAt,
UpdatedAt: record.UpdatedAt,
})
}
}
return &out, nil
}