mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 10:25:42 +03:00
Update ParseAndReplace to take a list of args and remove shell style parsing
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
package args
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/mattn/go-shellwords"
|
||||
)
|
||||
import "regexp"
|
||||
|
||||
var (
|
||||
envRe = regexp.MustCompile(`\$({[a-zA-Z0-9_]+}|[a-zA-Z0-9_]+)`)
|
||||
@@ -13,27 +8,17 @@ var (
|
||||
|
||||
// ParseAndReplace takes the user supplied args and a map of environment
|
||||
// variables. It replaces any instance of an environment variable in the args
|
||||
// with the actual value and does correct splitting of the arg list.
|
||||
func ParseAndReplace(args string, env map[string]string) ([]string, error) {
|
||||
// Set up parser.
|
||||
p := shellwords.NewParser()
|
||||
p.ParseEnv = false
|
||||
p.ParseBacktick = false
|
||||
|
||||
parsed, err := p.Parse(args)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Couldn't parse args %v: %v", args, err)
|
||||
}
|
||||
|
||||
replaced := make([]string, len(parsed))
|
||||
for i, arg := range parsed {
|
||||
// with the actual value.
|
||||
func ParseAndReplace(args []string, env map[string]string) ([]string, error) {
|
||||
replaced := make([]string, len(args))
|
||||
for i, arg := range args {
|
||||
replaced[i] = ReplaceEnv(arg, env)
|
||||
}
|
||||
|
||||
return replaced, nil
|
||||
}
|
||||
|
||||
// replaceEnv takes an arg and replaces all occurences of environment variables.
|
||||
// ReplaceEnv takes an arg and replaces all occurences of environment variables.
|
||||
// If the variable is found in the passed map it is replaced, otherwise the
|
||||
// original string is returned.
|
||||
func ReplaceEnv(arg string, env map[string]string) string {
|
||||
|
||||
@@ -21,7 +21,7 @@ var (
|
||||
)
|
||||
|
||||
func TestDriverArgs_ParseAndReplaceInvalidEnv(t *testing.T) {
|
||||
input := "invalid $FOO"
|
||||
input := []string{"invalid", "$FOO"}
|
||||
exp := []string{"invalid", "$FOO"}
|
||||
act, err := ParseAndReplace(input, envVars)
|
||||
if err != nil {
|
||||
@@ -34,7 +34,7 @@ func TestDriverArgs_ParseAndReplaceInvalidEnv(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDriverArgs_ParseAndReplaceValidEnv(t *testing.T) {
|
||||
input := fmt.Sprintf("nomad_ip \\\"$%v\\\"!", ipKey)
|
||||
input := []string{"nomad_ip", fmt.Sprintf(`"$%v"!`, ipKey)}
|
||||
exp := []string{"nomad_ip", fmt.Sprintf("\"%s\"!", ipVal)}
|
||||
act, err := ParseAndReplace(input, envVars)
|
||||
if err != nil {
|
||||
@@ -47,7 +47,7 @@ func TestDriverArgs_ParseAndReplaceValidEnv(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDriverArgs_ParseAndReplaceChainedEnv(t *testing.T) {
|
||||
input := fmt.Sprintf("-foo $%s$%s", ipKey, portKey)
|
||||
input := []string{"-foo", fmt.Sprintf("$%s$%s", ipKey, portKey)}
|
||||
exp := []string{"-foo", fmt.Sprintf("%s%s", ipVal, portVal)}
|
||||
act, err := ParseAndReplace(input, envVars)
|
||||
if err != nil {
|
||||
@@ -58,23 +58,3 @@ func TestDriverArgs_ParseAndReplaceChainedEnv(t *testing.T) {
|
||||
t.Fatalf("ParseAndReplace(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriverArgs_ParseAndReplaceInvalidArgEscape(t *testing.T) {
|
||||
input := "-c \"echo \"foo\\\" > bar.txt\""
|
||||
if _, err := ParseAndReplace(input, envVars); err == nil {
|
||||
t.Fatalf("ParseAndReplace(%v, %v) should have failed", input, envVars)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriverArgs_ParseAndReplaceValidArgEscape(t *testing.T) {
|
||||
input := "-c \"echo \\\"foo\\\" > bar.txt\""
|
||||
exp := []string{"-c", "echo \"foo\" > bar.txt"}
|
||||
act, err := ParseAndReplace(input, envVars)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse valid input args %v: %v", input, err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(act, exp) {
|
||||
t.Fatalf("ParseAndReplace(%v, %v) returned %#v; want %#v", input, envVars, act, exp)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user