-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
66 lines (56 loc) · 1.25 KB
/
conf.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
package main
import (
"io/ioutil"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
var conf Config
type Config struct {
configPath string
Addr string `yaml:"addr"`
Token string `yaml:"token"`
BotApiAddr string `yaml:"bot_api_addr"`
BotApiToken string `yaml:"bot_api_token"`
}
// set config file path
func (cfg *Config) SetConfigPath(configPath string) {
cfg.configPath = configPath
}
// write config
func (cfg *Config) Write() error {
if cfg.configPath == "" {
return errors.New("config path not set")
}
out, err := yaml.Marshal(cfg)
if err != nil {
return err
}
return ioutil.WriteFile(cfg.configPath, out, 0644)
}
// write config to yaml file
func (cfg *Config) WriteTo(filePath string) error {
if filePath == "" {
return errors.New("file path is empty")
}
cfg.configPath = filePath
return cfg.Write()
}
// load config
func (cfg *Config) Load() error {
if cfg.configPath == "" {
return errors.New("config path not set")
}
buf, err := ioutil.ReadFile(cfg.configPath)
if err != nil {
return err
}
return yaml.Unmarshal(buf, cfg)
}
// load config from yaml file
func (cfg *Config) LoadFrom(filePath string) error {
if filePath == "" {
return errors.New("file path is empty")
}
cfg.configPath = filePath
return cfg.Load()
}