github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/helper/args/args.go (about) 1 package args 2 3 import "regexp" 4 5 var ( 6 envRe = regexp.MustCompile(`\${[a-zA-Z0-9_\-\.]+}`) 7 ) 8 9 // ReplaceEnv takes an arg and replaces all occurrences of environment variables. 10 // If the variable is found in the passed map it is replaced, otherwise the 11 // original string is returned. 12 func ReplaceEnv(arg string, environments ...map[string]string) string { 13 return envRe.ReplaceAllStringFunc(arg, func(arg string) string { 14 stripped := arg[2 : len(arg)-1] 15 for _, env := range environments { 16 if value, ok := env[stripped]; ok { 17 return value 18 } 19 } 20 21 return arg 22 }) 23 }