Skip to content

Commit 4cf1d9c

Browse files
EastMacro2020Donghong Huang
andauthored
Env expansion for file handler supports upper and lower case in bash4 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]>
1 parent cb90da8 commit 4cf1d9c

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ serviceAddr := archaius.GetString("service.addr", "")
104104
```
105105
note:
106106

107-
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.
107+
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.
108108
2. For `service.addr` config is support "expand syntax". If environment variable `${IP}` or `${PORT}` is setted, will get env config.
109109
eg: `export IP=0.0.0.0 PORT=443` , `archaius.GetString("service.addr", "")` will return `0.0.0.0:443` .
110110

source/util/expand.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

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

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

3434
realValue = value
3535
for _, sub := range submatch {
36+
if len(sub) != 4 { //rule matching behaves in an unexpected way
37+
continue
38+
}
3639
item := os.Getenv(sub[1])
3740
if item == "" {
38-
item = sub[2]
41+
item = sub[3]
42+
} else {
43+
if sub[2] == "^^" {
44+
item = strings.ToUpper(item)
45+
} else if sub[2] == ",," {
46+
item = strings.ToLower(item)
47+
}
3948
}
4049
realValue = strings.ReplaceAll(realValue, sub[0], item)
4150
}

source/util/expand_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,13 @@ func TestExpandValueEnv(t *testing.T) {
4949
t.Logf("err:%+v", e)
5050
}
5151
assert.Equal(t, "${IP|}", ExpandValueEnv(str10))
52+
53+
os.Unsetenv("UPPER_ENV")
54+
str11 := "env:${UPPER_ENV^^||local}"
55+
assert.Equal(t, "env:local", ExpandValueEnv(str11))
56+
os.Setenv("UPPER_ENV", "Test")
57+
assert.Equal(t, "env:TEST", ExpandValueEnv(str11))
58+
str12 := "env:${UPPER_ENV,,||local}"
59+
assert.Equal(t, "env:test", ExpandValueEnv(str12))
60+
os.Unsetenv("UPPER_ENV")
5261
}

0 commit comments

Comments
 (0)