package main import ( "encoding/json" "io" "net" "os" "path/filepath" ) const configPathRelative = `.config/soundbox/config.json` type Config struct { Soundboxes []struct { Name string Mac net.HardwareAddr Enabled bool } URLs []struct { Name string Url string } } func (config *Config) UnmarshalJSON(data []byte) error { var buffer struct { Soundboxes []struct { Name string Mac string Enabled bool } URLs []struct { Name string Url string } } err := json.Unmarshal(data, &buffer) if err != nil { return err } parsed := Config{} 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 Enabled bool }{soundbox.Name, hwAddr, soundbox.Enabled}) } for _, url := range buffer.URLs { parsed.URLs = append(parsed.URLs, struct { Name string Url string }{url.Name, url.Url}) } *config = parsed return nil } func loadConfig() (Config, error) { home, err := os.UserHomeDir() if err != nil { return Config{}, err } path := filepath.Join(home, configPathRelative) file, err := os.Open(path) if err != nil { return Config{}, err } defer file.Close() bytes, err := io.ReadAll(file) if err != nil { return Config{}, err } var config Config err = json.Unmarshal(bytes, &config) if err != nil { return Config{}, err } return config, nil }