-
Notifications
You must be signed in to change notification settings - Fork 6
/
viper.go
47 lines (35 loc) · 914 Bytes
/
viper.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
package vaultcli
import (
"fmt"
"strings"
cst "github.com/DelineaXPM/dsv-cli/constants"
"github.com/spf13/viper"
)
const envVarPrefix = "thy"
func ViperInit() error {
viper.SetEnvPrefix(envVarPrefix)
envReplacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(envReplacer)
viper.AutomaticEnv()
cfgFile := viper.GetString(cst.Config)
cf, err := ReadConfigFile(cfgFile)
if err != nil {
return err
}
profile := viper.GetString(cst.Profile)
if profile == "" {
profile = cf.DefaultProfile
}
// Set profile name to lower case globally.
profile = strings.ToLower(profile)
viper.Set(cst.Profile, profile)
config, ok := cf.GetProfile(profile)
if !ok {
return fmt.Errorf("profile %q not found in configuration file %q", profile, cf.GetPath())
}
err = viper.MergeConfigMap(config.data)
if err != nil {
return fmt.Errorf("cannot initialize Viper: %w", err)
}
return nil
}