-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
59 lines (46 loc) · 883 Bytes
/
config.go
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
54
55
56
57
58
59
package main
import (
"encoding/json"
"io/ioutil"
"os"
)
type config struct {
MusicDir string `json:"music_dir"` //MusicDir is the directory where the
MaxPlaylistSize int `json:"max_playlist_size"`
}
func loadConfig() config {
var cfg config
f, err := os.Open("config.json")
if err != nil {
if !os.IsNotExist(err) {
panic(err)
}
h, err := os.UserHomeDir()
if err != nil {
panic(err)
}
cfg.MusicDir = h + "/Music"
cfg.MaxPlaylistSize = 25
f, err := os.Create("config.json")
if err != nil {
panic(err)
}
newConfig, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
panic(err)
}
if _, err = f.Write(newConfig); err != nil {
panic(err)
}
f.Close()
}
b, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
f.Close()
if err = json.Unmarshal(b, &cfg); err != nil {
panic(err)
}
return cfg
}