-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.go
79 lines (64 loc) · 1.66 KB
/
configuration.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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
)
type Configuration struct {
hosts []Host
}
// ReadConfig reads the configuration file
func NewConfiguration(configPath string) (*Configuration, error) {
confBytes, err := os.ReadFile(configPath)
if err != nil {
return nil, err
}
var hosts []Host
err = json.Unmarshal(confBytes, &hosts)
if err != nil {
return nil, err
}
this := new(Configuration)
for i := range hosts {
if strings.TrimSpace(hosts[i].Hostname) == "" {
fmt.Printf("Record %d (%s) has errors and is not loaded:\n", i, hosts[i].FQDN())
fmt.Println("Hostname property not set")
}
if strings.TrimSpace(hosts[i].Zone) == "" {
fmt.Printf("Record %d (%s) has errors and is not loaded:\n", i, hosts[i].FQDN())
fmt.Println("Zone property not set")
}
if strings.TrimSpace(hosts[i].APIToken) == "" {
fmt.Printf("Record %d (%s) has errors and is not loaded:\n", i, hosts[i].FQDN())
fmt.Println("APIToken property not set")
}
err := hosts[i].Populate()
if err != nil {
fmt.Printf("Record %d (%s) has errors and is not loaded:\n", i, hosts[i].FQDN())
fmt.Println(err)
continue
}
this.hosts = append(this.hosts, hosts[i])
}
confBytes, err = json.MarshalIndent(hosts, "", "\t")
if err != nil {
return nil, err
}
err = os.WriteFile(configPath, confBytes, 0600)
if err != nil {
return nil, err
}
if len(this.hosts) == 0 {
return nil, fmt.Errorf("no records were loaded")
}
return this, nil
}
func (c *Configuration) GetHost(fqdn string) (*Host, error) {
for i := range c.hosts {
if c.hosts[i].FQDN() == fqdn {
return &c.hosts[i], nil
}
}
return nil, fmt.Errorf("unknown fqdn %s", fqdn)
}