6
6
"os"
7
7
"strings"
8
8
9
+ defaults "github.com/mcuadros/go-defaults"
9
10
"github.com/spf13/viper"
10
11
11
12
"github.com/ovh/cds/engine/api/secret"
@@ -14,6 +15,10 @@ import (
14
15
15
16
// config reads in config file and ENV variables if set.
16
17
func config () {
18
+ for k := range AsEnvVariables (conf , "" ) {
19
+ viper .BindEnv (strings .ToLower (strings .Replace (k , "_" , "." , - 1 )), "CDS_" + k )
20
+ }
21
+
17
22
if cfgFile != "" {
18
23
//If the config file doesn't exists, let's exit
19
24
if _ , err := os .Stat (cfgFile ); os .IsNotExist (err ) {
@@ -26,9 +31,6 @@ func config() {
26
31
sdk .Exit (err .Error ())
27
32
}
28
33
29
- if err := viper .Unmarshal (conf ); err != nil {
30
- sdk .Exit (err .Error ())
31
- }
32
34
} else if remoteCfg != "" {
33
35
fmt .Println ("Reading configuration from consul @" , remoteCfg )
34
36
viper .AddRemoteProvider ("consul" , remoteCfg , remoteCfgKey )
@@ -56,14 +58,10 @@ func config() {
56
58
if err := viper .ReadConfig (cfgBuffer ); err != nil {
57
59
sdk .Exit ("Unable to read config: %v" , err .Error ())
58
60
}
59
-
60
- // Unmarshal in the conf
61
- if err := viper .Unmarshal (conf ); err != nil {
62
- sdk .Exit ("Unable to parse config: %v" , err .Error ())
63
- }
64
61
}
65
62
66
- viper .AutomaticEnv () // read in environment variables that match
67
- viper .SetEnvPrefix ("cds" )
68
- viper .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" , "-" , "_" )) // Replace "." and "-" by "_" for env variable lookup
63
+ if err := viper .Unmarshal (conf ); err != nil {
64
+ sdk .Exit ("Unable to parse config: %v" , err .Error ())
65
+ }
66
+ defaults .SetDefaults (conf )
69
67
}
0 commit comments