open-nomad/helper/args/args.go

29 lines
809 B
Go
Raw Normal View History

2015-09-26 22:37:48 +00:00
package args
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
)
2016-05-15 16:41:34 +00:00
// ReplaceEnv takes an arg and replaces all occurrences 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-03-26 19:49:49 +00:00
func ReplaceEnv(arg string, environments ...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-03-26 19:49:49 +00:00
for _, env := range environments {
2016-01-11 17:58:26 +00:00
if value, ok := env[stripped]; ok {
return value
}
2015-09-26 22:37:48 +00:00
}
return arg
})
}
// ReplaceEnvWithPlaceHolder replaces all occurrences of environment variables with the placeholder string.
func ReplaceEnvWithPlaceHolder(arg string, placeholder string) string {
return envRe.ReplaceAllString(arg, placeholder)
}