Skip to content

Commit dfd2734

Browse files
committed
add some debugging tools
1 parent efb7bfe commit dfd2734

File tree

12 files changed

+242
-149
lines changed

12 files changed

+242
-149
lines changed

cli/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package cli
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
57
"os"
68
"strings"
79

810
"github.com/hashicorp/hcl/v2"
911
"github.com/hashicorp/hcl/v2/hclsyntax"
12+
ctyjson "github.com/zclconf/go-cty/cty/json"
1013

1114
"github.com/coder/preview"
1215
"github.com/coder/preview/cli/clidisplay"
@@ -102,6 +105,14 @@ func (r *RootCmd) Root() *serpent.Command {
102105

103106
clidisplay.Parameters(os.Stdout, output.Parameters, output.Files)
104107

108+
if !output.ModuleOutput.IsNull() && !(output.ModuleOutput.Type().IsObjectType() && output.ModuleOutput.LengthInt() == 0) {
109+
fmt.Println("Module output")
110+
data, _ := ctyjson.Marshal(output.ModuleOutput, output.ModuleOutput.Type())
111+
var buf bytes.Buffer
112+
_ = json.Indent(&buf, data, "", " ")
113+
fmt.Println(buf.String())
114+
}
115+
105116
return nil
106117
},
107118
}

hclext/references.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,41 @@ import (
55
"strings"
66

77
"github.com/hashicorp/hcl/v2"
8+
"github.com/hashicorp/hcl/v2/hclsyntax"
89
"github.com/zclconf/go-cty/cty"
910
)
1011

12+
type ReferenceBuilder []hcl.Traverser
13+
14+
func NewReferenceBuilder(path ...string) *ReferenceBuilder {
15+
return (&ReferenceBuilder{}).AddPath(path...)
16+
}
17+
18+
func (b *ReferenceBuilder) AddPath(path ...string) *ReferenceBuilder {
19+
for _, p := range path {
20+
if len(*b) == 0 {
21+
*b = append(*b, hcl.TraverseRoot{Name: p})
22+
} else {
23+
*b = append(*b, hcl.TraverseAttr{Name: p})
24+
}
25+
}
26+
return b
27+
}
28+
29+
func (b *ReferenceBuilder) AddIndex(idx int) *ReferenceBuilder {
30+
*b = append(*b, hcl.TraverseIndex{Key: cty.NumberIntVal(int64(idx))})
31+
return b
32+
}
33+
34+
func (b *ReferenceBuilder) AddKey(key string) *ReferenceBuilder {
35+
*b = append(*b, hcl.TraverseIndex{Key: cty.StringVal(key)})
36+
return b
37+
}
38+
39+
func (b ReferenceBuilder) Expression() hcl.Expression {
40+
return &hclsyntax.ScopeTraversalExpr{Traversal: hcl.Traversal(b)}
41+
}
42+
1143
func ReferenceNames(exp hcl.Expression) []string {
1244
if exp == nil {
1345
return []string{}

preview.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser"
1212
"github.com/aquasecurity/trivy/pkg/log"
1313
"github.com/hashicorp/hcl/v2"
14+
"github.com/zclconf/go-cty/cty"
1415

1516
"github.com/coder/preview/types"
1617
)
@@ -22,6 +23,7 @@ type Input struct {
2223
}
2324

2425
type Output struct {
26+
ModuleOutput cty.Value
2527
Parameters []types.Parameter
2628
WorkspaceTags types.TagBlocks
2729
Files map[string]*hcl.File
@@ -99,12 +101,11 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
99101
}
100102
}
101103

102-
var _ = outputs
103-
104104
diags := make(hcl.Diagnostics, 0)
105105
rp, rpDiags := RichParameters(modules)
106106
tags, tagDiags := WorkspaceTags(modules, p.Files())
107107
return &Output{
108+
ModuleOutput: outputs,
108109
Parameters: rp,
109110
WorkspaceTags: tags,
110111
Files: p.Files(),

preview_test.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,32 @@ func Test_Extract(t *testing.T) {
271271
params: map[string]assertParam{},
272272
},
273273
{
274-
skip: "Broken until https://github.com/aquasecurity/trivy/pull/8479 fixed",
275274
name: "demo",
276275
dir: "demo",
277276
expTags: map[string]string{
278277
"cluster": "confidential",
278+
"hash": "52bb4d943694f2f5867a251780f85e5a68906787b4ffa3157e29b9ef510b1a97",
279279
},
280280
input: preview.Input{
281-
PlanJSONPath: "",
282-
ParameterValues: map[string]string{},
281+
PlanJSONPath: "plan.json",
282+
ParameterValues: map[string]string{
283+
"hash": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
284+
},
283285
Owner: types.WorkspaceOwner{
284286
Groups: []string{"admin"},
285287
},
286288
},
287289
unknownTags: []string{},
288-
params: map[string]assertParam{},
290+
params: map[string]assertParam{
291+
"hash": ap().
292+
value("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"),
293+
"security_level": ap(),
294+
"region": ap(),
295+
"cpu": ap(),
296+
"browser": ap(),
297+
"team": ap().optVals("frontend", "backend", "fullstack"),
298+
"jetbrains_ide": ap(),
299+
},
289300
},
290301
{
291302
name: "demo_flat",
@@ -378,15 +389,25 @@ func Test_Extract(t *testing.T) {
378389
input: preview.Input{
379390
PlanJSONPath: "",
380391
ParameterValues: map[string]string{
381-
"first": "curse",
392+
"first": "curs",
382393
},
383394
Owner: types.WorkspaceOwner{},
384395
},
385396
unknownTags: []string{},
386397
params: map[string]assertParam{
387-
"first": ap().value("curse"),
398+
"first": ap().value("curs"),
388399
},
389400
},
401+
{
402+
name: "submodcount",
403+
dir: "submodcount",
404+
expTags: map[string]string{},
405+
input: preview.Input{
406+
ParameterValues: map[string]string{},
407+
},
408+
unknownTags: []string{},
409+
params: map[string]assertParam{},
410+
},
390411
} {
391412
t.Run(tc.name, func(t *testing.T) {
392413
t.Parallel()

testdata/demo/parameters.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
data "coder_parameter" "team" {
2-
name = "Team"
2+
name = "team"
3+
display_name = "Team"
34
description = "Which team are you on?"
45
type = "string"
56
default = "fullstack"
@@ -96,7 +97,8 @@ data "coder_workspace_tags" "test" {
9697
// Advanced admin parameter
9798
data "coder_parameter" "image_hash" {
9899
count = local.isAdmin ? 1 : 0
99-
name = "Image Hash"
100+
name = "hash"
101+
display_name = "Image Hash"
100102
description = "Override the hash of the image to use. Only available to admins."
101103
// Value can get stale
102104
default = trimprefix(data.docker_registry_image.coder.sha256_digest, "sha256:")
@@ -113,7 +115,7 @@ data "docker_registry_image" "coder" {
113115
}
114116

115117
data "coder_parameter" "region" {
116-
name = "Region"
118+
name = "region"
117119
display_name = "Region"
118120
description = "What region are you in?"
119121
default = local.default_region

0 commit comments

Comments
 (0)