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-02-27 16:42:33 +00:00
|
|
|
envRe = regexp.MustCompile(`\${[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 {
|
2016-02-05 00:50:20 +00:00
|
|
|
stripped := arg[2 : len(arg)-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
|
|
|
|
})
|
|
|
|
}
|