generated from LucasCarioca/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
51 lines (43 loc) · 1.06 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
package config
import (
"fmt"
"github.com/spf13/viper"
"os"
"strings"
)
var config *viper.Viper
// Init initializes the configs and loads the specific values
func Init(env string) {
config = viper.New()
config.SetConfigName(fmt.Sprintf("config.%s", env))
config.SetConfigType("yaml")
config.AddConfigPath(".")
config.AddConfigPath("../../")
config.SetEnvPrefix("secret")
err := config.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
for _, k := range config.AllKeys() {
value := config.GetString(k)
if strings.HasPrefix(value, "${") && strings.HasSuffix(value, "}") {
config.Set(k, getEnvOrPanic(strings.TrimSuffix(strings.TrimPrefix(value, "${"), "}")))
}
}
}
// GetConfig returns the instance of the config values
func GetConfig() *viper.Viper {
return config
}
func getEnvOrPanic(raw string) string {
values := strings.Split(raw, "|")
env := values[0]
res := os.Getenv(env)
if len(res) == 0 {
if len(values) == 2 {
return values[1]
}
panic("Mandatory env variable not found:" + env)
}
return res
}