2015-09-26 22:37:48 +00:00
|
|
|
package args
|
|
|
|
|
2015-11-18 23:16:42 +00:00
|
|
|
import "regexp"
|
2015-09-26 22:37:48 +00:00
|
|
|
|
|
|
|
var (
|
2016-01-11 17:58:26 +00:00
|
|
|
envRe = regexp.MustCompile(`\$({[a-zA-Z0-9_\.]+}|[a-zA-Z0-9_\.]+)`)
|
2015-09-26 22:37:48 +00:00
|
|
|
)
|
|
|
|
|
2015-11-18 23:16:42 +00:00
|
|
|
// ReplaceEnv takes an arg and replaces all occurences of environment variables.
|
2015-09-26 22:37:48 +00:00
|
|
|
// If the variable is found in the passed map it is replaced, otherwise the
|
|
|
|
// original string is returned.
|
2016-01-11 17:58:26 +00:00
|
|
|
func ReplaceEnv(arg string, environents ...map[string]string) string {
|
2015-09-26 22:37:48 +00:00
|
|
|
return envRe.ReplaceAllStringFunc(arg, func(arg string) string {
|
|
|
|
stripped := arg[1:]
|
|
|
|
if stripped[0] == '{' {
|
|
|
|
stripped = stripped[1 : len(stripped)-1]
|
|
|
|
}
|
|
|
|
|
2016-01-11 17:58:26 +00:00
|
|
|
for _, env := range environents {
|
|
|
|
if value, ok := env[stripped]; ok {
|
|
|
|
return value
|
|
|
|
}
|
2015-09-26 22:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return arg
|
|
|
|
})
|
|
|
|
}
|