-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
74 lines (54 loc) · 1.16 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
package api
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
)
// Config contain the necessary data to interact with the API
type Config struct {
Id int `yaml:"id"`
Address string `yaml:"address"`
Username string `yaml:"username"`
Steps int `yaml:"steps"`
}
func getConfigPath() (configPath string, err error) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return
}
configPath = filepath.Join(dir, "config.yml")
return
}
// GetConfig read the config.yml file in the executable directory and return it
func GetConfig() (config Config, err error) {
configPath, err := getConfigPath()
if err != nil {
return
}
configBytes, err := ioutil.ReadFile(configPath)
if err != nil {
return
}
err = yaml.Unmarshal(configBytes, &config)
if err != nil {
return
}
return
}
// SetConfig write te config.yml file from the config parameter
func SetConfig(config *Config) (err error) {
configPath, err := getConfigPath()
if err != nil {
return
}
configYaml, err := yaml.Marshal(config)
if err != nil {
return
}
ioutil.WriteFile(configPath, configYaml, 0640)
if err != nil {
return
}
return
}