Skip to content

Commit

Permalink
support env can include underscore, if you set db_password, you can c…
Browse files Browse the repository at this point in the history
…all archaius.Get("db.password") to get it (#74)
  • Loading branch information
tianxiaoliang authored Jun 13, 2019
1 parent b45d50f commit 4d2017a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions examples/env/f1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cred:
db:
user: admin
pwd:
20 changes: 20 additions & 0 deletions examples/env/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github.com/go-chassis/go-archaius"
"log"
"os"
)

func main() {
os.Setenv("cred_db_user", "root")
os.Setenv("cred_db_pwd", "root")
err := archaius.Init(archaius.WithRequiredFiles([]string{"f1.yaml"}),
archaius.WithENVSource())
if err != nil {
panic(err)
}
log.Println(archaius.Get("cred.db.user"))
log.Println(archaius.Get("cred.db.pwd"))

}
8 changes: 7 additions & 1 deletion sources/enviromentvariable-source/envconfigurationsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var _ core.ConfigSource = &EnvConfigurationSource{}

//NewEnvConfigurationSource configures a new environment configuration
func NewEnvConfigurationSource() core.ConfigSource {
openlogging.Info("enable env source")
envConfigSource := new(EnvConfigurationSource)
envConfigSource.priority = envVariableSourcePriority
config, err := envConfigSource.pullConfigurations()
Expand All @@ -61,7 +62,12 @@ func (*EnvConfigurationSource) pullConfigurations() (map[string]interface{}, err
for _, value := range os.Environ() {
rs := []rune(value)
in := strings.Index(value, "=")
configMap[string(rs[0:in])] = string(rs[in+1:])
key := string(rs[0:in])
value := string(rs[in+1:])
envKey := strings.Replace(key, "_", ".", -1)
configMap[key] = value
configMap[envKey] = value

}

return configMap, nil
Expand Down
13 changes: 12 additions & 1 deletion sources/enviromentvariable-source/envconfigurationsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package envconfigsource

import (
"github.com/go-chassis/go-archaius/core"
"gopkg.in/go-playground/assert.v1"
"os"
"testing"
)
Expand All @@ -34,13 +35,23 @@ func populatEnvConfiguration() {
os.Setenv("testenvkey1", "envkey1")
os.Setenv("testenvkey2", "envkey2")
os.Setenv("testenvkey3", "a=b=c")

}

func TestEnvConfigurationSource(t *testing.T) {

populatEnvConfiguration()
envsource := NewEnvConfigurationSource()

t.Run("set env with underscore, use dot to get ", func(t *testing.T) {
os.Setenv("a_b_c_d", "asd")
envsource := NewEnvConfigurationSource()
v, err := envsource.GetConfigurationByKey("a.b.c.d")
assert.Equal(t, nil, err)
assert.Equal(t, "asd", v)
v, err = envsource.GetConfigurationByKey("a_b_c_d")
assert.Equal(t, nil, err)
assert.Equal(t, "asd", v)
})
t.Log("Test envconfigurationsource.go")

t.Log("verifying envsource configurations by GetConfigurations method")
Expand Down

0 comments on commit 4d2017a

Please sign in to comment.