-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
62 lines (51 loc) · 1.13 KB
/
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
60
61
62
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path"
)
const configFilename = "$HOME/.gomuche/config.json"
// Config represents gomuche config file
type Config struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
// NewConfig returns a new Config.
func NewConfig(clientID, clientSecret string) *Config {
return &Config{
ClientID: clientID,
ClientSecret: clientSecret,
}
}
// NewConfigFromFile read config from file and returns it.
func NewConfigFromFile() *Config {
filename := os.ExpandEnv(configFilename)
bytes, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalln(err)
}
config := new(Config)
err = json.Unmarshal(bytes, &config)
if err != nil {
log.Fatalln(err)
}
return config
}
// SaveConfig saves config to file.
func SaveConfig(conf *Config) {
bytes, err := json.MarshalIndent(conf, "", " ")
if err != nil {
log.Fatalln(err)
}
filename := os.ExpandEnv(configFilename)
err = os.MkdirAll(path.Dir(filename), 0755)
if err != nil {
log.Fatalln(err)
}
err = ioutil.WriteFile(filename, bytes, 0755)
if err != nil {
log.Fatalln(err)
}
}