Skip to content

Commit edec220

Browse files
committed
chore: add invalidation option
1 parent db2174d commit edec220

File tree

7 files changed

+189
-25
lines changed

7 files changed

+189
-25
lines changed

cli/env.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cli
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"fmt"
7+
"os"
8+
"strings"
9+
10+
"github.com/coder/serpent"
11+
)
12+
13+
func (r *RootCmd) SetEnv() *serpent.Command {
14+
var (
15+
vars []string
16+
groups []string
17+
)
18+
19+
cmd := &serpent.Command{
20+
Use: "env",
21+
Short: "Sets environment variables for terraform plan/apply.",
22+
Options: []serpent.Option{
23+
{
24+
Name: "vars",
25+
Description: "Variables.",
26+
Flag: "vars",
27+
FlagShorthand: "v",
28+
Default: "",
29+
Value: serpent.StringArrayOf(&vars),
30+
},
31+
{
32+
Name: "groups",
33+
Description: "Groups.",
34+
Flag: "groups",
35+
FlagShorthand: "g",
36+
Default: "",
37+
Value: serpent.StringArrayOf(&groups),
38+
},
39+
},
40+
Hidden: false,
41+
Handler: func(i *serpent.Invocation) error {
42+
for _, val := range vars {
43+
parts := strings.Split(val, "=")
44+
if len(parts) != 2 {
45+
continue
46+
}
47+
sum := sha256.Sum256([]byte(parts[0]))
48+
err := os.Setenv("CODER_PARAMETER_"+hex.EncodeToString(sum[:]), parts[1])
49+
if err != nil {
50+
return err
51+
}
52+
fmt.Println("CODER_PARAMETER_" + hex.EncodeToString(sum[:]) + "=" + parts[1])
53+
}
54+
55+
return nil
56+
},
57+
}
58+
59+
return cmd
60+
}

cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func (r *RootCmd) Root() *serpent.Command {
118118
}
119119
cmd.AddSubcommands(r.TerraformPlan())
120120
cmd.AddSubcommands(r.WebsocketServer())
121+
cmd.AddSubcommands(r.SetEnv())
121122
return cmd
122123
}
123124

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.20250320160506-90c9d74d3be2
12+
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250320193216-20d6f8c6e701
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-20250320162119-5a4161c52765
145+
replace github.com/aquasecurity/trivy => github.com/emyrk/trivy v0.0.0-20250320190949-47caa1ac2d53

go.sum

Lines changed: 4 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.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=
721+
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250320193216-20d6f8c6e701 h1:MDg0cnoNJQ82uNqGDE6JRE+flW2PmniKJRZS2aHOKmw=
722+
github.com/coder/terraform-provider-coder/v2 v2.2.1-0.20250320193216-20d6f8c6e701/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,8 @@ 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-20250320162119-5a4161c52765 h1:lyQUWrbVGq082z5eo68sDbJnJuZAErRJK7lYFot8BTg=
755-
github.com/emyrk/trivy v0.0.0-20250320162119-5a4161c52765/go.mod h1:QqQijstmQF9wfPij09KE96MLfbFGtfC21dG299ty+Fc=
754+
github.com/emyrk/trivy v0.0.0-20250320190949-47caa1ac2d53 h1:0bj1/UEj/7ZwQSm2EAYuYd87feUvqmlrUfR3MRzKOag=
755+
github.com/emyrk/trivy v0.0.0-20250320190949-47caa1ac2d53/go.mod h1:QqQijstmQF9wfPij09KE96MLfbFGtfC21dG299ty+Fc=
756756
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
757757
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
758758
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=

testdata/connections/checker/main.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
variable "solutions" {
3+
type = map(list(string))
4+
}
5+
6+
variable "guess" {
7+
type = list(string)
8+
}
9+
10+
locals {
11+
# [for connection, solution in local.solutions : connection if (length(setintersection(solution, jsondecode(data.coder_parameter.rows["yellow"].value))) == 4)]
12+
diff = [for connection, solution in var.solutions : {
13+
connection = connection
14+
distance = 4 - length(setintersection(solution, var.guess))
15+
}]
16+
17+
solved = [for diff in local.diff : diff.connection if diff.distance == 0]
18+
one_away = [for diff in local.diff : diff.connection if diff.distance == 1]
19+
description = length(local.one_away) == 1 ? "One away..." : (
20+
length(local.solved) == 1 ? "Solved!" : (
21+
"Select 4 words that share a common connection."
22+
)
23+
)
24+
}
25+
26+
output "out" {
27+
value = local.one_away
28+
}
29+
30+
output "title" {
31+
value = length(local.solved) == 1 ? "${local.solved[0]}" : "??"
32+
}
33+
34+
output "description" {
35+
value = local.description
36+
}
37+
38+
output "solved" {
39+
value = length(local.solved) == 1 ? true : false
40+
}

