Get escape token by parsing ENV command

This commit is contained in:
Priya Wadhwa 2018-03-19 10:42:40 -07:00
parent af6a074fd6
commit 070b0517d5
No known key found for this signature in database
GPG Key ID: 0D0DAFD8F7AA73AE
4 changed files with 40 additions and 23 deletions

View File

@ -67,10 +67,6 @@ func execute() error {
if err != nil {
return err
}
// Set the escape token
if err := dockerfile.SetEscapeToken(d); err != nil {
return err
}
baseImage := stages[0].BaseName
// Unpack file system to root

View File

@ -17,9 +17,10 @@ limitations under the License.
package commands
import (
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/dockerfile"
"bytes"
"github.com/containers/image/manifest"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/docker/docker/builder/dockerfile/parser"
"github.com/docker/docker/builder/dockerfile/shell"
"github.com/sirupsen/logrus"
"os"
@ -32,15 +33,20 @@ type EnvCommand struct {
func (e *EnvCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Info("cmd: ENV")
// 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"
envString := envToString(e.cmd)
p, err := parser.Parse(bytes.NewReader([]byte(envString)))
if err != nil {
return err
}
shlex := shell.NewLex(p.EscapeToken)
newEnvs := e.cmd.Env
for index, pair := range newEnvs {
// 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
@ -92,6 +98,14 @@ Loop:
return nil
}
func envToString(cmd *instructions.EnvCommand) string {
env := []string{"ENV"}
for _, kvp := range cmd.Env {
env = append(env, kvp.Key+"="+kvp.Value)
}
return strings.Join(env, " ")
}
// We know that no files have changed, so return an empty array
func (e *EnvCommand) FilesToSnapshot() []string {
return []string{}

View File

@ -53,3 +53,21 @@ func TestUpdateEnvConfig(t *testing.T) {
updateConfigEnv(newEnvs, cfg)
testutil.CheckErrorAndDeepEqual(t, false, nil, expectedEnvArray, cfg.Env)
}
func TestEnvToString(t *testing.T) {
envCmd := &instructions.EnvCommand{
Env: []instructions.KeyValuePair{
{
Key: "PATH",
Value: "/some/path",
},
{
Key: "HOME",
Value: "/root",
},
},
}
expectedString := "ENV PATH=/some/path HOME=/root"
actualString := envToString(envCmd)
testutil.CheckErrorAndDeepEqual(t, false, nil, expectedString, actualString)
}

View File

@ -23,8 +23,6 @@ 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))
@ -37,12 +35,3 @@ func Parse(b []byte) ([]instructions.Stage, error) {
}
return stages, err
}
func SetEscapeToken(b []byte) error {
p, err := parser.Parse(bytes.NewReader(b))
if err != nil {
return err
}
EscapeToken = p.EscapeToken
return nil
}