-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.go
93 lines (78 loc) · 2.09 KB
/
address.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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
)
// Implement the "address" command.
func commandA(invokedAs []string) error {
commands := Commands{
"get": getA,
"delete": deleteA,
"add": addA,
"update": updateA,
}
if len(os.Args) < 2 {
commandHelp(invokedAs, commands, "need a subcommand", 1)
}
if strings.HasPrefix(os.Args[1], "-") || strings.EqualFold(os.Args[1], "help") {
commandHelp(invokedAs, commands, "", 0)
}
command := strings.ToLower(os.Args[1])
os.Args[0] += " " + os.Args[1]
os.Args = append(os.Args[:1], os.Args[2:]...)
function, ok := commands[command]
if !ok {
commandHelp(invokedAs, commands, fmt.Sprintf("unrecognized command \"%s\"", command), 1)
}
// Run the func for the specified command:
invokedAs = append(invokedAs, command)
return function(invokedAs)
}
// Define a set of methods for managing record "states". See also the states.go file.
func (s StatesA) AddRecords(nameData string, body []byte) error {
if len(s[nameData].records) == 0 {
s[nameData].err = json.Unmarshal(body, &s[nameData].records)
} else {
var records []*RecordA
if s[nameData].err = json.Unmarshal(body, &records); s[nameData].err == nil {
for _, record := range records {
if !findRefA(s[nameData].records, record.Ref) {
s[nameData].records = append(s[nameData].records, record)
}
}
}
}
return nil
}
func (s StatesA) NewState(nameData string) {
s[nameData] = new(StateA)
}
func (s StatesA) GetObjectType() string {
return "record:a"
}
func (s StatesA) GetNDKeys() (string, string) {
return "name", "ipv4addr"
}
func (s StatesA) GetNDPairs() (nds []string) {
return keys(s)
}
func (s StatesA) SetError(nameData string, err error) {
s[nameData].err = err
}
func (s StatesA) GetError(nd string) error {
return s[nd].err
}
func (s StatesA) GetRecordCount(nd string) int {
return len(s[nd].records)
}
// Check if one or more records in the specified list have the specified object reference.
func findRefA(records []*RecordA, ref string) bool {
for _, record := range records {
if record.Ref == ref {
return true
}
}
return false
}