Get escape token by parsing ENV command
This commit is contained in:
parent
af6a074fd6
commit
070b0517d5
|
|
@ -67,10 +67,6 @@ func execute() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Set the escape token
|
|
||||||
if err := dockerfile.SetEscapeToken(d); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
baseImage := stages[0].BaseName
|
baseImage := stages[0].BaseName
|
||||||
|
|
||||||
// Unpack file system to root
|
// Unpack file system to root
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,10 @@ limitations under the License.
|
||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/dockerfile"
|
"bytes"
|
||||||
"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/docker/docker/builder/dockerfile/parser"
|
||||||
"github.com/docker/docker/builder/dockerfile/shell"
|
"github.com/docker/docker/builder/dockerfile/shell"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -32,15 +33,20 @@ type EnvCommand struct {
|
||||||
|
|
||||||
func (e *EnvCommand) ExecuteCommand(config *manifest.Schema2Config) error {
|
func (e *EnvCommand) ExecuteCommand(config *manifest.Schema2Config) error {
|
||||||
logrus.Info("cmd: ENV")
|
logrus.Info("cmd: ENV")
|
||||||
newEnvs := e.cmd.Env
|
|
||||||
for index, pair := range newEnvs {
|
|
||||||
// The dockerfile/shell package handles processing env values
|
// The dockerfile/shell package handles processing env values
|
||||||
// It handles escape characters and supports expansion from the config.Env array
|
// 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)
|
// Shlex handles some of the following use cases (these and more are tested in integration tests)
|
||||||
// ""a'b'c"" -> "a'b'c"
|
// ""a'b'c"" -> "a'b'c"
|
||||||
// "Rex\ The\ Dog \" -> "Rex The Dog"
|
// "Rex\ The\ Dog \" -> "Rex The Dog"
|
||||||
// "a\"b" -> "a"b"
|
// "a\"b" -> "a"b"
|
||||||
shlex := shell.NewLex(dockerfile.EscapeToken)
|
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 {
|
||||||
expandedValue, err := shlex.ProcessWord(pair.Value, config.Env)
|
expandedValue, err := shlex.ProcessWord(pair.Value, config.Env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -92,6 +98,14 @@ Loop:
|
||||||
return nil
|
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
|
// We know that no files have changed, so return an empty array
|
||||||
func (e *EnvCommand) FilesToSnapshot() []string {
|
func (e *EnvCommand) FilesToSnapshot() []string {
|
||||||
return []string{}
|
return []string{}
|
||||||
|
|
|
||||||
|
|
@ -53,3 +53,21 @@ func TestUpdateEnvConfig(t *testing.T) {
|
||||||
updateConfigEnv(newEnvs, cfg)
|
updateConfigEnv(newEnvs, cfg)
|
||||||
testutil.CheckErrorAndDeepEqual(t, false, nil, expectedEnvArray, cfg.Env)
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,6 @@ 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))
|
||||||
|
|
@ -37,12 +35,3 @@ func Parse(b []byte) ([]instructions.Stage, error) {
|
||||||
}
|
}
|
||||||
return stages, err
|
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue