Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement expand value env #115

Merged
merged 7 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ some:
ttl: 30s
service:
name: ${NAME||go-archaius}
addr: ${IP||127.0.0.1}:${PORT||80}
```
after adding file
```go
Expand All @@ -96,10 +97,13 @@ you can get value
ttl := archaius.GetString("ttl", "60s")
i := archaius.GetInt("some.config", "")
serviceName := archaius.GetString("service.name", "")
serviceAddr := archaius.GetString("service.addr", "")
```
note:

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.
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` .

if you want to read some.config from env
you can run
Expand Down
57 changes: 30 additions & 27 deletions source/util/expand.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
package util

import "os"
import (
"os"
"regexp"
"strings"
)

// 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}(.*?)}`

// reg exp
var variableReg *regexp.Regexp

func init() {
variableReg = regexp.MustCompile(envVariable)
}

// if string like ${NAME||archaius}
// will query environment variable for ${NAME}
// if environment variable is "" return default string `archaius`
// support multi variable, eg:
// value string => addr:${IP||127.0.0.1}:${PORT||8080}
// if environment variable => IP=0.0.0.0 PORT=443 , result => addr:0.0.0.0:443
// if no exist environment variable , result => addr:127.0.0.1:8080
func ExpandValueEnv(value string) (realValue string) {
realValue = value

vLen := len(value)
// 3 = ${}
if vLen < 3 {
return
}
// Need start with "${" and end with "}", then return.
if value[0] != '$' || value[1] != '{' || value[vLen-1] != '}' {
return
value = strings.TrimSpace(value)
submatch := variableReg.FindAllStringSubmatch(value, -1)
if len(submatch) == 0 {
return value
}

key := ""
defaultV := ""
// value start with "${"
for i := 2; i < vLen; i++ {
if value[i] == '|' && (i+1 < vLen && value[i+1] == '|') {
key = value[2:i]
defaultV = value[i+2 : vLen-1] // other string is default value.
break
} else if value[i] == '}' {
key = value[2:i]
break
realValue = value
for _, sub := range submatch {
item := os.Getenv(sub[1])
if item == "" {
item = sub[2]
}
}

realValue = os.Getenv(key)
if realValue == "" {
realValue = defaultV
realValue = strings.ReplaceAll(realValue, sub[0], item)
}

return
Expand Down
44 changes: 40 additions & 4 deletions source/util/expand_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
package util

import (
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func TestExpandValueEnv(t *testing.T) {
str := "${NAME||archaius}"
if e := os.Setenv("NAM", "go-archaius"); e != nil {
str1 := "${NAME||archaius}=${IP||}:${PORT||8080}+++${0PORT||8080}+++${_PORT||8080}"
assert.Equal(t, "archaius=:8080+++${0PORT||8080}+++8080", ExpandValueEnv(str1))

str2 := "${IP||}:${PORT||8080}:${||8080}"
assert.Equal(t, ":8080:${||8080}", ExpandValueEnv(str2))

str3 := "${IP|}:${PORT||8080}"
assert.Equal(t, "${IP|}:8080", ExpandValueEnv(str3))

str4 := " $${IP_09090||} "
assert.Equal(t, "$", ExpandValueEnv(str4))

str5 := "${IP||}:8080${ADD R||:8080}"
assert.Equal(t, ":8080${ADD R||:8080}", ExpandValueEnv(str5))

str6 := "${IP||0.0.0.0}:8080"
assert.Equal(t, "0.0.0.0:8080", ExpandValueEnv(str6))

str7 := "${NAME||archaius}:${IP||github.com}"
if e := os.Setenv("NAME", "go-archaius"); e != nil {
t.Logf("err:%+v", e)
}
assert.Equal(t, "go-archaius:github.com", ExpandValueEnv(str7))

str8 := "addr:${IP||127.0.0.1}:${PORT||8080}"
if e := os.Setenv("IP", "0.0.0.0"); e != nil {
t.Logf("err:%+v", e)
}
assert.Equal(t, "addr:0.0.0.0:8080", ExpandValueEnv(str8))

str9 := "${POD_IP||}"
if e := os.Setenv("POD_IP", "0.0.0.0"); e != nil {
t.Logf("err:%+v", e)
}
assert.Equal(t, "0.0.0.0", ExpandValueEnv(str9))

str10 := "${IP|}"
if e := os.Setenv("IP", "0.0.0.0"); e != nil {
t.Logf("err:%+v", e)
}
realStr := ExpandValueEnv(str)
t.Logf("realStr: %s", realStr)
assert.Equal(t, "${IP|}", ExpandValueEnv(str10))
}