-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (165 loc) · 3.02 KB
/
main.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
172
173
174
175
176
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/cloudflare/cloudflare-go"
)
var (
api *cloudflare.API
raw bool
)
func init() {
var err error
api, err = cloudflare.NewWithAPIToken(getToken())
if err != nil {
log.Fatal(err)
}
}
func main() {
flag.BoolVar(&raw, "raw", false, "show json")
flag.Parse()
switch flag.Arg(0) {
case "ls":
if flag.NArg() != 1 {
showHelp()
}
listZones()
case "records":
if flag.NArg() != 2 {
showHelp()
}
listZoneRecords(flag.Arg(1))
case "addrecord":
if flag.NArg() != 5 {
showHelp()
}
createDNSRecord(flag.Arg(1), flag.Arg(2), flag.Arg(3), flag.Arg(4))
case "delrecord":
if flag.NArg() != 3 {
showHelp()
}
deleteDNSRecord(flag.Arg(1), flag.Arg(2))
default:
showHelp()
}
}
func getToken() (token string) {
var err error
token = os.Getenv("CF_TOKEN")
if token != "" {
return
}
var home string
home, err = os.UserHomeDir()
if err != nil {
return
}
file := filepath.Join(home, ".cloudflare.json")
var fileContent []byte
fileContent, err = ioutil.ReadFile(file)
if err != nil {
return
}
var config struct {
Token string `json:"token"`
}
err = json.Unmarshal(fileContent, &config)
if err != nil {
return
}
token = config.Token
return
}
func showHelp() {
fmt.Println("commands:")
fmt.Println(" ls")
fmt.Println(" records <domain>")
fmt.Println(" addrecord <domain> <name> <type> <value>")
fmt.Println(" delrecord <domain> <recordid>")
os.Exit(1)
}
func listZones() {
zones, err := api.ListZones()
if err != nil {
log.Fatal(err)
}
if raw {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "\t")
err = enc.Encode(zones)
if err != nil {
log.Fatal(err)
}
} else {
for _, zone := range zones {
fmt.Println(zone.Name)
}
}
}
func listZoneRecords(name string) {
zoneID, err := api.ZoneIDByName(name)
if err != nil {
log.Fatal(err)
}
recs, err := api.DNSRecords(zoneID, cloudflare.DNSRecord{})
if err != nil {
log.Fatal(err)
}
if raw {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "\t")
err = enc.Encode(recs)
if err != nil {
log.Fatal(err)
}
} else {
for _, rec := range recs {
fmt.Printf("%s\t%s\t%s\t%s\n", rec.ID, rec.Type, rec.Content, rec.Name)
}
}
}
func createDNSRecord(name, dname, dtype, dvalue string) {
zoneID, err := api.ZoneIDByName(name)
if err != nil {
log.Fatal(err)
}
resp, err := api.CreateDNSRecord(zoneID, cloudflare.DNSRecord{
Name: dname,
Type: dtype,
Content: dvalue,
})
if err != nil {
log.Fatal(err)
}
if raw {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "\t")
err = enc.Encode(resp)
if err != nil {
log.Fatal(err)
}
}
}
func deleteDNSRecord(name, recordId string) {
zoneID, err := api.ZoneIDByName(name)
if err != nil {
log.Fatal(err)
}
err = api.DeleteDNSRecord(zoneID, recordId)
if err != nil {
log.Fatal(err)
}
if raw {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "\t")
err = enc.Encode(true)
if err != nil {
log.Fatal(err)
}
}
}