summaryrefslogtreecommitdiff
path: root/config.go
blob: a4f280574d9554bdb95bc4848e53750b80be9fcb (plain)
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
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 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
}