Skip to content

Commit 55dccae

Browse files
authored
chore(docs): document how to correctly override list(string) parameters (coder#15497)
- Adds documentation for how to correctly hold --parameter with list(string) - Adds tests for the aforementioned documented correct finger positions for --parameter list(string)
1 parent 6ff302b commit 55dccae

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

cli/create_test.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -864,24 +864,34 @@ func TestCreateValidateRichParameters(t *testing.T) {
864864
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
865865
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
866866

867-
inv, root := clitest.New(t, "create", "my-workspace", "--template", template.Name)
868-
clitest.SetupConfig(t, member, root)
869-
pty := ptytest.New(t).Attach(inv)
870-
clitest.Start(t, inv)
867+
t.Run("Prompt", func(t *testing.T) {
868+
inv, root := clitest.New(t, "create", "my-workspace-1", "--template", template.Name)
869+
clitest.SetupConfig(t, member, root)
870+
pty := ptytest.New(t).Attach(inv)
871+
clitest.Start(t, inv)
872+
873+
pty.ExpectMatch(listOfStringsParameterName)
874+
pty.ExpectMatch("aaa, bbb, ccc")
875+
pty.ExpectMatch("Confirm create?")
876+
pty.WriteLine("yes")
877+
})
871878

872-
matches := []string{
873-
listOfStringsParameterName, "",
874-
"aaa, bbb, ccc", "",
875-
"Confirm create?", "yes",
876-
}
877-
for i := 0; i < len(matches); i += 2 {
878-
match := matches[i]
879-
value := matches[i+1]
880-
pty.ExpectMatch(match)
881-
if value != "" {
882-
pty.WriteLine(value)
883-
}
884-
}
879+
t.Run("Default", func(t *testing.T) {
880+
t.Parallel()
881+
inv, root := clitest.New(t, "create", "my-workspace-2", "--template", template.Name, "--yes")
882+
clitest.SetupConfig(t, member, root)
883+
clitest.Run(t, inv)
884+
})
885+
886+
t.Run("CLIOverride/DoubleQuote", func(t *testing.T) {
887+
t.Parallel()
888+
889+
// Note: see https://go.dev/play/p/vhTUTZsVrEb for how to escape this properly
890+
parameterArg := fmt.Sprintf(`"%s=[""ddd=foo"",""eee=bar"",""fff=baz""]"`, listOfStringsParameterName)
891+
inv, root := clitest.New(t, "create", "my-workspace-3", "--template", template.Name, "--parameter", parameterArg, "--yes")
892+
clitest.SetupConfig(t, member, root)
893+
clitest.Run(t, inv)
894+
})
885895
})
886896

887897
t.Run("ValidateListOfStrings_YAMLFile", func(t *testing.T) {

docs/admin/templates/extending-templates/parameters.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,31 @@ data "coder_parameter" "security_groups" {
7979
}
8080
```
8181

82+
> [!NOTE] Overriding a `list(string)` on the CLI is tricky because:
83+
>
84+
> - `--parameter "parameter_name=parameter_value"` is parsed as CSV.
85+
> - `parameter_value` is parsed as JSON.
86+
>
87+
> So, to properly specify a `list(string)` with the `--parameter` CLI argument,
88+
> you will need to take care of both CSV quoting and shell quoting.
89+
>
90+
> For the above example, to override the default values of the `security_groups`
91+
> parameter, you will need to pass the following argument to `coder create`:
92+
>
93+
> ```
94+
> --parameter "\"security_groups=[\"\"DevOps Security Group\"\",\"\"Backend Security Group\"\"]\""
95+
> ```
96+
>
97+
> Alternatively, you can use `--rich-parameter-file` to work around the above
98+
> issues. This allows you to specify parameters as YAML. An equivalent parameter
99+
> file for the above `--parameter` is provided below:
100+
>
101+
> ```yaml
102+
> security_groups:
103+
> - DevOps Security Group
104+
> - Backend Security Group
105+
> ```
106+
82107
## Options
83108
84109
A `string` parameter can provide a set of options to limit the user's choices:

0 commit comments

Comments
 (0)