Use default token for parsing
This commit is contained in:
parent
f6139f249a
commit
b64f23b078
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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{}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue