-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
78 lines (65 loc) · 1.92 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
package main
import (
"github.com/spf13/viper"
"log"
"os"
)
// make this config object available outside
var ConfigObj = KleanerConfig{}
// This map would need to be updated to account for any other behavior we would want to enable
// The keys in this map have to map directly to the DeleteFuncString in the SweeperConfigDetails struct
var funcMap = map[string]DeleteFunc{
"DeleteCrash": DeleteCrash,
"DeleteGeneric": DeleteGeneric,
}
/**
* This is the parent config object
*/
type KleanerConfig struct {
Reasons []SweeperConfigDetails `yaml:"reasons"`
}
/**
* This is the object that holds the necessary information
*/
type SweeperConfigDetails struct {
Reason string `yaml:"reason"`
RestartThreshold int `yaml:"restartThreshold,omitempty"`
DeleteFuncString string `yaml:"deleteFuncString"`
DeleteFunction DeleteFunc
}
func (dc *KleanerConfig) SetFunctions(fnMap map[string]DeleteFunc) {
for i, reason := range dc.Reasons {
// get the appropriate function out of the map
val, ok := fnMap[reason.DeleteFuncString]
if ok {
// add the appropriate function call to the referenced object
dc.Reasons[i].DeleteFunction = val
}
}
}
func init() {
// we'll read config in from YAML
viper.SetConfigType("yaml")
// The config file name is config.yaml
viper.SetConfigName("config")
// Just in case we want to set another directory via an environment variable
configDir := os.Getenv("GO_CONFIG_DIR")
if len(configDir) > 0 {
viper.AddConfigPath(configDir)
}
viper.AddConfigPath("./configs")
// read the config file into memory
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err.Error())
}
// unmarshal yaml file into ConfigObj member
err = viper.UnmarshalKey("kleaner", &ConfigObj)
viper.SetEnvPrefix("kleaner")
viper.AutomaticEnv()
if err != nil {
log.Fatal(err.Error())
}
// this is necessary to assign the appropriate functions to the right objects
ConfigObj.SetFunctions(funcMap)
}