-
Notifications
You must be signed in to change notification settings - Fork 271
/
formatters.go
97 lines (82 loc) · 2.12 KB
/
formatters.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
94
95
96
97
package cli53
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"text/tabwriter"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/urfave/cli/v2"
)
type Formatter interface {
formatZoneList(zones <-chan *route53.HostedZone, w io.Writer)
}
type TextFormatter struct {
}
func (self *TextFormatter) formatZoneList(zones <-chan *route53.HostedZone, w io.Writer) {
for zone := range zones {
fmt.Fprintf(w, "%+v\n", zone)
}
}
type JsonFormatter struct {
}
func (self *JsonFormatter) formatZoneList(zones <-chan *route53.HostedZone, w io.Writer) {
var all []*route53.HostedZone
for zone := range zones {
all = append(all, zone)
}
if err := json.NewEncoder(w).Encode(all); err != nil {
fatalIfErr(err)
}
}
type JlFormatter struct {
}
func (self *JlFormatter) formatZoneList(zones <-chan *route53.HostedZone, w io.Writer) {
for zone := range zones {
if err := json.NewEncoder(w).Encode(zone); err != nil {
fatalIfErr(err)
}
}
}
type TableFormatter struct {
}
func zoneComment(zone *route53.HostedZone) string {
var ret string
if zone.Config != nil && zone.Config.Comment != nil {
ret = *zone.Config.Comment
}
return ret
}
func (self *TableFormatter) formatZoneList(zones <-chan *route53.HostedZone, w io.Writer) {
wr := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
fmt.Fprintln(wr, "ID\tName\tRecord count\tComment")
for zone := range zones {
fmt.Fprintf(wr, "%s\t%s\t%d\t%s\n", (*zone.Id)[12:], *zone.Name, *zone.ResourceRecordSetCount, zoneComment(zone))
}
wr.Flush()
}
type CSVFormatter struct {
}
func (self *CSVFormatter) formatZoneList(zones <-chan *route53.HostedZone, w io.Writer) {
wr := csv.NewWriter(w)
wr.Write([]string{"id", "name", "record count", "comment"})
for zone := range zones {
wr.Write([]string{(*zone.Id)[12:], *zone.Name, fmt.Sprint(*zone.ResourceRecordSetCount), zoneComment(zone)})
}
wr.Flush()
}
func getFormatter(c *cli.Context) Formatter {
switch c.String("format") {
case "text":
return &TextFormatter{}
case "json":
return &JsonFormatter{}
case "jl":
return &JlFormatter{}
case "table":
return &TableFormatter{}
case "csv":
return &CSVFormatter{}
}
return nil
}