-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
147 lines (124 loc) · 3.41 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"github.com/Sirupsen/logrus"
)
var (
leftPadding int = 22
)
// Capture error updating a record
// in case content (IP) is invalid.
type ValidationRecord struct {
Errors map[string][]string
}
type ErrorMessage struct {
Message string
}
func readConfig() *Config {
config := &Config{}
if _, err := os.Stat(configFile); os.IsNotExist(err) {
// Create the path and an empty configuration
createConfigPath()
}
configData, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatal("readConfig-ReadFile", err)
}
err = json.Unmarshal(configData, config)
if err != nil {
log.Fatal("readConfig-Unmarshal", err)
}
// Check ApiURL is properly terminated
if !strings.HasSuffix(config.ApiURL, "/") {
config.ApiURL = config.ApiURL + "/"
}
return config
}
func filterRecords(m MultipleRecords) {
stdoutHeader()
for _, item := range m {
r := item["record"]
// Skip system records are managed by DNSimple
if !r.SystemRecord {
stdoutRecord(r)
}
}
}
// Update record provides two different JSON responses
// for both error and success. Capture the error in case
// the IP is invalid. It does not validate the IP itself.
func validateRecordUpdate(data []byte) {
validation := new(ValidationRecord)
err := json.Unmarshal(data, &validation)
if err != nil {
log.Fatal("json-unmarshal", err)
}
if len(validation.Errors["content"]) > 1 {
fmt.Println(validation.Errors["content"][1])
os.Exit(1)
}
}
func stdoutHeader() {
headerFmt := fmt.Sprintf("%s%21s%22s%27s%21s", "Type", "Name", "TTL", "RecordID", "Content")
fmt.Println(headerFmt)
}
// Print out a record according the leftpadding
func stdoutRecord(r Record) {
recordType := "%" + fmt.Sprintf("%d", (leftPadding-len(r.RecordType))+len(r.Name+config.Domain)) + "s"
recordName := "%" + fmt.Sprintf("%d", (leftPadding-len(r.Name+config.Domain))+len(strconv.Itoa(r.TTL))) + "d"
recordTTL := "%" + fmt.Sprintf("%d", (leftPadding-len(strconv.Itoa(r.TTL)))+len(strconv.Itoa(r.ID))) + "d"
recordId := "%" + fmt.Sprintf("%d", (leftPadding-len(strconv.Itoa(r.ID)))+len(r.Content)) + "s"
data := fmt.Sprint("%s", recordType, recordName, recordTTL, recordId)
formatEnd := fmt.Sprintf("%s", data)
fmt.Printf(fmt.Sprintf(formatEnd, r.RecordType, r.Name+"."+config.Domain, r.TTL, r.ID, r.Content))
fmt.Println("")
}
func stdoutAutoRenew(a AutoRenew) {
lo := "false"
au := "false"
if a.Lockable {
lo = "true"
}
if a.AutoRenew {
au = "true"
}
headerFmt := fmt.Sprintf("%s%24s%23s", "Domain", "Lockable", "AutoRenew")
fmt.Println(headerFmt)
dom := "%" + fmt.Sprintf("%d", (leftPadding-len(a.Domain))+len(lo)) + "s"
lock := "%" + fmt.Sprintf("%d", (leftPadding-len(lo))+len(au)) + "s"
data := fmt.Sprint("%s", dom, lock)
formatEnd := fmt.Sprintf("%s", data)
fmt.Printf(fmt.Sprintf(formatEnd, a.Domain, lo, au))
fmt.Println("")
}
func isConfigEmpty(c Config) bool {
if c.ApiURL == "" || c.Mail == "" || c.Token == "" {
return true
}
return false
}
func isRecordDefined(data []byte) {
// The record is already create
validation := new(ErrorMessage)
json.Unmarshal(data, &validation)
if validation.Message != "" {
fmt.Println(validation.Message)
os.Exit(1)
}
}
func isDomainEmpty(domain string) {
if domain != "" {
config.Domain = domain
return
}
if config.Domain == "" {
logrus.Error("Set a domain in your configuration file or provide one.")
os.Exit(1)
}
}