Skip to content

Commit ff76f9e

Browse files
committed
update clidisplay for multi select
1 parent 14036b9 commit ff76f9e

File tree

6 files changed

+119
-9
lines changed

6 files changed

+119
-9
lines changed

cli/clidisplay/resources.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package clidisplay
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"fmt"
67
"io"
8+
"slices"
79
"strings"
810

911
"github.com/hashicorp/hcl/v2"
1012
"github.com/jedib0t/go-pretty/v6/table"
1113

1214
"github.com/coder/preview/types"
15+
"github.com/coder/terraform-provider-coder/v2/provider"
1316
)
1417

1518
func WorkspaceTags(writer io.Writer, tags types.TagBlocks) hcl.Diagnostics {
@@ -59,6 +62,10 @@ func Parameters(writer io.Writer, params []types.Parameter, files map[string]*hc
5962
tableWriter.AppendHeader(row)
6063
for _, p := range params {
6164
strVal := p.Value.AsString()
65+
selections := []string{strVal}
66+
if p.FormType == provider.ParameterFormTypeMultiSelect {
67+
_ = json.Unmarshal([]byte(strVal), &selections)
68+
}
6269
//value := p.Value.Value
6370
//
6471
//if value.IsNull() {
@@ -72,7 +79,7 @@ func Parameters(writer io.Writer, params []types.Parameter, files map[string]*hc
7279
//}
7380

7481
tableWriter.AppendRow(table.Row{
75-
fmt.Sprintf("(%s) %s: %s\n%s", p.DisplayName, p.Name, p.Description, formatOptions(strVal, p.Options)),
82+
fmt.Sprintf("(%s) %s: %s\n%s", p.DisplayName, p.Name, p.Description, formatOptions(selections, p.Options)),
7683
})
7784

7885
if hcl.Diagnostics(p.Diagnostics).HasErrors() {
@@ -87,15 +94,15 @@ func Parameters(writer io.Writer, params []types.Parameter, files map[string]*hc
8794
_, _ = fmt.Fprintln(writer, tableWriter.Render())
8895
}
8996

90-
func formatOptions(selected string, options []*types.ParameterOption) string {
97+
func formatOptions(selected []string, options []*types.ParameterOption) string {
9198
var str strings.Builder
9299
sep := ""
93100
found := false
94101

95102
for _, opt := range options {
96103
str.WriteString(sep)
97104
prefix := "[ ]"
98-
if opt.Value.AsString() == selected {
105+
if slices.Contains(selected, opt.Value.AsString()) {
99106
prefix = "[X]"
100107
found = true
101108
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/aquasecurity/trivy v0.58.2
1010
github.com/coder/guts v1.0.2-0.20250227211802-139809366a22
1111
github.com/coder/serpent v0.10.0
12-
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250317152424-16ab5102775e
12+
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250320160506-90c9d74d3be2
1313
github.com/coder/websocket v1.8.13
1414
github.com/go-chi/chi v4.1.2+incompatible
1515
github.com/hashicorp/go-version v1.7.0
@@ -142,4 +142,4 @@ require (
142142
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
143143
)
144144

145-
replace github.com/aquasecurity/trivy => github.com/emyrk/trivy v0.0.0-20250319180840-c56ab499011d
145+
replace github.com/aquasecurity/trivy => /home/steven/go/src/github.com/aquasecurity/trivy

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,8 @@ github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx
718718
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc=
719719
github.com/coder/serpent v0.10.0 h1:ofVk9FJXSek+SmL3yVE3GoArP83M+1tX+H7S4t8BSuM=
720720
github.com/coder/serpent v0.10.0/go.mod h1:cZFW6/fP+kE9nd/oRkEHJpG6sXCtQ+AX7WMMEHv0Y3Q=
721-
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250317152424-16ab5102775e h1:CW8AyXdqeGRk6wE1L9py5jFbJrlGBwrUNbyb+26NrFU=
722-
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250317152424-16ab5102775e/go.mod h1:X28s3rz+aEM5PkBKvk3xcUrQFO2eNPjzRChUg9wb70U=
721+
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250320160506-90c9d74d3be2 h1:uKg+PvSQRsGdDQQNO5zKRZFjybb/izmmHp+HR0sjdCI=
722+
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250320160506-90c9d74d3be2/go.mod h1:X28s3rz+aEM5PkBKvk3xcUrQFO2eNPjzRChUg9wb70U=
723723
github.com/coder/websocket v1.8.13 h1:f3QZdXy7uGVz+4uCJy2nTZyM0yTBj8yANEHhqlXZ9FE=
724724
github.com/coder/websocket v1.8.13/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
725725
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
@@ -751,8 +751,6 @@ github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd/go.mod h1:MxLav0peU43G
751751
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
752752
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
753753
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
754-
github.com/emyrk/trivy v0.0.0-20250319180840-c56ab499011d h1:Dx8N/+CQ8Riri5dF6+yhNlguH95rRfzzfuBgwkHhgT4=
755-
github.com/emyrk/trivy v0.0.0-20250319180840-c56ab499011d/go.mod h1:QqQijstmQF9wfPij09KE96MLfbFGtfC21dG299ty+Fc=
756754
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
757755
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
758756
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=

preview_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package preview_test
22

33
import (
44
"context"
5+
"encoding/json"
56
"os"
67
"path/filepath"
78
"testing"
@@ -17,6 +18,14 @@ import (
1718
func Test_Extract(t *testing.T) {
1819
t.Parallel()
1920

21+
jsonencode := func(v interface{}) string {
22+
b, err := json.Marshal(v)
23+
if err != nil {
24+
panic(err)
25+
}
26+
return string(b)
27+
}
28+
2029
for _, tc := range []struct {
2130
skip string
2231
name string
@@ -401,6 +410,20 @@ func Test_Extract(t *testing.T) {
401410
"three": ap(),
402411
},
403412
},
413+
{
414+
name: "connections",
415+
dir: "connections",
416+
expTags: map[string]string{},
417+
input: preview.Input{
418+
PlanJSONPath: "",
419+
ParameterValues: map[string]string{
420+
"yellow": jsonencode([]string{}),
421+
},
422+
Owner: types.WorkspaceOwner{},
423+
},
424+
unknownTags: []string{},
425+
params: map[string]assertParam{},
426+
},
404427
{
405428
name: "submodcount",
406429
dir: "submodcount",

testdata/connections/main.tf

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
}
6+
}
7+
}
8+
9+
locals {
10+
word_bank = [
11+
// Outspoken -- Yellow
12+
"direct", "frank", "loud", "vocal",
13+
// Bodies of water -- Green
14+
"bay", "channel", "sound", "strait",
15+
// Kinds of cords -- Blue
16+
"bungee", "extension", "spinal", "umbilical",
17+
// Things in bottles -- Purple
18+
"genie", "lighting", "message", "ship"
19+
]
20+
21+
used_words = setunion(
22+
[],
23+
jsondecode(data.coder_parameter.yellow.value),
24+
jsondecode(data.coder_parameter.green.value),
25+
)
26+
27+
available_words = setsubtract(toset(local.word_bank), toset(local.used_words))
28+
}
29+
30+
data "coder_parameter" "yellow" {
31+
name = "yellow"
32+
display_name = "Row"
33+
type = "list(string)"
34+
form_type = "multi-select"
35+
# default = "[]"
36+
37+
dynamic "option" {
38+
# for_each = tolist(setsubtract(toset(local.word_bank), toset(local.used_words)))
39+
for_each = local.word_bank
40+
content {
41+
name = option.value
42+
value = option.value
43+
}
44+
}
45+
}
46+
47+
data "coder_parameter" "green" {
48+
name = "green"
49+
display_name = "Row"
50+
type = "list(string)"
51+
form_type = "multi-select"
52+
# default = "[]"
53+
54+
dynamic "option" {
55+
# for_each = tolist(setsubtract(toset(local.word_bank), toset(local.used_words)))
56+
for_each = local.word_bank
57+
content {
58+
name = option.value
59+
value = option.value
60+
}
61+
}
62+
}
63+
64+
output "remaining" {
65+
value = local.available_words
66+
}
67+
68+
output "used" {
69+
value = local.used_words
70+
}
71+
72+
output "yellow" {
73+
value = data.coder_parameter.yellow.value
74+
}
75+

testdata/wordle/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ locals {
2828
toset(local.alphabet),
2929
local.remove_letters
3030
)
31+
32+
won = data.coder_parameter.one.value == local.correct ||
33+
data.coder_parameter.two.value == local.correct ||
34+
data.coder_parameter.three.value == local.correct ||
35+
data.coder_parameter.four.value == local.correct ||
36+
data.coder_parameter.five.value == local.correct ||
37+
data.coder_parameter.six.value == local.correct
3138
}
3239

3340
output "unmatched" {

0 commit comments

Comments
 (0)