Skip to content

Commit 985ce58

Browse files
committed
add warnings when preview fails to predict count meta arguments
1 parent dfd2734 commit 985ce58

File tree

4 files changed

+103
-18
lines changed

4 files changed

+103
-18
lines changed

preview.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
104104
diags := make(hcl.Diagnostics, 0)
105105
rp, rpDiags := RichParameters(modules)
106106
tags, tagDiags := WorkspaceTags(modules, p.Files())
107+
108+
// Add warnings
109+
diags = diags.Extend(warnings(modules))
110+
107111
return &Output{
108112
ModuleOutput: outputs,
109113
Parameters: rp,

testdata/wordle/main.tf

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module "one" {
1919
index = 0
2020
correct = local.correct
2121
previous = " "
22-
default = "curse"
2322
}
2423

2524
module "two" {
@@ -29,15 +28,38 @@ module "two" {
2928
previous = module.one.value
3029
}
3130

32-
data "coder_parameter" "dump" {
33-
name = "dump"
34-
display_name = "test"
35-
description = "Dump the state"
36-
type = "string"
37-
order = 100
38-
default = ""
31+
module "three" {
32+
source = "./word"
33+
index = 2
34+
correct = local.correct
35+
previous = module.two.value
3936
}
37+
#
38+
# module "four" {
39+
# source = "./word"
40+
# index = 3
41+
# correct = local.correct
42+
# previous = module.three.value
43+
# }
44+
#
45+
# module "five" {
46+
# source = "./word"
47+
# index = 4
48+
# correct = local.correct
49+
# previous = module.four.value
50+
# }
51+
#
52+
# module "six" {
53+
# source = "./word"
54+
# index = 5
55+
# correct = local.correct
56+
# previous = module.five.value
57+
# }
58+
4059

60+
output "debug" {
61+
value = module.two.debug
62+
}
4163

4264
#
4365
# module "word_one" {

testdata/wordle/word/main.tf

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,10 @@ variable "default" {
3333
}
3434

3535
output "value" {
36-
value = data.coder_parameter.word[0].value
36+
value = lower(data.coder_parameter.word[0].value)
3737
}
3838

3939
locals {
40-
matching = join("", [
41-
for i in range(0, length(var.correct)) : (
42-
substr(var.previous, i, 1) == substr(var.correct, i, 1) ? "#" : "_"
43-
)
44-
])
45-
4640
// unmatchedLetters are letters that are not exact matches from the
4741
// previous input.
4842
unmatchedLetters = [
@@ -65,21 +59,42 @@ locals {
6559
l
6660
) if contains(local.remainingLetters, l)
6761
]
62+
63+
matching = join("", [
64+
for i in range(0, length(var.correct)) : (
65+
substr(var.previous, i, 1) == substr(var.correct, i, 1) ?
66+
upper(substr(var.correct, i, 1)) :
67+
contains(local.letterExists, substr(var.previous, i, 1)) ?
68+
lower(substr(var.previous, i, 1)) :
69+
"_"
70+
)
71+
])
6872
}
6973

7074
data "coder_parameter" "word" {
7175
count = length(var.previous) == 5 ? 1 : 0
7276
name = local.names[var.index]
73-
display_name = "--> ${local.matching} <-- with ${join("", local.letterExists)}"
74-
description = local.matching
77+
display_name = var.index == 0 ? "Take a guess what the 5 letter word might be!" : "${var.index}: --> ${local.matching} <--"
78+
description = var.index == 0 ? "Additional guesses will appear.": "Capital letters are an exact match, lowercase are letters that are out of place."
7579
type = "string"
7680
order = var.index + 10
7781
default = var.default
7882

7983
validation {
80-
regex = "^[a-zA-Z]{5}$"
84+
regex = "^[a-z]{5}$"
8185
error = "You must enter a 5 letter word."
8286
}
8387
}
8488

8589

90+
91+
output "debug" {
92+
value = {
93+
"correct" = var.correct
94+
"previous" = var.previous
95+
"unmatchedLetters" = local.unmatchedLetters
96+
"remainingLetters" = local.remainingLetters
97+
"letterExists" = local.letterExists
98+
"matching" = local.matching
99+
}
100+
}

warnings.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package preview
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aquasecurity/trivy/pkg/iac/terraform"
7+
"github.com/hashicorp/hcl/v2"
8+
)
9+
10+
func warnings(modules terraform.Modules) hcl.Diagnostics {
11+
var diags hcl.Diagnostics
12+
diags = diags.Extend(unexpandedCountBlocks(modules))
13+
14+
return diags
15+
}
16+
17+
// unexpandedCountBlocks is to compensate for a bug in the trivy parser.
18+
// It is related to https://github.com/aquasecurity/trivy/pull/8479.
19+
// Essentially, submodules are processed once. So if there is interdependent
20+
// submodule references, then
21+
func unexpandedCountBlocks(modules terraform.Modules) hcl.Diagnostics {
22+
var diags hcl.Diagnostics
23+
24+
for _, block := range modules.GetBlocks() {
25+
block := block
26+
27+
if countAttr, ok := block.Attributes()["count"]; ok {
28+
if block.IsExpanded() {
29+
continue
30+
}
31+
32+
diags = append(diags, &hcl.Diagnostic{
33+
Severity: hcl.DiagWarning,
34+
Summary: fmt.Sprintf("Unexpanded count argument on block %q", block.FullName()),
35+
Detail: "The count argument is not expanded. This may lead to unexpected behavior. The default behavior is to assume count is 1.",
36+
Subject: &countAttr.HCLAttribute().Range,
37+
Context: &block.HCLBlock().DefRange,
38+
Expression: countAttr.HCLAttribute().Expr,
39+
EvalContext: block.Context().Inner(),
40+
})
41+
}
42+
}
43+
return diags
44+
}

0 commit comments

Comments
 (0)