support.go 1.5 KB

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