testdata/connections/main.tf

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
3+
go run ../../cmd/preview/main.go \
4+
-v "\"yellow=[\"\"bay\"\",\"\"sound\"\",\"\"strait\"\", \"\"channel\"\"]\"" \
5+
-v "\"green=[\"\"bungee\"\",\"\"extension\"\",\"\"spinal\"\", \"\"umbilical\"\"]\"" \
6+
-v "\"blue=[\"\"direct\"\",\"\"loud\"\",\"\"vocal\"\", \"\"frank\"\"]\"" \
7+
-v "\"purple=[\"\"ship\"\",\"\"genie\"\",\"\"lighting\"\", \"\"message\"\"]\""
8+
*/
9+
110
terraform {
211
required_providers {
312
coder = {
@@ -7,16 +16,19 @@ terraform {
716
}
817

918
locals {
10-
word_bank = [
19+
solutions = tomap ({
1120
// Outspoken -- Yellow
12-
"direct", "frank", "loud", "vocal",
21+
"Outspoken": ["direct", "frank", "loud", "vocal"],
1322
// Bodies of water -- Green
14-
"bay", "channel", "sound", "strait",
23+
"Bodies of water": ["bay", "channel", "sound", "strait"],
1524
// Kinds of cords -- Blue
16-
"bungee", "extension", "spinal", "umbilical",
25+
"Kinds of cords": ["bungee", "extension", "spinal", "umbilical"],
1726
// Things in bottles -- Purple
18-
"genie", "lighting", "message", "ship"
19-
]
27+
"Things in a bottle": ["genie", "lighting", "message", "ship"],
28+
})
29+
# solution_list = [for _, words in local.solutions : words]
30+
word_bank = flatten([for _, words in local.solutions : words])
31+
2032

2133
used_words = setunion(
2234
[],
@@ -27,36 +39,68 @@ locals {
2739
)
2840

2941
available_words = setsubtract(toset(local.word_bank), toset(local.used_words))
42+
43+
colors = toset(["yellow", "green", "blue", "purple"])
44+
45+
solved = length([for color in local.colors : module.checker[color].solved if module.checker[color].solved]) == 4
3046
}
3147

48+
49+
50+
module "checker" {
51+
for_each = local.colors
52+
source = "./checker"
53+
solutions = local.solutions
54+
guess = jsondecode(coalesce(data.coder_parameter.rows[each.value].value, "[]"))
55+
}
56+
57+
data "coder_parameter" display {
58+
name = "display"
59+
display_name = local.solved ? "Workspace name" : join(", ", local.available_words)
60+
description = local.solved ? "Congrats, you won! What is your workspace name?" : "Remaining words"
61+
type = "string"
62+
default = local.solved ? "" : "Keep guessing!"
63+
64+
form_type_metadata = jsonencode({
65+
disabled = !local.solved
66+
})
67+
}
68+
69+
output "solved" {
70+
value = local.solved
71+
}
72+
73+
3274
data "coder_parameter" "rows" {
33-
for_each = toset(["yellow", "green", "blue", "purple"])
75+
for_each = local.colors
3476
name = each.value
77+
display_name = module.checker[each.value].title
78+
description = module.checker[each.value].description
3579
# name = "rows"
36-
display_name = "Row"
3780
type = "list(string)"
3881
form_type = "multi-select"
82+
form_type_metadata = jsonencode({
83+
disabled = module.checker[each.value].solved
84+
})
3985
default = "[]"
86+
order = 11
4087

4188
dynamic "option" {
42-
# for_each = tolist(setsubtract(toset(local.word_bank), toset(local.used_words)))
43-
for_each = local.available_words
89+
# for_each = toset(local.word_bank)
90+
// Must include the options that are selected, otherwise they are not in
91+
// the option set.
92+
for_each = toset(concat(tolist(local.available_words), jsondecode(data.coder_parameter.rows[each.value].value)))
4493
content {
4594
name = option.value
4695
value = option.value
4796
}
4897
}
49-
}
5098

51-
output "remaining" {
52-
value = local.available_words
99+
validation {
100+
error = "Hey!"
101+
invalid = length(data.coder_parameter.rows[each.value].value) > 4
102+
}
53103
}
54104

55-
output "used" {
56-
value = local.used_words
57-
}
58105

59-
output "yellow" {
60-
value = data.coder_parameter.rows["yellow"].value
61-
}
62106

testdata/invalid/main.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
}
6+
}
7+
}
8+
9+
data "coder_parameter" "invalid" {
10+
name = "invalid"
11+
type = "string"
12+
default = "random"
13+
order = 1
14+
15+
validation {
16+
invalid = true
17+
error = "This is an invalid parameter"
18+
}
19+
}

0 commit comments

Comments
 (0)