@@ -5,12 +5,9 @@ import (
55 "encoding/json"
66 "fmt"
77 "io/fs"
8- "log/slog"
9- "os"
108 "path/filepath"
119
1210 "github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser"
13- "github.com/aquasecurity/trivy/pkg/log"
1411 "github.com/hashicorp/hcl/v2"
1512 "github.com/zclconf/go-cty/cty"
1613
@@ -28,16 +25,40 @@ type Input struct {
2825}
2926
3027type Output struct {
31- ModuleOutput cty.Value
28+ // ModuleOutput is any 'output' values from the terraform files. This has 0
29+ // effect on the parameters, tags, etc. It can be helpful for debugging, as it
30+ // allows exporting some terraform values to the caller to review.
31+ ModuleOutput cty.Value
32+
3233 Parameters []types.Parameter
3334 WorkspaceTags types.TagBlocks
34- Files map [string ]* hcl.File
35+ // Files is included for printing diagnostics.
36+ // TODO: Is the memory impact of this too much? Should we render diagnostic source code
37+ // into the diagnostics up front? and remove this?
38+ Files map [string ]* hcl.File
3539}
3640
37- func Preview (ctx context.Context , input Input , dir fs.FS ) (* Output , hcl.Diagnostics ) {
38- // TODO: FIX LOGGING
39- slog .SetLogLoggerLevel (slog .LevelDebug )
40- slog .SetDefault (slog .New (log .NewHandler (os .Stderr , nil )))
41+ func Preview (ctx context.Context , input Input , dir fs.FS ) (output * Output , diagnostics hcl.Diagnostics ) {
42+ // The trivy package works with `github.com/zclconf/go-cty`. This package is
43+ // similar to `reflect` in its usage. This package can panic if types are
44+ // misused. To protect the caller, a general `recover` is used to catch any
45+ // mistakes. If this happens, there is a developer bug that needs to be resolved.
46+ defer func () {
47+ if r := recover (); r != nil {
48+ diagnostics = hcl.Diagnostics {
49+ {
50+ Severity : hcl .DiagError ,
51+ Summary : "Panic occurred in preview. This should not happen, please report this to Coder." ,
52+ Detail : fmt .Sprintf ("panic in preview: %+v" , r ),
53+ },
54+ }
55+ }
56+ }()
57+
58+ // TODO: Fix logging. There is no way to pass in an instanced logger to
59+ // the parser.
60+ //slog.SetLogLoggerLevel(slog.LevelDebug)
61+ //slog.SetDefault(slog.New(log.NewHandler(os.Stderr, nil)))
4162
4263 varFiles , err := tfVarFiles ("" , dir )
4364 if err != nil {
@@ -50,7 +71,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
5071 }
5172 }
5273
53- planHook , err := PlanJSONHook (dir , input )
74+ planHook , err := planJSONHook (dir , input )
5475 if err != nil {
5576 return nil , hcl.Diagnostics {
5677 {
@@ -61,7 +82,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
6182 }
6283 }
6384
64- ownerHook , err := WorkspaceOwnerHook (dir , input )
85+ ownerHook , err := workspaceOwnerHook (dir , input )
6586 if err != nil {
6687 return nil , hcl.Diagnostics {
6788 {
@@ -81,7 +102,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
81102 parser .OptionWithTFVarsPaths (varFiles ... ),
82103 parser .OptionWithEvalHook (planHook ),
83104 parser .OptionWithEvalHook (ownerHook ),
84- parser .OptionWithEvalHook (ParameterContextsEvalHook (input )),
105+ parser .OptionWithEvalHook (parameterContextsEvalHook (input )),
85106 )
86107
87108 err = p .ParseFS (ctx , "." )
@@ -107,8 +128,8 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
107128 }
108129
109130 diags := make (hcl.Diagnostics , 0 )
110- rp , rpDiags := RichParameters (modules )
111- tags , tagDiags := WorkspaceTags (modules , p .Files ())
131+ rp , rpDiags := parameters (modules )
132+ tags , tagDiags := workspaceTags (modules , p .Files ())
112133
113134 // Add warnings
114135 diags = diags .Extend (warnings (modules ))
0 commit comments