kaniko/pkg/util/command_util.go

62 lines
2.1 KiB
Go

/*
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"bytes"
"github.com/docker/docker/builder/dockerfile/parser"
"github.com/docker/docker/builder/dockerfile/shell"
"path/filepath"
)
// 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
// 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"
func ResolveEnvironmentReplacement(command, value string, envs []string) (string, error) {
p, err := parser.Parse(bytes.NewReader([]byte(command)))
if err != nil {
return "", err
}
shlex := shell.NewLex(p.EscapeToken)
return shlex.ProcessWord(value, envs)
}
// ResolveFilepathEnvironmentReplacement replaces env variables in filepaths
// and returns a cleaned version of the path
func ResolveFilepathEnvironmentReplacement(command, value string, envs []string) (string, error) {
p, err := parser.Parse(bytes.NewReader([]byte(command)))
if err != nil {
return "", err
}
shlex := shell.NewLex(p.EscapeToken)
fp, err := shlex.ProcessWord(value, envs)
if err != nil {
return "", err
}
fp = filepath.Clean(fp)
if filepath.IsAbs(value) {
fp = filepath.Join(fp, "/")
}
return fp, nil
}