package main import ( "encoding/json" "io" "os" "path/filepath" ) const configPathRelative = `.config/soundbox/config.json` type SoundboxConfig struct { Name string `json:"name"` Mac string `json:"mac"` } type GlobalConfig struct { Soundboxes []SoundboxConfig `json:"soundboxes"` } 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 }