This repository was archived by the owner on Apr 25, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -57,8 +57,13 @@ const cmdRoot = "fabric_sdk"
5757// InitConfig ...
5858// initConfig reads in config file
5959func 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 )
Original file line number Diff line number Diff 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+
108132func TestMain (m * testing.M ) {
109133 err := InitConfig ("../test/fixtures/config/config_test.yaml" )
110134 if err != nil {
You can’t perform that action at this time.
0 commit comments