-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
103 lines (81 loc) · 2.33 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
103
package CIScriptsHelpers
import (
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/spf13/viper"
)
func init() {
var configFile string
var ok bool
if configFile, ok = os.LookupEnv("CI_SCRIPTS_CONFIG"); !ok {
configFile = "./.ci_scripts"
}
// remove file extension
configFile = strings.TrimSuffix(configFile, filepath.Ext(configFile))
viper.SetConfigName(path.Base(configFile)) // no need to include file extension
viper.AddConfigPath(path.Dir(configFile)) // set the path of your config file
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
LogWarning("No configuration file loaded\n")
}
}
// ConfigCheck sets environment variable key to value if it is not already set. Used to set default env values
func ConfigSetDefault(key, value string) {
viper.SetDefault(key, value)
}
// RequiredEnv ensures an environment variable is set. If it is not set, it will exit with status code 1
func RequiredConfigKey(key string) {
if s := viper.GetString(key); s == "" {
PrettyExit(1, "Required environment variable %s not set", key)
}
}
func ConfigFetchWithDefault(key, defaultValue string) (value string) {
val, _ := ConfigFetch(key, defaultValue)
return val
}
// EnvFetch fetchs an environment variables. Sets the value to the default value if it does not exist.
func ConfigFetch(key string, defaultValue ...string) (value string, ok bool) {
value = viper.GetString(key)
ok = (value != "")
if !ok {
if len(defaultValue) > 0 {
value = defaultValue[0]
ok = true
}
}
return value, ok
}
func RequiredConfigFetch(key string) (value string) {
if s, ok := ConfigFetch(key); !ok {
PrettyExit(1, "Required configuration variable %s not set", key)
} else {
return s
}
return ""
}
func ParseCLIArguments() {
flagParser, _ := regexp.Compile("--?([^=]+)=?(.+)?")
flagName := ""
for _, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "-") {
match := flagParser.FindStringSubmatch(arg)
if len(match) < 1 {
LogError("Failed to parse flag with regex --?([^=]+)=?(.+): ", arg)
}
flagName = strings.Replace(match[1], "-", ".", -1)
if len(match) == 3 && match[2] != "" {
viper.Set(flagName, match[2])
flagName = ""
}
} else if flagName != "" {
viper.Set(flagName, arg)
flagName = ""
}
}
}