-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
60 lines (55 loc) · 1.19 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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"os/user"
"path"
)
func checkForConfig(filename string) (config Config, err error) {
usr, err := user.Current()
if err != nil {
return config, err
}
if filename == "" {
filename = path.Join(usr.HomeDir + "/.alyconfig.json")
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
config = createConfig()
config.updateJSON()
} else {
file, err := ioutil.ReadFile(path.Join(usr.HomeDir + "/.alyconfig.json"))
if err != nil {
return config, err
}
json.Unmarshal(file, &config)
}
return config, nil
}
func (config Config) updateJSON() (err error) {
usr, err := user.Current()
if err != nil {
return err
}
configJSON, err := json.Marshal(&config)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(usr.HomeDir+"/.alyconfig.json"), configJSON, os.ModePerm)
if err != nil {
return err
}
return nil
}
func (config Config) allPlugins() (plugins []Plugin) {
plugins = append(config.DisabledPlugins, config.EnabledPlugins...)
return plugins
}
func createConfig() (config Config) {
config = Config{
Version: "1.1.0",
EnabledPlugins: []Plugin{},
DisabledPlugins: []Plugin{},
}
return config
}