2014-08-15 12:29:35 -04:00
package builder
2014-08-05 16:17:40 -04:00
import (
"regexp"
"strings"
)
var (
2014-10-14 02:58:55 -04: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}`
2014-10-17 15:15:07 -04:00
tokenEnvInterpolation = regexp . MustCompile ( ` (\\|\\\\+|[^\\]|\b|\A)\$([[:alnum:]_]+| { [[:alnum:]_]+}) ` )
2014-10-14 02:58:55 -04:00
// this intentionally punts on more exotic interpolations like ${SOME_VAR%suffix} and lets the shell handle those directly
2014-08-05 16:17:40 -04:00
)
2014-08-07 01:56:44 -04:00
// handle environment replacement. Used in dispatcher.
2014-08-26 15:25:44 -04:00
func ( b * Builder ) replaceEnv ( str string ) string {
2014-10-14 02:58:55 -04:00
for _ , match := range tokenEnvInterpolation . FindAllString ( str , - 1 ) {
2014-10-17 15:15:07 -04:00
idx := strings . Index ( match , "\\$" )
if idx != - 1 {
if idx + 2 >= len ( match ) {
str = strings . Replace ( str , match , "\\$" , - 1 )
continue
}
2014-10-25 13:58:57 -04:00
prefix := match [ : idx ]
2014-10-17 15:15:07 -04:00
stripped := match [ idx + 2 : ]
2014-10-25 13:58:57 -04:00
str = strings . Replace ( str , match , prefix + "$" + stripped , - 1 )
2014-10-17 15:15:07 -04:00
continue
}
2014-08-05 16:17:40 -04:00
match = match [ strings . Index ( match , "$" ) : ]
matchKey := strings . Trim ( match , "${}" )
2014-08-13 06:07:41 -04: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 07:14:21 -04:00
break
2014-08-05 16:17:40 -04:00
}
}
}
return str
}
2014-08-13 06:07:41 -04:00
func handleJsonArgs ( args [ ] string , attributes map [ string ] bool ) [ ] string {
2014-08-30 07:34:09 -04:00
if len ( args ) == 0 {
return [ ] string { }
}
2014-08-13 06:07:41 -04:00
if attributes != nil && attributes [ "json" ] {
return args
}
// literal string command, not an exec array
2014-08-30 07:34:09 -04:00
return [ ] string { strings . Join ( args , " " ) }
2014-08-13 06:07:41 -04:00
}