-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_yaml.go
62 lines (50 loc) · 1.21 KB
/
config_yaml.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 (
"fmt"
"io/ioutil"
yaml "gopkg.in/yaml.v2"
)
const yamlFileName = "./config.yaml"
type yamlConfig struct {
config *config
}
func newYamlConfig() *yamlConfig {
var c yamlConfig
c.load()
return &c
}
func (c *yamlConfig) load() {
c.config = &config{}
c.loadConfig()
}
func (c *yamlConfig) getConfigAccounts() *[]configAccount {
return &c.config.Accounts
}
// This function is now unused, place it back if you need to create an example config file
// func (c *yamlConfig) createExampleConfig() {
// conf := newConfig(true)
// a := []configAccount{conf.config.Accounts[0], conf.config.Accounts[0]}
// y := &config{Accounts: a}
// file, err := os.Create(yamlFileName)
// if err != nil {
// fmt.Println("Error creating file:", err)
// return
// }
// defer file.Close()
// encoder := yaml.NewEncoder(file)
// if err := encoder.Encode(y); err != nil {
// fmt.Println("Error encoding to YAML:", err)
// return
// }
// }
func (c *yamlConfig) loadConfig() {
file, err := ioutil.ReadFile(yamlFileName)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
if err := yaml.Unmarshal(file, c.config); err != nil {
fmt.Println("Error unmarshaling YAML:", err)
return
}
}