github.com/manicqin/nomad@v0.9.5/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  }
    24  
    25  // ReplaceEnvWithPlaceHolder replaces all occurrences of environment variables with the placeholder string.
    26  func ReplaceEnvWithPlaceHolder(arg string, placeholder string) string {
    27  	return envRe.ReplaceAllString(arg, placeholder)
    28  }
    29  
    30  // ContainsEnv takes an arg and returns true if if contains an environment variable reference
    31  func ContainsEnv(arg string) bool {
    32  	return envRe.MatchString(arg)
    33  }