-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
102 lines (88 loc) · 2.08 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
100
101
102
package main
import (
"errors"
"io/ioutil"
"log"
"gopkg.in/yaml.v2"
)
// Config stores all the config options for sysminerd
type Config struct {
Interval float64
Hostname string
ConfigPath string `yaml:"config_path"`
}
type ModuleConfig struct {
Name string
Enabled bool
Settings map[string]interface{}
}
func parseConfig(path string) Config {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Error reading config: %v", err)
}
yamlConfig := Config{}
err = yaml.Unmarshal(data, &yamlConfig)
if err != nil {
log.Fatalf("Error parsing yaml: %v", err)
}
log.Printf("Config: %v", yamlConfig)
return yamlConfig
}
func parseModuleConfig(path string) ModuleConfig {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Error reading config: %v", err)
}
moduleConfig := ModuleConfig{}
err = yaml.Unmarshal(data, &moduleConfig)
if err != nil {
log.Fatalf("Error parsing yaml: %v", err)
}
return moduleConfig
}
func (config *ModuleConfig) SettingsString(key string) (string, error) {
value, ok := config.Settings[key]
if !ok {
return "", errors.New("Key does not exist")
}
svalue, ok := value.(string)
if !ok {
return "", errors.New("value is not a string")
}
return svalue, nil
}
func (config *ModuleConfig) SettingsInt(key string) (int, error) {
value, ok := config.Settings[key]
if !ok {
return 0, errors.New("Key does not exist")
}
ivalue, ok := value.(int)
if !ok {
return 0, errors.New("value is not an int")
}
return ivalue, nil
}
func (config *ModuleConfig) SettingsArray(key string) ([]interface{}, error) {
value, ok := config.Settings[key]
if !ok {
return nil, errors.New("Key does not exist")
}
avalue, ok := value.([]interface{})
if !ok {
return nil, errors.New("value is not an array")
}
return avalue, nil
}
func (config *ModuleConfig) SettingsStringArray(key string) ([]string, error) {
avalue, err := config.SettingsArray(key)
if err != nil {
return nil, err
}
values := make([]string, 0, len(avalue))
for _, v := range avalue {
sv, _ := v.(string)
values = append(values, sv)
}
return values, nil
}