mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-02 18:13:44 +02:00
47 lines
1.6 KiB
Go
47 lines
1.6 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)
|
|
processedWord, err := shlex.ProcessWord(value, envs)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Clean(processedWord), nil
|
|
}
|