-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
165 lines (131 loc) · 3.8 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
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
156
157
158
159
160
161
162
163
164
165
package conf
import (
"fmt"
"github.com/flosch/pongo2"
"github.com/lafrinte/nops/fs"
"github.com/lafrinte/nops/str"
"github.com/spf13/viper"
"regexp"
"time"
)
type Config struct {
viper *viper.Viper
Template string
WriteTo string
DefaultVal interface{}
}
/*
Write is used in cli for generate conf file with conf template which may contains description message for
each section and option.
*/
func (c *Config) Write() error {
tpl, err := pongo2.FromString(c.Template)
if err != nil {
return fmt.Errorf("read template failed: err -> %s", err)
}
val := pongo2.Context{}
if c.DefaultVal != nil {
for k, v := range c.DefaultVal.(map[string]interface{}) {
val[k] = v
}
}
s, err := tpl.Execute(val)
if err != nil {
return fmt.Errorf("parsing conf failed: err -> %s", err)
}
// replace string
// 1. prevent indent error: turn tab to 4 blank space
// use lower case for bool type object parsing by pongo2. default True/False -> true/false
s = str.Replaces(
s,
str.ReplacePoint{Old: "\t", New: " ", N: -1},
str.ReplacePoint{Old: ": True", New: ": true", N: -1},
str.ReplacePoint{Old: ": False", New: ": false", N: -1},
)
// '\n \n \n' -> \n
s = regexp.MustCompile("(?:\\n\\s*){2,}\\n").ReplaceAllString(s, "\n")
// '\n \n' -> '\n\n'
s = regexp.MustCompile("\\n\\s+\\n").ReplaceAllString(s, "\n\n")
// '\n\n -' -> '\n -'
s = regexp.MustCompile("\\n\\n(\\s+-)").ReplaceAllString(s, "\n${1}")
if err := fs.WriteFile(c.WriteTo, s, 0666); err != nil {
return fmt.Errorf("write into conf failed: err -> %s", err)
}
return nil
}
/*
Read is used to parsing conf. when conf does not exist, func will try to write into conf file
from template parsing with default value.
*/
func (c *Config) Read() error {
if err := c.viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
return fmt.Errorf("no conf file, and write default template conf failed: err -> %s", err)
}
// parsing error will return directly
return fmt.Errorf("reading failed: err -> %s", err)
}
return nil
}
func (c *Config) Get(key string) interface{} {
return c.viper.Get(key)
}
func (c *Config) GetString(key string) string {
return c.viper.GetString(key)
}
func (c *Config) GetBool(key string) bool {
return c.viper.GetBool(key)
}
func (c *Config) GetInt(key string) int {
return c.viper.GetInt(key)
}
func (c *Config) GetInt32(key string) int32 {
return c.viper.GetInt32(key)
}
func (c *Config) GetInt64(key string) int64 {
return c.viper.GetInt64(key)
}
func (c *Config) GetUint(key string) uint {
return c.viper.GetUint(key)
}
func (c *Config) GetUint16(key string) uint16 {
return c.viper.GetUint16(key)
}
func (c *Config) GetUint32(key string) uint32 {
return c.viper.GetUint32(key)
}
func (c *Config) GetUint64(key string) uint64 {
return c.viper.GetUint64(key)
}
func (c *Config) GetFloat64(key string) float64 {
return c.viper.GetFloat64(key)
}
func (c *Config) GetTime(key string) time.Time {
return c.viper.GetTime(key)
}
func (c *Config) GetDuration(key string) time.Duration {
return c.viper.GetDuration(key)
}
func (c *Config) GetIntSlice(key string) []int {
return c.viper.GetIntSlice(key)
}
func (c *Config) GetStringSlice(key string) []string {
return c.viper.GetStringSlice(key)
}
func (c *Config) GetStringMap(key string) map[string]interface{} {
return c.viper.GetStringMap(key)
}
func (c *Config) GetStringMapString(key string) map[string]string {
return c.viper.GetStringMapString(key)
}
func (c *Config) GetStringMapStringSlice(key string) map[string][]string {
return c.viper.GetStringMapStringSlice(key)
}
func (c *Config) GetMapSlice(key string) []map[string]interface{} {
var out []map[string]interface{}
dt := c.viper.Get(key).([]interface{})
for _, v := range dt {
out = append(out, v.(map[string]interface{}))
}
return out
}