diff options
Diffstat (limited to 'config.go')
-rw-r--r-- | config.go | 73 |
1 files changed, 46 insertions, 27 deletions
@@ -10,62 +10,81 @@ import ( const configPathRelative = `.config/soundbox/config.json` -type MacAddress net.HardwareAddr +type Config struct { + Soundboxes []struct { + Name string + Mac net.HardwareAddr + } + URLs []struct { + Name string + Url string + } +} -func (m *MacAddress) UnmarshalJSON(data []byte) error { - var macStr string - err := json.Unmarshal(data, &macStr) - if err != nil { - return err +func (config *Config) UnmarshalJSON(data []byte) error { + var buffer struct { + Soundboxes []struct { + Name string + Mac string + } + URLs []struct { + Name string + Url string + } } - hwAddr, err := net.ParseMAC(macStr) + err := json.Unmarshal(data, &buffer) if err != nil { return err } - *m = MacAddress(hwAddr) - return nil -} + parsed := Config{} -type SoundboxConfig struct { - Name string `json:"name"` - Mac MacAddress `json:"mac"` -} + for _, soundbox := range buffer.Soundboxes { + hwAddr, err := net.ParseMAC(soundbox.Mac) + if err != nil { + return err + } + parsed.Soundboxes = append(parsed.Soundboxes, struct { + Name string + Mac net.HardwareAddr + }{soundbox.Name, hwAddr}) + } -type URLConfig struct { - Name string `json:"name"` - Url string `json:"url"` -} + for _, url := range buffer.URLs { + parsed.URLs = append(parsed.URLs, struct { + Name string + Url string + }{url.Name, url.Url}) + } -type GlobalConfig struct { - Soundboxes []SoundboxConfig `json:"soundboxes"` - URLs []URLConfig `json:"urls"` + *config = parsed + return nil } -func loadConfig() (GlobalConfig, error) { +func loadConfig() (Config, error) { home, err := os.UserHomeDir() if err != nil { - return GlobalConfig{}, err + return Config{}, err } path := filepath.Join(home, configPathRelative) file, err := os.Open(path) if err != nil { - return GlobalConfig{}, err + return Config{}, err } defer file.Close() bytes, err := io.ReadAll(file) if err != nil { - return GlobalConfig{}, err + return Config{}, err } - var config GlobalConfig + var config Config err = json.Unmarshal(bytes, &config) if err != nil { - return GlobalConfig{}, err + return Config{}, err } return config, nil |