Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit350ba3d

Browse files
committed
chore(docs): document how to correctly override list(string) parameters
1 parentf2fe379 commit350ba3d

File tree

2 files changed

+87
-16
lines changed

2 files changed

+87
-16
lines changed

‎cli/create_test.go

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package cli_test
22

33
import (
4+
"bytes"
45
"context"
6+
"encoding/csv"
7+
"encoding/json"
58
"fmt"
69
"net/http"
710
"os"
811
"regexp"
12+
"strings"
913
"testing"
1014
"time"
1115

@@ -864,24 +868,62 @@ func TestCreateValidateRichParameters(t *testing.T) {
864868
coderdtest.AwaitTemplateVersionJobCompleted(t,client,version.ID)
865869
template:=coderdtest.CreateTemplate(t,client,owner.OrganizationID,version.ID)
866870

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)
871+
t.Run("Prompt",func(t*testing.T) {
872+
inv,root:=clitest.New(t,"create","my-workspace-1","--template",template.Name)
873+
clitest.SetupConfig(t,member,root)
874+
pty:=ptytest.New(t).Attach(inv)
875+
clitest.Start(t,inv)
871876

872-
matches:= []string{
873-
listOfStringsParameterName,"",
874-
"aaa, bbb, ccc","",
875-
"Confirm create?","yes",
876-
}
877-
fori:=0;i<len(matches);i+=2 {
878-
match:=matches[i]
879-
value:=matches[i+1]
880-
pty.ExpectMatch(match)
881-
ifvalue!="" {
882-
pty.WriteLine(value)
877+
matches:= []string{
878+
listOfStringsParameterName,"",
879+
"aaa, bbb, ccc","",
880+
"Confirm create?","yes",
883881
}
884-
}
882+
fori:=0;i<len(matches);i+=2 {
883+
match:=matches[i]
884+
value:=matches[i+1]
885+
pty.ExpectMatch(match)
886+
ifvalue!="" {
887+
pty.WriteLine(value)
888+
}
889+
}
890+
})
891+
892+
t.Run("Default",func(t*testing.T) {
893+
t.Parallel()
894+
inv,root:=clitest.New(t,"create","my-workspace-2","--template",template.Name,"--yes")
895+
clitest.SetupConfig(t,member,root)
896+
clitest.Run(t,inv)
897+
})
898+
899+
t.Run("CLIOverride/DoubleQuote",func(t*testing.T) {
900+
t.Parallel()
901+
inv,root:=clitest.New(t,"create","my-workspace-3","--template",template.Name,"--parameter","\""+listOfStringsParameterName+"=[\"\"ddd=foo\"\",\"\"eee=bar\"\",\"\"fff=baz\"\"]\"","--yes")
902+
clitest.SetupConfig(t,member,root)
903+
clitest.Run(t,inv)
904+
})
905+
906+
t.Run("CLIOverride/SingleQuote",func(t*testing.T) {
907+
t.Parallel()
908+
inv,root:=clitest.New(t,"create","my-workspace-4","--template",template.Name,"--parameter","\""+listOfStringsParameterName+"=[''ddd=foo'',''eee=bar'',''fff=baz'']\"","--yes")
909+
clitest.SetupConfig(t,member,root)
910+
clitest.Run(t,inv)
911+
})
912+
913+
t.Run("WhatShouldItLookLike",func(t*testing.T) {
914+
t.Parallel()
915+
916+
var (
917+
b bytes.Buffer
918+
sb strings.Builder
919+
)
920+
require.NoError(t,json.NewEncoder(&b).Encode([]string{"ddd=foo","eee=bar","fff=baz"}))
921+
cw:=csv.NewWriter(&sb)
922+
require.NoError(t,cw.Write([]string{listOfStringsParameterName+"="+b.String()}))
923+
cw.Flush()
924+
require.NoError(t,cw.Error())
925+
t.Logf("it looks like this:\n%q",strings.TrimSpace(sb.String()))
926+
})
885927
})
886928

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

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,35 @@ 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+
> You can use [this Go Playground link](https://go.dev/play/p/yvI9rdtS0ch) to generate a
98+
> correctly-quoted argument.
99+
>
100+
> Alternatively, you can use `--rich-parameter-file` to work around the above issues.
101+
> This allows you to specify parameters as YAML.
102+
> An equivalent parameter file for the above `--parameter` is provided below:
103+
>
104+
> ```yaml
105+
> security_groups:
106+
> - DevOps Security Group
107+
> - Backend Security Group
108+
> ```
109+
110+
82111
## Options
83112
84113
A `string` parameter can provide a set of options to limit the user's choices:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp