This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
145 lines (120 loc) · 3.12 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
package config
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"strings"
"time"
)
/*
Creation Time: 2020 - Nov - 11
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
var (
_Viper *viper.Viper
)
func MustInit(configFileName string, configSearchPaths ...string) {
err := Init(configFileName, configSearchPaths...)
if err != nil {
panic(err)
}
}
func Init(configFileName string, configSearchPaths ...string) error {
_Viper = viper.NewWithOptions(
viper.EnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")),
)
for _, p := range configSearchPaths {
_Viper.AddConfigPath(p)
}
if configFileName == "" {
configFileName = "config"
}
_Viper.SetConfigName(configFileName)
return _Viper.ReadInConfig()
}
// SetEnvPrefix defines a prefix that ENVIRONMENT variables will use.
// // E.g. if your prefix is "spf", the env registry will look for env
// // variables that start with "SPF_".
func SetEnvPrefix(prefix string) {
_Viper.SetEnvPrefix(prefix)
_Viper.AutomaticEnv()
}
// BindCmdFlags binds a full flag set to the configuration, using each flag's long
// name as the config key.
func BindCmdFlags(cmd *cobra.Command) error {
return _Viper.BindPFlags(cmd.Flags())
}
// SetCmdFlags sets the flags defined by FlagOption to the command 'cmd'.
func SetCmdFlags(cmd *cobra.Command, opts ...FlagOption) {
for _, fo := range opts {
fo(cmd.Flags())
}
}
// SetCmdPersistentFlags sets the persistent flags defined by FlagOption to the command 'cmd'.
func SetCmdPersistentFlags(cmd *cobra.Command, opts ...FlagOption) {
for _, fo := range opts {
fo(cmd.PersistentFlags())
}
}
// Set sets the value for the key in the override register.
// Set is case-insensitive for a key.
// Will be used instead of values obtained via
// flags, config file, ENV, default, or key/value store.
func Set(key string, val interface{}) {
_Viper.Set(key, val)
}
func GetBool(key string) bool {
return _Viper.GetBool(key)
}
func GetString(key string) string {
return _Viper.GetString(key)
}
func GetInt64(key string) int64 {
return _Viper.GetInt64(key)
}
func GetUint64(key string) uint64 {
return _Viper.GetUint64(key)
}
func GetInt32(key string) int32 {
return _Viper.GetInt32(key)
}
func GetUint32(key string) uint32 {
return _Viper.GetUint32(key)
}
func GetInt(key string) int {
return _Viper.GetInt(key)
}
func GetUint(key string) uint {
return _Viper.GetUint(key)
}
func GetIntSlice(key string) []int {
return _Viper.GetIntSlice(key)
}
func GetInt32Slice(key string) []int32 {
ints := _Viper.GetIntSlice(key)
int32s := make([]int32, 0, len(ints))
for _, i := range ints {
int32s = append(int32s, int32(i))
}
return int32s
}
func GetInt64Slice(key string) []int64 {
ints := _Viper.GetIntSlice(key)
int64s := make([]int64, 0, len(ints))
for _, i := range ints {
int64s = append(int64s, int64(i))
}
return int64s
}
func GetStringSlice(key string) []string {
return _Viper.GetStringSlice(key)
}
func GetDuration(key string) time.Duration {
return _Viper.GetDuration(key)
}
func GetTime(key string) time.Time {
return _Viper.GetTime(key)
}