-
Notifications
You must be signed in to change notification settings - Fork 1
/
yaml.go
115 lines (96 loc) · 2.94 KB
/
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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
package util
import (
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
// YamlConfig represents the koinos yaml application config values
type YamlConfig struct {
Global map[string]interface{} `yaml:"global,omitempty"`
P2P map[string]interface{} `yaml:"p2p,omitempty"`
BlockStore map[string]interface{} `yaml:"block_store,omitempty"`
JSONRPC map[string]interface{} `yaml:"jsonrpc,omitempty"`
TransactionStore map[string]interface{} `yaml:"transaction_store,omitempty"`
ContractMetaStore map[string]interface{} `yaml:"contract_meta_store,omitempty"`
}
// GetStringOption fetches a string cli value, respecting values in a given config
func GetStringOption(key string, defaultValue string, cliArg string, configs ...map[string]interface{}) string {
if cliArg != "" {
return cliArg
}
for _, config := range configs {
if v, ok := config[key]; ok {
if option, ok := v.(string); ok {
return option
}
}
}
return defaultValue
}
// GetStringSliceOption fetches a string slicecli value, respecting values in a given config
func GetStringSliceOption(key string, cliArg []string, configs ...map[string]interface{}) []string {
stringSlice := cliArg
for _, config := range configs {
if v, ok := config[key]; ok {
if slice, ok := v.([]interface{}); ok {
for _, option := range slice {
if str, ok := option.(string); ok {
stringSlice = append(stringSlice, str)
}
}
}
}
}
return stringSlice
}
// GetBoolOption fetches a bool cli value, respecting values in a given config
func GetBoolOption(key string, defaultValue bool, cliArg bool, configs ...map[string]interface{}) bool {
if cliArg != defaultValue {
return cliArg
}
for _, config := range configs {
if v, ok := config[key]; ok {
if option, ok := v.(bool); ok && option != defaultValue {
return option
}
}
}
return defaultValue
}
// GetIntOption fetches an int value, respecting values in a given config
func GetIntOption(key string, defaultValue int, cliArg int, configs ...map[string]interface{}) int {
if cliArg != defaultValue {
return cliArg
}
for _, config := range configs {
if v, ok := config[key]; ok {
if option, ok := v.(int); ok && option != defaultValue {
return option
}
}
}
return defaultValue
}
// InitYamlConfig initializes a yaml config
func InitYamlConfig(baseDir string) *YamlConfig {
yamlConfigPath := filepath.Join(baseDir, "config.yml")
if _, err := os.Stat(yamlConfigPath); os.IsNotExist(err) {
yamlConfigPath = filepath.Join(baseDir, "config.yaml")
}
yamlConfig := YamlConfig{}
if _, err := os.Stat(yamlConfigPath); err == nil {
data, err := os.ReadFile(yamlConfigPath)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(data, &yamlConfig)
if err != nil {
panic(err)
}
} else {
yamlConfig.Global = make(map[string]interface{})
yamlConfig.P2P = make(map[string]interface{})
yamlConfig.BlockStore = make(map[string]interface{})
}
return &yamlConfig
}