fix(ansible): trim comma-separated CLI inputs
This commit is contained in:
parent
4d1e46b933
commit
e5b891e7d7
2 changed files with 32 additions and 2 deletions
|
|
@ -31,7 +31,13 @@ func splitCommaSeparatedOption(value string) []string {
|
|||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
return split(value, ",")
|
||||
var out []string
|
||||
for _, item := range split(value, ",") {
|
||||
if trimmed := trimSpace(item); trimmed != "" {
|
||||
out = append(out, trimmed)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// positionalArgs extracts all positional arguments from Options.
|
||||
|
|
@ -185,6 +191,11 @@ func parseKeyValueExtraVars(value string) map[string]any {
|
|||
vars := make(map[string]any)
|
||||
|
||||
for _, pair := range split(value, ",") {
|
||||
pair = trimSpace(pair)
|
||||
if pair == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
parts := splitN(pair, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
|
|
@ -195,7 +206,7 @@ func parseKeyValueExtraVars(value string) map[string]any {
|
|||
continue
|
||||
}
|
||||
|
||||
vars[key] = parts[1]
|
||||
vars[key] = trimSpace(parts[1])
|
||||
}
|
||||
|
||||
return vars
|
||||
|
|
|
|||
|
|
@ -42,6 +42,21 @@ func TestExtraVars_Good_UsesShortAlias(t *testing.T) {
|
|||
}, vars)
|
||||
}
|
||||
|
||||
func TestExtraVars_Good_TrimsWhitespaceAroundPairs(t *testing.T) {
|
||||
opts := core.NewOptions(
|
||||
core.Option{Key: "extra-vars", Value: " version = 1.2.3 , env = prod , empty = "},
|
||||
)
|
||||
|
||||
vars, err := extraVars(opts)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, map[string]any{
|
||||
"version": "1.2.3",
|
||||
"env": "prod",
|
||||
"empty": "",
|
||||
}, vars)
|
||||
}
|
||||
|
||||
func TestExtraVars_Good_IgnoresMalformedPairs(t *testing.T) {
|
||||
opts := core.NewOptions(
|
||||
core.Option{Key: "extra-vars", Value: "missing_equals,keep=this"},
|
||||
|
|
@ -57,6 +72,10 @@ func TestExtraVars_Good_IgnoresMalformedPairs(t *testing.T) {
|
|||
}, vars)
|
||||
}
|
||||
|
||||
func TestSplitCommaSeparatedOption_Good_TrimsWhitespace(t *testing.T) {
|
||||
assert.Equal(t, []string{"deploy", "setup", "smoke"}, splitCommaSeparatedOption(" deploy, setup ,smoke "))
|
||||
}
|
||||
|
||||
func TestExtraVars_Good_SupportsStructuredYAMLAndJSON(t *testing.T) {
|
||||
opts := core.NewOptions(
|
||||
core.Option{Key: "extra-vars", Value: "app:\n port: 8080\n debug: true"},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue