Add string map version ProcessWord to Lex
Signed-off-by: Yuichiro Kaneko <spiketeika@gmail.com>docker-18.09
parent
6583105db9
commit
8715fbf6bf
|
@ -44,6 +44,13 @@ func (s *Lex) ProcessWords(word string, env []string) ([]string, error) {
|
||||||
return words, err
|
return words, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProcessWordWithMap will use the 'env' list of environment variables,
|
||||||
|
// and replace any env var references in 'word'.
|
||||||
|
func (s *Lex) ProcessWordWithMap(word string, env map[string]string) (string, error) {
|
||||||
|
word, _, err := s.process(word, env)
|
||||||
|
return word, err
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Lex) process(word string, env map[string]string) (string, []string, error) {
|
func (s *Lex) process(word string, env map[string]string) (string, []string, error) {
|
||||||
sw := &shellWord{
|
sw := &shellWord{
|
||||||
envs: env,
|
envs: env,
|
||||||
|
|
|
@ -22,6 +22,7 @@ func TestShellParser4EnvVars(t *testing.T) {
|
||||||
shlex := NewLex('\\')
|
shlex := NewLex('\\')
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
envs := []string{"PWD=/home", "SHELL=bash", "KOREAN=한국어"}
|
envs := []string{"PWD=/home", "SHELL=bash", "KOREAN=한국어"}
|
||||||
|
envsMap := buildEnvs(envs)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
lineCount++
|
lineCount++
|
||||||
|
@ -56,6 +57,14 @@ func TestShellParser4EnvVars(t *testing.T) {
|
||||||
assert.Check(t, err, "at line %d of %s", lineCount, fn)
|
assert.Check(t, err, "at line %d of %s", lineCount, fn)
|
||||||
assert.Check(t, is.Equal(newWord, expected), "at line %d of %s", lineCount, fn)
|
assert.Check(t, is.Equal(newWord, expected), "at line %d of %s", lineCount, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newWord, err = shlex.ProcessWordWithMap(source, envsMap)
|
||||||
|
if expected == "error" {
|
||||||
|
assert.Check(t, is.ErrorContains(err, ""), "input: %q, result: %q", source, newWord)
|
||||||
|
} else {
|
||||||
|
assert.Check(t, err, "at line %d of %s", lineCount, fn)
|
||||||
|
assert.Check(t, is.Equal(newWord, expected), "at line %d of %s", lineCount, fn)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue