support.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. idx := strings.Index(match, "\\$")
  18. if idx != -1 {
  19. if idx+2 >= len(match) {
  20. str = strings.Replace(str, match, "\\$", -1)
  21. continue
  22. }
  23. prefix := match[:idx]
  24. stripped := match[idx+2:]
  25. str = strings.Replace(str, match, prefix+"$"+stripped, -1)
  26. continue
  27. }
  28. match = match[strings.Index(match, "$"):]
  29. matchKey := strings.Trim(match, "${}")
  30. for _, keyval := range b.Config.Env {
  31. tmp := strings.SplitN(keyval, "=", 2)
  32. if tmp[0] == matchKey {
  33. str = strings.Replace(str, match, tmp[1], -1)
  34. break
  35. }
  36. }
  37. }
  38. return str
  39. }
  40. func handleJsonArgs(args []string, attributes map[string]bool) []string {
  41. if len(args) == 0 {
  42. return []string{}
  43. }
  44. if attributes != nil && attributes["json"] {
  45. return args
  46. }
  47. // literal string command, not an exec array
  48. return []string{strings.Join(args, " ")}
  49. }