-
Notifications
You must be signed in to change notification settings - Fork 235
/
dns.go
171 lines (137 loc) · 3.82 KB
/
dns.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package cmd
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/docstrings"
"github.com/superfly/flyctl/internal/client"
)
func newDNSCommand(client *client.Client) *Command {
dnsStrings := docstrings.Get("dns-records")
cmd := BuildCommandKS(nil, nil, dnsStrings, client, requireSession)
listStrings := docstrings.Get("dns-records.list")
listCmd := BuildCommandKS(cmd, runRecordsList, listStrings, client, requireSession)
listCmd.Args = cobra.ExactArgs(1)
recordsExportStrings := docstrings.Get("dns-records.export")
recordsExportCmd := BuildCommandKS(cmd, runRecordsExport, recordsExportStrings, client, requireSession)
recordsExportCmd.Args = cobra.MinimumNArgs(1)
recordsExportCmd.Args = cobra.MaximumNArgs(3)
recordsExportCmd.AddBoolFlag(BoolFlagOpts{
Name: "overwrite",
})
recordsImportStrings := docstrings.Get("dns-records.import")
recordsImportCmd := BuildCommandKS(cmd, runRecordsImport, recordsImportStrings, client, requireSession)
recordsImportCmd.Args = cobra.MaximumNArgs(3)
recordsImportCmd.Args = cobra.MinimumNArgs(1)
return cmd
}
func runRecordsList(ctx *cmdctx.CmdContext) error {
name := ctx.Args[0]
records, err := ctx.Client.API().GetDNSRecords(name)
if err != nil {
return err
}
fmt.Printf("Records for domain %s\n", name)
if ctx.OutputJSON() {
ctx.WriteJSON(records)
return nil
}
table := tablewriter.NewWriter(ctx.Out)
table.SetAutoWrapText(true)
table.SetReflowDuringAutoWrap(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetNoWhiteSpace(true)
table.SetTablePadding(" ")
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeader([]string{"FQDN", "TTL", "Type", "Content"})
for _, record := range records {
table.Append([]string{record.FQDN, strconv.Itoa(record.TTL), record.Type, record.RData})
}
table.Render()
return nil
}
func runRecordsExport(ctx *cmdctx.CmdContext) error {
name := ctx.Args[0]
domain, err := ctx.Client.API().GetDomain(name)
if err != nil {
return err
}
records, err := ctx.Client.API().ExportDNSRecords(domain.ID)
if err != nil {
return err
}
if len(ctx.Args) == 1 {
fmt.Println(records)
} else {
var filename = ctx.Args[1]
_, err := os.Stat(filename)
if err == nil {
return fmt.Errorf("File %s already exists", filename)
}
err = ioutil.WriteFile(filename, []byte(records), 0644)
if err != nil {
return err
}
fmt.Printf("Zone exported to %s\n", filename)
}
return nil
}
func runRecordsImport(ctx *cmdctx.CmdContext) error {
name := ctx.Args[0]
var filename string
if len(ctx.Args) == 1 {
// One arg, use stdin
filename = "-"
} else {
filename = ctx.Args[1]
}
domain, err := ctx.Client.API().GetDomain(name)
if err != nil {
return err
}
var data []byte
if filename != "-" {
data, err = ioutil.ReadFile(filename)
if err != nil {
return err
}
} else {
data, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
}
warnings, changes, err := ctx.Client.API().ImportDNSRecords(domain.ID, string(data))
if err != nil {
return err
}
fmt.Printf("Zonefile import report for %s\n", domain.Name)
if filename == "-" {
fmt.Printf("Imported from stdin\n")
} else {
fmt.Printf("Imported from %s\n", filename)
}
fmt.Printf("%d warnings\n", len(warnings))
for _, warning := range warnings {
fmt.Println("->", warning.Action, warning.Message)
}
fmt.Printf("%d changes\n", len(changes))
for _, change := range changes {
switch change.Action {
case "CREATE":
fmt.Println("-> Created", change.NewText)
case "DELETE":
fmt.Println("-> Deleted", change.OldText)
case "UPDATE":
fmt.Println("-> Updated", change.OldText, "=>", change.NewText)
}
}
return nil
}