This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasrc.go
113 lines (87 loc) · 2.73 KB
/
datasrc.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
)
// getJSONFile retreives the hostnames JSON file from the filesystem
// and attempts to read it into a []byte and return the value
func getJSONFile() []byte {
hostnamesFile, err := os.Open(getJSONFilePath())
if err != nil {
log.Fatalln(err)
}
defer hostnamesFile.Close() // defer the closing of our jsonFile so that we can parse it later on
byteValue, readErr := ioutil.ReadAll(hostnamesFile)
if readErr != nil {
log.Fatalln(err)
}
return byteValue
}
// writeJSONFile takes an interface payload
// and writes it to the hostnames JSON file in the filesystem
func writeJSONFile(payload interface{}) {
b, _ := json.MarshalIndent(payload, "", " ")
_ = ioutil.WriteFile(getJSONFilePath(), b, 0644)
}
// getHostnamesFromJSON gets the hostnames JSON file from getJSONFile
// and converts the bytevalue to JSON
func getHostnamesFromJSON() Hostnames {
var hostnames Hostnames
var byteValue = getJSONFile()
json.Unmarshal(byteValue, &hostnames)
return hostnames
}
// getHostnameByDomain takes a domain string
// then retrieves the hostname that matches that domain
// from the hostnames JSON file and returns it
func getHostnameByDomain(domain string) *Hostname {
var hostnames = getHostnamesFromJSON()
var result *Hostname
for _, hostname := range hostnames.Hostnames {
if hostname.Domain == domain {
result = &hostname
break
}
}
return result
}
// createHostname takes a hostname interface
// and writes it to the hostnames JSON file
// returning the updated hostnames
func createHostname(hostname Hostname) Hostnames {
var hostnames = getHostnamesFromJSON()
hostnames.Hostnames = append(hostnames.Hostnames, hostname)
writeJSONFile(hostnames)
return hostnames
}
// updateHostname takes a Hostname interface
// and retrieves the hostnames JSON, then updates the matching
// hostname with the passed values, returning the updated hostnames
func updateHostname(updatedHostname Hostname) Hostnames {
var hostnames = getHostnamesFromJSON()
for index, hostname := range hostnames.Hostnames {
if hostname.Domain == updatedHostname.Domain {
hostnames.Hostnames[index] = updatedHostname
break
}
}
writeJSONFile(hostnames)
return hostnames
}
// deleteHostname takes a domain string
// and looks for matching hostname in hostnames JSON
// then removes it, writing the updated hostnames to JSON file
// and returns the updated hostnames
func deleteHostname(domain string) Hostnames {
var hostnames = getHostnamesFromJSON()
for index, hostname := range hostnames.Hostnames {
if hostname.Domain == domain {
hostnames.Hostnames = append(hostnames.Hostnames[:index], hostnames.Hostnames[index+1:]...)
break
}
}
writeJSONFile(hostnames)
return hostnames
}