Skip to content

Commit

Permalink
[FAB-3409] Add support for env variables in config
Browse files Browse the repository at this point in the history
Change-Id: I7ee6e0ad31eb2f98e0d8997df995bbc0117421c7
Signed-off-by: Emir Heidinger <emir.heidinger@securekey.com>
  • Loading branch information
emirsh committed Apr 26, 2017
1 parent 0aacd1f commit 5d2c9c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/config.go
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions config/config_test.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 5d2c9c8

Please sign in to comment.