/
goconfig.go
155 lines (128 loc) · 3.53 KB
/
goconfig.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package goconfig
import (
"encoding/json"
"flag"
"fmt"
"os"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// GoConfig hold the configuration.
type GoConfig struct {
flagsBinded bool
}
// New creates a new Config.
func New() *GoConfig {
// always add the current working directory
viper.AddConfigPath(".")
gc := &GoConfig{}
// add default flags
gc.addFlag("show-config", false, "prints the configuration")
gc.addFlag("show-config-debug", false, "prints the configuration with resolution steps")
gc.addFlag("config-file", "", "configuration file to use")
return gc
}
// SetConfigName sets the configuration filename.
func (c *GoConfig) SetConfigName(filename string) {
viper.SetConfigName(filename)
}
// AddConfigurationPaths adds the paths where to find the configuration file.
func (c *GoConfig) AddConfigurationPaths(paths ...string) {
for _, p := range paths {
viper.AddConfigPath(p)
}
}
// Add adds a configuration key with default values and usage information.
func (c *GoConfig) Add(key string, defaultValue interface{}, usage string) {
// set defaults
viper.SetDefault(key, defaultValue)
// set flag
c.addFlag(key, defaultValue, usage)
}
// GetString gets the value as a string.
func (c *GoConfig) GetString(key string) string {
c.mustExists(key)
return viper.GetString(key)
}
// GetInt gets the value as a int.
func (c *GoConfig) GetInt(key string) int {
c.mustExists(key)
return viper.GetInt(key)
}
// GetInt64 gets the value as int64.
func (c *GoConfig) GetInt64(key string) int64 {
c.mustExists(key)
return viper.GetInt64(key)
}
// GetBool gets the value as bool.
func (c *GoConfig) GetBool(key string) bool {
c.mustExists(key)
return viper.GetBool(key)
}
// GetFloat64 gets the value as float64.
func (c *GoConfig) GetFloat64(key string) float64 {
c.mustExists(key)
return viper.GetFloat64(key)
}
func (c *GoConfig) mustExists(key string) {
if !viper.IsSet(key) {
panic(fmt.Errorf("key %s has not been added", key))
}
}
// PrintConfig prints the configuration.
func (c *GoConfig) PrintConfig() {
encoded, _ := json.MarshalIndent(viper.AllSettings(), "", " ")
fmt.Println(string(encoded))
}
// PrintDebugConfig prints configuration with resolution steps.
func (c *GoConfig) PrintDebugConfig() {
viper.Debug()
fmt.Printf("Merged Config: \n%#v\n", viper.AllSettings())
}
// BindFlags binds configuration elements to flags.
func (c *GoConfig) BindFlags() {
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
c.flagsBinded = true
}
// ReadConfig reads the configuration.
func (c *GoConfig) ReadConfig() {
if c.flagsBinded {
if viper.GetString("config-file") != "" {
viper.SetConfigFile(viper.GetString("config-file"))
}
}
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
c.executeActionFlagsIfAny()
}
func (c *GoConfig) executeActionFlagsIfAny() {
if viper.GetBool("show-config") {
c.PrintConfig()
os.Exit(1)
}
if viper.GetBool("show-config-debug") {
c.PrintDebugConfig()
os.Exit(1)
}
}
func (c *GoConfig) addFlag(key string, defaultValue interface{}, usage string) {
switch defaultValue.(type) {
case int:
flag.Int(key, defaultValue.(int), usage)
case int64:
flag.Int64(key, defaultValue.(int64), usage)
flag.Uint64(key, defaultValue.(uint64), usage)
case string:
flag.String(key, defaultValue.(string), usage)
case float64:
flag.Float64(key, defaultValue.(float64), usage)
case bool:
flag.Bool(key, defaultValue.(bool), usage)
default:
panic(fmt.Errorf("unknow type:%T for value:%#v", defaultValue, defaultValue))
}
}