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 }