2014-08-15 16:29:35 +00:00
package builder
2014-08-05 20:17:40 +00:00
import (
"regexp"
"strings"
)
var (
2014-10-14 06:58:55 +00:00
// `\\\\+|[^\\]|\b|\A` - match any number of "\\" (ie, properly-escaped backslashes), or a single non-backslash character, or a word boundary, or beginning-of-line
// `\$` - match literal $
// `[[:alnum:]_]+` - match things like `$SOME_VAR`
// `{[[:alnum:]_]+}` - match things like `${SOME_VAR}`
tokenEnvInterpolation = regexp . MustCompile ( ` (\\\\+|[^\\]|\b|\A)\$([[:alnum:]_]+| { [[:alnum:]_]+}) ` )
// this intentionally punts on more exotic interpolations like ${SOME_VAR%suffix} and lets the shell handle those directly
2014-08-05 20:17:40 +00:00
)
2014-08-07 05:56:44 +00:00
// handle environment replacement. Used in dispatcher.
2014-08-26 19:25:44 +00:00
func ( b * Builder ) replaceEnv ( str string ) string {
2014-10-14 06:58:55 +00:00
for _ , match := range tokenEnvInterpolation . FindAllString ( str , - 1 ) {
2014-08-05 20:17:40 +00:00
match = match [ strings . Index ( match , "$" ) : ]
matchKey := strings . Trim ( match , "${}" )
2014-08-13 10:07:41 +00:00
for _ , keyval := range b . Config . Env {
tmp := strings . SplitN ( keyval , "=" , 2 )
if tmp [ 0 ] == matchKey {
str = strings . Replace ( str , match , tmp [ 1 ] , - 1 )
2014-08-19 11:14:21 +00:00
break
2014-08-05 20:17:40 +00:00
}
}
}
return str
}
2014-08-13 10:07:41 +00:00
func handleJsonArgs ( args [ ] string , attributes map [ string ] bool ) [ ] string {
2014-08-30 11:34:09 +00:00
if len ( args ) == 0 {
return [ ] string { }
}
2014-08-13 10:07:41 +00:00
if attributes != nil && attributes [ "json" ] {
return args
}
// literal string command, not an exec array
2014-08-30 11:34:09 +00:00
return [ ] string { strings . Join ( args , " " ) }
2014-08-13 10:07:41 +00:00
}