-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
99 lines (79 loc) · 1.99 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
var defaultConfig = map[string]interface{}{
"log-file": "webinc.log",
"log-level": "info",
}
type Config struct {
configFileName string
config map[string]interface{}
}
func NewConfig() (*Config, error) {
// Search TOML configuration
homeDir, err := os.UserHomeDir()
if err != nil {
log.Info("Failed to locate HOME folder, defaulting to CWD")
}
configFile := filepath.Join(homeDir, ".config/webinc/webinc.conf")
c := &Config{
configFileName: configFile,
config: make(map[string]interface{}),
}
return c, c.readConfigFile()
}
func (c *Config) getValue(name string) interface{} {
if v, ok := c.config[name]; ok {
return v
}
return defaultConfig[name]
}
func (c *Config) readConfigFile() error {
if _, err := os.Stat(c.configFileName); os.IsNotExist(err) {
// Nothing to read
return nil
}
if _, err := toml.DecodeFile(c.configFileName, &c.config); err != nil {
return errors.Wrap(err, "failed to parse config file")
}
return nil
}
func (c *Config) GetString(name string) string {
v := c.getValue(name)
if v == nil {
return ""
} else if s, ok := v.(string); ok {
return s
} else {
return fmt.Sprintf("%v", v)
}
}
func (c *Config) SetString(name string, value string) {
c.config[name] = value
}
func (c *Config) Save() error {
// Check if folder exists
folder := filepath.Dir(c.configFileName)
if _, err := os.Stat(folder); os.IsNotExist(err) {
err := os.MkdirAll(folder, 0700)
if err != nil {
return errors.Wrap(err, "failed to create folder for configuration file")
}
}
file, err := os.OpenFile(c.configFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return errors.Wrap(err, "failed to open file to write configuration")
}
defer file.Close()
err = toml.NewEncoder(file).Encode(c.config)
if err != nil {
return errors.Wrap(err, "failed to write configuration")
}
return nil
}