github.com/dottorblaster/docker@v1.5.0/builder/support.go (about)

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