Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit b6f4099

Browse files
committed
[FAB-3558] Allow specific env variable prefix for SDK
Change-Id: I2c7c76233c2069ce1d40772968b613bfd76b9cb0 Signed-off-by: Emir Heidinger <emir.heidinger@securekey.com>
1 parent 4e7806a commit b6f4099

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

config/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ const cmdRoot = "fabric_sdk"
5757
// InitConfig ...
5858
// initConfig reads in config file
5959
func InitConfig(configFile string) error {
60+
return InitConfigWithCmdRoot(configFile, cmdRoot)
61+
}
6062

61-
myViper.SetEnvPrefix(cmdRoot)
63+
// InitConfigWithCmdRoot reads in a config file and allows the
64+
// environment variable prefixed to be specified
65+
func InitConfigWithCmdRoot(configFile string, cmdRootPrefix string) error {
66+
myViper.SetEnvPrefix(cmdRootPrefix)
6267
myViper.AutomaticEnv()
6368
replacer := strings.NewReplacer(".", "_")
6469
myViper.SetEnvKeyReplacer(replacer)

config/config_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestMultipleVipers(t *testing.T) {
8181
}
8282
}
8383

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

108+
func TestEnvironmentVariablesSpecificCmdRoot(t *testing.T) {
109+
testValue := myViper.GetString("env.test")
110+
if testValue != "" {
111+
t.Fatalf("Expected environment variable value to be empty but got: %s", testValue)
112+
}
113+
114+
err := os.Setenv("TEST_ROOT_ENV_TEST", "456")
115+
defer os.Unsetenv("TEST_ROOT_ENV_TEST")
116+
117+
if err != nil {
118+
fmt.Println(err.Error())
119+
}
120+
121+
err = InitConfigWithCmdRoot("../test/fixtures/config/config_test.yaml", "test_root")
122+
if err != nil {
123+
fmt.Println(err.Error())
124+
}
125+
126+
testValue = myViper.GetString("env.test")
127+
if testValue != "456" {
128+
t.Fatalf("Expected environment variable value but got: %s", testValue)
129+
}
130+
}
131+
108132
func TestMain(m *testing.M) {
109133
err := InitConfig("../test/fixtures/config/config_test.yaml")
110134
if err != nil {

0 commit comments

Comments
 (0)