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
|
package main
import (
"encoding/json"
"io"
"net"
"os"
"path/filepath"
)
const configPathRelative = `.config/soundbox/config.json`
type MacAddress net.HardwareAddr
func (m *MacAddress) UnmarshalJSON(data []byte) error {
var macStr string
err := json.Unmarshal(data, &macStr)
if err != nil {
return err
}
hwAddr, err := net.ParseMAC(macStr)
if err != nil {
return err
}
*m = MacAddress(hwAddr)
return nil
}
type SoundboxConfig struct {
Name string `json:"name"`
Mac MacAddress `json:"mac"`
}
type URLConfig struct {
Name string `json:"name"`
Url string `json:"url"`
}
type GlobalConfig struct {
Soundboxes []SoundboxConfig `json:"soundboxes"`
URLs []URLConfig `json:"urls"`
}
func loadConfig() (GlobalConfig, error) {
home, err := os.UserHomeDir()
if err != nil {
return GlobalConfig{}, err
}
path := filepath.Join(home, configPathRelative)
file, err := os.Open(path)
if err != nil {
return GlobalConfig{}, err
}
defer file.Close()
bytes, err := io.ReadAll(file)
if err != nil {
return GlobalConfig{}, err
}
var config GlobalConfig
err = json.Unmarshal(bytes, &config)
if err != nil {
return GlobalConfig{}, err
}
return config, nil
}
|