Skip to content

Commit

Permalink
[FAB-3558] Allow specific env variable prefix for SDK
Browse files Browse the repository at this point in the history
Change-Id: I2c7c76233c2069ce1d40772968b613bfd76b9cb0
Signed-off-by: Emir Heidinger <emir.heidinger@securekey.com>
  • Loading branch information
emirsh committed May 2, 2017
1 parent 4e7806a commit b6f4099
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
7 changes: 6 additions & 1 deletion config/config.go
Expand Up @@ -57,8 +57,13 @@ const cmdRoot = "fabric_sdk"
// InitConfig ...
// initConfig reads in config file
func InitConfig(configFile string) error {
return InitConfigWithCmdRoot(configFile, cmdRoot)
}

myViper.SetEnvPrefix(cmdRoot)
// InitConfigWithCmdRoot reads in a config file and allows the
// environment variable prefixed to be specified
func InitConfigWithCmdRoot(configFile string, cmdRootPrefix string) error {
myViper.SetEnvPrefix(cmdRootPrefix)
myViper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
myViper.SetEnvKeyReplacer(replacer)
Expand Down
26 changes: 25 additions & 1 deletion config/config_test.go
Expand Up @@ -81,7 +81,7 @@ func TestMultipleVipers(t *testing.T) {
}
}

func TestEnvironmentVariables(t *testing.T) {
func TestEnvironmentVariablesDefaultCmdRoot(t *testing.T) {
testValue := myViper.GetString("env.test")
if testValue != "" {
t.Fatalf("Expected environment variable value to be empty but got: %s", testValue)
Expand All @@ -105,6 +105,30 @@ func TestEnvironmentVariables(t *testing.T) {
}
}

func TestEnvironmentVariablesSpecificCmdRoot(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("TEST_ROOT_ENV_TEST", "456")
defer os.Unsetenv("TEST_ROOT_ENV_TEST")

if err != nil {
fmt.Println(err.Error())
}

err = InitConfigWithCmdRoot("../test/fixtures/config/config_test.yaml", "test_root")
if err != nil {
fmt.Println(err.Error())
}

testValue = myViper.GetString("env.test")
if testValue != "456" {
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 b6f4099

Please sign in to comment.