support.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package builder
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. var (
  7. // `\\\\+|[^\\]|\b|\A` - match any number of "\\" (ie, properly-escaped backslashes), or a single non-backslash character, or a word boundary, or beginning-of-line
  8. // `\$` - match literal $
  9. // `[[:alnum:]_]+` - match things like `$SOME_VAR`
  10. // `{[[:alnum:]_]+}` - match things like `${SOME_VAR}`
  11. tokenEnvInterpolation = regexp.MustCompile(`(\\\\+|[^\\]|\b|\A)\$([[:alnum:]_]+|{[[:alnum:]_]+})`)
  12. // this intentionally punts on more exotic interpolations like ${SOME_VAR%suffix} and lets the shell handle those directly
  13. )
  14. // handle environment replacement. Used in dispatcher.
  15. func (b *Builder) replaceEnv(str string) string {
  16. for _, match := range tokenEnvInterpolation.FindAllString(str, -1) {
  17. match = match[strings.Index(match, "$"):]
  18. matchKey := strings.Trim(match, "${}")
  19. for _, keyval := range b.Config.Env {
  20. tmp := strings.SplitN(keyval, "=", 2)
  21. if tmp[0] == matchKey {
  22. str = strings.Replace(str, match, tmp[1], -1)
  23. break
  24. }
  25. }
  26. }
  27. return str
  28. }
  29. func handleJsonArgs(args []string, attributes map[string]bool) []string {
  30. if len(args) == 0 {
  31. return []string{}
  32. }
  33. if attributes != nil && attributes["json"] {
  34. return args
  35. }
  36. // literal string command, not an exec array
  37. return []string{strings.Join(args, " ")}
  38. }