Skip to content

Commit

Permalink
Use default token for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Priya Wadhwa committed Mar 27, 2018
1 parent f6139f2 commit b64f23b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 35 deletions.
7 changes: 1 addition & 6 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *CopyCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Infof("dest: %s", dest)

// First, resolve any environment replacement
resolvedEnvs, err := util.ResolveEnvironmentReplacementList(c.copyToString(), c.cmd.SourcesAndDest, config.Env, true)
resolvedEnvs, err := util.ResolveEnvironmentReplacementList(c.cmd.SourcesAndDest, config.Env, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -86,11 +86,6 @@ func (c *CopyCommand) ExecuteCommand(config *manifest.Schema2Config) error {
return nil
}

func (c *CopyCommand) copyToString() string {
copy := []string{"COPY"}
return strings.Join(append(copy, c.cmd.SourcesAndDest...), " ")
}

// FilesToSnapshot should return an empty array if still nil; no files were changed
func (c *CopyCommand) FilesToSnapshot() []string {
return c.snapshotFiles
Expand Down
13 changes: 2 additions & 11 deletions pkg/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ type EnvCommand struct {

func (e *EnvCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Info("cmd: ENV")
envString := envToString(e.cmd)
newEnvs := e.cmd.Env
for index, pair := range newEnvs {
expandedKey, err := util.ResolveEnvironmentReplacement(envString, pair.Key, config.Env, false)
expandedKey, err := util.ResolveEnvironmentReplacement(pair.Key, config.Env, false)
if err != nil {
return err
}
expandedValue, err := util.ResolveEnvironmentReplacement(envString, pair.Value, config.Env, false)
expandedValue, err := util.ResolveEnvironmentReplacement(pair.Value, config.Env, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -89,14 +88,6 @@ 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{}
Expand Down
8 changes: 1 addition & 7 deletions pkg/commands/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ type ExposeCommand struct {
func (r *ExposeCommand) ExecuteCommand(config *manifest.Schema2Config) error {
// Grab the currently exposed ports
existingPorts := config.ExposedPorts
exposeString := r.exposeToString()
// Add any new ones in
for _, p := range r.cmd.Ports {
// Resolve any environment variables
p, err := util.ResolveEnvironmentReplacement(exposeString, p, config.Env, false)
p, err := util.ResolveEnvironmentReplacement(p, config.Env, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -66,11 +65,6 @@ func validProtocol(protocol string) bool {
return false
}

func (r *ExposeCommand) exposeToString() string {
expose := []string{"EXPOSE"}
return strings.Join(append(expose, r.cmd.Ports...), " ")
}

func (r *ExposeCommand) FilesToSnapshot() []string {
return []string{}
}
Expand Down
15 changes: 5 additions & 10 deletions pkg/util/command_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package util

import (
"bytes"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/docker/docker/builder/dockerfile/parser"
"github.com/docker/docker/builder/dockerfile/shell"
Expand All @@ -29,10 +28,10 @@ import (
)

// ResolveEnvironmentReplacement resolves a list of values by calling resolveEnvironmentReplacement
func ResolveEnvironmentReplacementList(command string, values, envs []string, isFilepath bool) ([]string, error) {
func ResolveEnvironmentReplacementList(values, envs []string, isFilepath bool) ([]string, error) {
var resolvedValues []string
for _, value := range values {
resolved, err := ResolveEnvironmentReplacement(command, value, envs, isFilepath)
resolved, err := ResolveEnvironmentReplacement(value, envs, isFilepath)
logrus.Debugf("Resolved %s to %s", value, resolved)
if err != nil {
return nil, err
Expand All @@ -42,7 +41,7 @@ func ResolveEnvironmentReplacementList(command string, values, envs []string, is
return resolvedValues, nil
}

// resolveEnvironmentReplacement resolves replacing env variables in some text from envs
// ResolveEnvironmentReplacement resolves replacing env variables in some text from envs
// It takes in a string representation of the command, the value to be resolved, and a list of envs (config.Env)
// Ex: fp = $foo/newdir, envs = [foo=/foodir], then this should return /foodir/newdir
// The dockerfile/shell package handles processing env values
Expand All @@ -51,12 +50,8 @@ func ResolveEnvironmentReplacementList(command string, values, envs []string, is
// ""a'b'c"" -> "a'b'c"
// "Rex\ The\ Dog \" -> "Rex The Dog"
// "a\"b" -> "a"b"
func ResolveEnvironmentReplacement(command, value string, envs []string, isFilepath bool) (string, error) {
p, err := parser.Parse(bytes.NewReader([]byte(command)))
if err != nil {
return "", err
}
shlex := shell.NewLex(p.EscapeToken)
func ResolveEnvironmentReplacement(value string, envs []string, isFilepath bool) (string, error) {
shlex := shell.NewLex(parser.DefaultEscapeToken)
fp, err := shlex.ProcessWord(value, envs)
if !isFilepath {
return fp, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/command_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var testEnvReplacement = []struct {

func Test_EnvReplacement(t *testing.T) {
for _, test := range testEnvReplacement {
actualPath, err := ResolveEnvironmentReplacement(test.command, test.path, test.envs, test.isFilepath)
actualPath, err := ResolveEnvironmentReplacement(test.path, test.envs, test.isFilepath)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedPath, actualPath)

}
Expand Down

0 comments on commit b64f23b

Please sign in to comment.