Replaced shlex with dockerfile/shell package

This commit is contained in:
Priya Wadhwa 2018-03-16 12:16:07 -07:00
parent e84ebcc494
commit f33e507018
No known key found for this signature in database
GPG Key ID: 0D0DAFD8F7AA73AE
4 changed files with 31 additions and 6 deletions

View File

@ -13,3 +13,7 @@ ENV test2='a"b"c'
ENV test3=a\ b name2=b\ c
ENV test4="a\"b"
ENV test5='a\"b'
ENV test6="a\'b"
ENV atomic=one
ENV atomic=two newatomic=$atomic
ENV newenv=$doesntexist/newenv

View File

@ -31,3 +31,11 @@ metadataTest:
value: a"b
- key: test5
value: a\"b
- key: test6
value: a\'b
- key: atomic
value: two
- key: newatomic
value: one
- key: newenv
value: /newenv

View File

@ -17,9 +17,10 @@ limitations under the License.
package commands
import (
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/dockerfile"
"github.com/containers/image/manifest"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/google/shlex"
"github.com/docker/docker/builder/dockerfile/shell"
"github.com/sirupsen/logrus"
"os"
"strings"
@ -33,14 +34,23 @@ func (e *EnvCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Info("cmd: ENV")
newEnvs := e.cmd.Env
for index, pair := range newEnvs {
newVal := os.Expand(pair.Value, os.Getenv)
tokens, _ := shlex.Split(newVal)
// The dockerfile/shell package handles processing env values
// 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{
Key: pair.Key,
Value: strings.Join(tokens, " "),
Value: expandedValue,
}
logrus.Infof("Setting environment variable %s=%s", pair.Key, pair.Value)
if err := os.Setenv(pair.Key, pair.Value); err != nil {
logrus.Infof("Setting environment variable %s=%s", pair.Key, expandedValue)
if err := os.Setenv(pair.Key, expandedValue); err != nil {
return err
}
}

View File

@ -23,12 +23,15 @@ import (
"github.com/docker/docker/builder/dockerfile/parser"
)
var EscapeToken rune
// Parse parses the contents of a Dockerfile and returns a list of commands
func Parse(b []byte) ([]instructions.Stage, error) {
p, err := parser.Parse(bytes.NewReader(b))
if err != nil {
return nil, err
}
EscapeToken = p.EscapeToken
stages, _, err := instructions.Parse(p.AST)
if err != nil {
return nil, err