Skip to content

Commit

Permalink
Env expansion for file handler supports upper and lower case in bash4…
Browse files Browse the repository at this point in the history
… format (#144)

* finish dev and test

* add a length condition for rule matching result, and append a note to the env syntax in README

Co-authored-by: Donghong Huang <[email protected]>
  • Loading branch information
EastMacro2020 and Donghong Huang authored Apr 14, 2021
1 parent cb90da8 commit 4cf1d9c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ serviceAddr := archaius.GetString("service.addr", "")
```
note:

1. For `service.name` config with value of `${NAME||go-archaius}` is support env syntax. If environment variable `${NAME}` isn't setting, return default value `go-archaius`. It's setted, will get real environment variable value.
1. For `service.name` config with value of `${NAME||go-archaius}` is support env syntax. If environment variable `${NAME}` isn't setting, return default value `go-archaius`. It's setted, will get real environment variable value. Besides, if `${Name^^}` is used instead of `${Name}`, the value of environment variable `Name` will be shown in upper case, and `${Name,,}` will bring the value in lower case.
2. For `service.addr` config is support "expand syntax". If environment variable `${IP}` or `${PORT}` is setted, will get env config.
eg: `export IP=0.0.0.0 PORT=443` , `archaius.GetString("service.addr", "")` will return `0.0.0.0:443` .

Expand Down
13 changes: 11 additions & 2 deletions source/util/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or
// the underscore character ( _), and can't begin with number.
const envVariable = `\${([a-zA-Z_]{1}[\w]+)[\|]{2}(.*?)}`
const envVariable = `\${([a-zA-Z_]{1}[\w]+)((?:\,\,|\^\^)?)[\|]{2}(.*?)}`

// reg exp
var variableReg *regexp.Regexp
Expand All @@ -33,9 +33,18 @@ func ExpandValueEnv(value string) (realValue string) {

realValue = value
for _, sub := range submatch {
if len(sub) != 4 { //rule matching behaves in an unexpected way
continue
}
item := os.Getenv(sub[1])
if item == "" {
item = sub[2]
item = sub[3]
} else {
if sub[2] == "^^" {
item = strings.ToUpper(item)
} else if sub[2] == ",," {
item = strings.ToLower(item)
}
}
realValue = strings.ReplaceAll(realValue, sub[0], item)
}
Expand Down
9 changes: 9 additions & 0 deletions source/util/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ func TestExpandValueEnv(t *testing.T) {
t.Logf("err:%+v", e)
}
assert.Equal(t, "${IP|}", ExpandValueEnv(str10))

os.Unsetenv("UPPER_ENV")
str11 := "env:${UPPER_ENV^^||local}"
assert.Equal(t, "env:local", ExpandValueEnv(str11))
os.Setenv("UPPER_ENV", "Test")
assert.Equal(t, "env:TEST", ExpandValueEnv(str11))
str12 := "env:${UPPER_ENV,,||local}"
assert.Equal(t, "env:test", ExpandValueEnv(str12))
os.Unsetenv("UPPER_ENV")
}

0 comments on commit 4cf1d9c

Please sign in to comment.