-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
51 lines (40 loc) · 898 Bytes
/
config.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
package huecontroller
import (
"bufio"
"encoding/json"
"log"
"os"
)
const CONFIG_FILE = "config.json"
type BridgeConfig struct {
Ip string `json:"ip"`
ApiKey string `json:"apikey"`
}
func (config *BridgeConfig) Load() {
f, err := os.Open(CONFIG_FILE)
if err != nil {
log.Fatal("error opening config file: ", err.Error())
}
defer f.Close()
scanner := bufio.NewScanner(f)
content := ""
for scanner.Scan() {
content += scanner.Text()
}
err = json.Unmarshal([]byte(content), config)
if err != nil {
log.Fatal("error parsing config file: ", err.Error())
}
}
func (config *BridgeConfig) Save() {
f, err := os.Create(CONFIG_FILE)
if err != nil {
log.Fatal("error creating config file: ", err.Error())
}
defer f.Close()
configBytes, err := json.Marshal(*config)
if err != nil {
log.Fatal("error saving config file: ", err.Error())
}
f.Write(configBytes)
}