forked from crackcomm/cloudflare
/
cmd_records_list.go
88 lines (77 loc) · 1.52 KB
/
cmd_records_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
package cmd
import (
"fmt"
"log"
"os"
"github.com/codegangsta/cli"
"github.com/olekukonko/tablewriter"
"golang.org/x/net/context"
"github.com/crackcomm/cloudflare"
)
var cmdRecordsList = cli.Command{
Name: "list",
Usage: "lists zone records",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "list",
Usage: "print list instead of table",
},
},
Action: func(c *cli.Context) {
zoneID, err := getZoneID(c)
if err != nil {
return
}
records, err := client(c).Records.List(context.Background(), zoneID)
if err != nil {
log.Fatal(err)
}
if c.Bool("list") {
for _, record := range records {
fmt.Println(record.ID)
}
return
}
table := newRecordsTable()
for _, record := range records {
table.add(record)
}
table.Render()
},
}
type recordsTable struct {
table *tablewriter.Table
}
func newRecordsTable() *recordsTable {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"ID",
"Type",
"Name",
"Content",
"Proxiable",
"Proxied",
"Locked",
"TTL",
"Created On",
"Modified On",
})
return &recordsTable{
table: table,
}
}
func (table *recordsTable) Render() { table.table.Render() }
func (table *recordsTable) add(record *cloudflare.Record) {
table.table.Append([]string{
record.ID,
record.Type,
record.Name,
record.Content,
yesOrNo(record.Proxiable),
yesOrNo(record.Proxied),
yesOrNo(record.Locked),
fmt.Sprintf("%d", record.TTL),
record.CreatedOn.Format("2006/01/02 15:04:05"),
record.ModifiedOn.Format("2006/01/02 15:04:05"),
})
}