diff --git a/config/config.go b/config/config.go index 69f0e503c4..4db94b4074 100644 --- a/config/config.go +++ b/config/config.go @@ -50,10 +50,17 @@ var format = logging.MustStringFormatter( `%{color}%{time:15:04:05.000} [%{module}] %{level:.4s} : %{color:reset} %{message}`, ) +const cmdRoot = "fabric_sdk" + // InitConfig ... // initConfig reads in config file func InitConfig(configFile string) error { + myViper.SetEnvPrefix(cmdRoot) + myViper.AutomaticEnv() + replacer := strings.NewReplacer(".", "_") + myViper.SetEnvKeyReplacer(replacer) + if configFile != "" { // create new viper myViper.SetConfigFile(configFile) diff --git a/config/config_test.go b/config/config_test.go index 9937a4a3bd..dd23f1da79 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -78,6 +78,30 @@ func TestMultipleVipers(t *testing.T) { } } +func TestEnvironmentVariables(t *testing.T) { + testValue := myViper.GetString("env.test") + if testValue != "" { + t.Fatalf("Expected environment variable value to be empty but got: %s", testValue) + } + + err := os.Setenv("FABRIC_SDK_ENV_TEST", "123") + defer os.Unsetenv("FABRIC_SDK_ENV_TEST") + + if err != nil { + fmt.Println(err.Error()) + } + + err = InitConfig("../test/fixtures/config/config_test.yaml") + if err != nil { + fmt.Println(err.Error()) + } + + testValue = myViper.GetString("env.test") + if testValue != "123" { + t.Fatalf("Expected environment variable value but got: %s", testValue) + } +} + func TestMain(m *testing.M) { err := InitConfig("../test/fixtures/config/config_test.yaml") if err != nil {