Add string map version ProcessWord to Lex

Signed-off-by: Yuichiro Kaneko <spiketeika@gmail.com>
docker-18.09
Yuichiro Kaneko 2018-07-16 17:49:28 +09:00
parent 6583105db9
commit 8715fbf6bf
2 changed files with 16 additions and 0 deletions

View File

@ -44,6 +44,13 @@ func (s *Lex) ProcessWords(word string, env []string) ([]string, error) {
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) {
sw := &shellWord{
envs: env,

View File

@ -22,6 +22,7 @@ func TestShellParser4EnvVars(t *testing.T) {
shlex := NewLex('\\')
scanner := bufio.NewScanner(file)
envs := []string{"PWD=/home", "SHELL=bash", "KOREAN=한국어"}
envsMap := buildEnvs(envs)
for scanner.Scan() {
line := scanner.Text()
lineCount++
@ -56,6 +57,14 @@ func TestShellParser4EnvVars(t *testing.T) {
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)
}
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)
}
}
}
}