Replaced shlex with dockerfile/shell package
This commit is contained in:
		
							parent
							
								
									e84ebcc494
								
							
						
					
					
						commit
						f33e507018
					
				|  | @ -13,3 +13,7 @@ ENV test2='a"b"c' | ||||||
| ENV test3=a\ b name2=b\ c | ENV test3=a\ b name2=b\ c | ||||||
| ENV test4="a\"b" | ENV test4="a\"b" | ||||||
| ENV test5='a\"b' | ENV test5='a\"b' | ||||||
|  | ENV test6="a\'b" | ||||||
|  | ENV atomic=one | ||||||
|  | ENV atomic=two newatomic=$atomic | ||||||
|  | ENV newenv=$doesntexist/newenv | ||||||
|  |  | ||||||
|  | @ -31,3 +31,11 @@ metadataTest: | ||||||
|       value: a"b |       value: a"b | ||||||
|     - key: test5 |     - key: test5 | ||||||
|       value: a\"b |       value: a\"b | ||||||
|  |     - key: test6 | ||||||
|  |       value: a\'b | ||||||
|  |     - key: atomic | ||||||
|  |       value: two | ||||||
|  |     - key: newatomic | ||||||
|  |       value: one | ||||||
|  |     - key: newenv | ||||||
|  |       value: /newenv | ||||||
|  |  | ||||||
|  | @ -17,9 +17,10 @@ limitations under the License. | ||||||
| package commands | package commands | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/dockerfile" | ||||||
| 	"github.com/containers/image/manifest" | 	"github.com/containers/image/manifest" | ||||||
| 	"github.com/docker/docker/builder/dockerfile/instructions" | 	"github.com/docker/docker/builder/dockerfile/instructions" | ||||||
| 	"github.com/google/shlex" | 	"github.com/docker/docker/builder/dockerfile/shell" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
|  | @ -33,14 +34,23 @@ func (e *EnvCommand) ExecuteCommand(config *manifest.Schema2Config) error { | ||||||
| 	logrus.Info("cmd: ENV") | 	logrus.Info("cmd: ENV") | ||||||
| 	newEnvs := e.cmd.Env | 	newEnvs := e.cmd.Env | ||||||
| 	for index, pair := range newEnvs { | 	for index, pair := range newEnvs { | ||||||
| 		newVal := os.Expand(pair.Value, os.Getenv) | 		// The dockerfile/shell package handles processing env values
 | ||||||
| 		tokens, _ := shlex.Split(newVal) | 		// It handles escape characters and supports expansion from the config.Env array
 | ||||||
|  | 		// Shlex handles some of the following use cases (these and more are tested in integration tests)
 | ||||||
|  | 		// ""a'b'c"" -> "a'b'c"
 | ||||||
|  | 		// "Rex\ The\ Dog \" -> "Rex The Dog"
 | ||||||
|  | 		// "a\"b" -> "a"b"
 | ||||||
|  | 		shlex := shell.NewLex(dockerfile.EscapeToken) | ||||||
|  | 		expandedValue, err := shlex.ProcessWord(pair.Value, config.Env) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
| 		newEnvs[index] = instructions.KeyValuePair{ | 		newEnvs[index] = instructions.KeyValuePair{ | ||||||
| 			Key:   pair.Key, | 			Key:   pair.Key, | ||||||
| 			Value: strings.Join(tokens, " "), | 			Value: expandedValue, | ||||||
| 		} | 		} | ||||||
| 		logrus.Infof("Setting environment variable %s=%s", pair.Key, pair.Value) | 		logrus.Infof("Setting environment variable %s=%s", pair.Key, expandedValue) | ||||||
| 		if err := os.Setenv(pair.Key, pair.Value); err != nil { | 		if err := os.Setenv(pair.Key, expandedValue); err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -23,12 +23,15 @@ import ( | ||||||
| 	"github.com/docker/docker/builder/dockerfile/parser" | 	"github.com/docker/docker/builder/dockerfile/parser" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | var EscapeToken rune | ||||||
|  | 
 | ||||||
| // Parse parses the contents of a Dockerfile and returns a list of commands
 | // Parse parses the contents of a Dockerfile and returns a list of commands
 | ||||||
| func Parse(b []byte) ([]instructions.Stage, error) { | func Parse(b []byte) ([]instructions.Stage, error) { | ||||||
| 	p, err := parser.Parse(bytes.NewReader(b)) | 	p, err := parser.Parse(bytes.NewReader(b)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | 	EscapeToken = p.EscapeToken | ||||||
| 	stages, _, err := instructions.Parse(p.AST) | 	stages, _, err := instructions.Parse(p.AST) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue