@@ -8,12 +8,14 @@ import (
88 "log/slog"
99 "os"
1010 "path/filepath"
11+ "strings"
1112
1213 "github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser"
1314 "github.com/aquasecurity/trivy/pkg/log"
1415 "github.com/hashicorp/hcl/v2"
1516 "github.com/zclconf/go-cty/cty"
1617
18+ "github.com/coder/preview/tfvars"
1719 "github.com/coder/preview/types"
1820)
1921
@@ -25,6 +27,7 @@ type Input struct {
2527 PlanJSON json.RawMessage
2628 ParameterValues map [string ]string
2729 Owner types.WorkspaceOwner
30+ TFVars map [string ]cty.Value
2831}
2932
3033type Output struct {
@@ -50,6 +53,17 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
5053 }
5154 }
5255
56+ variableValues , err := tfvars .LoadTFVars (dir , varFiles )
57+ if err != nil {
58+ return nil , hcl.Diagnostics {
59+ {
60+ Severity : hcl .DiagError ,
61+ Summary : "Failed to load tfvars from files" ,
62+ Detail : err .Error (),
63+ },
64+ }
65+ }
66+
5367 planHook , err := PlanJSONHook (dir , input )
5468 if err != nil {
5569 return nil , hcl.Diagnostics {
@@ -71,17 +85,23 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
7185 },
7286 }
7387 }
74- var _ = ownerHook
88+
89+ // Override with user supplied variables
90+ for k , v := range input .TFVars {
91+ variableValues [k ] = v
92+ }
7593
7694 // moduleSource is "" for a local module
7795 p := parser .New (dir , "" ,
7896 parser .OptionStopOnHCLError (false ),
7997 parser .OptionWithDownloads (false ),
8098 parser .OptionWithSkipCachedModules (true ),
81- parser .OptionWithTFVarsPaths (varFiles ... ),
8299 parser .OptionWithEvalHook (planHook ),
83100 parser .OptionWithEvalHook (ownerHook ),
84101 parser .OptionWithEvalHook (ParameterContextsEvalHook (input )),
102+ // 'OptionsWithTfVars' cannot be set with 'OptionWithTFVarsPaths'. So load the
103+ // tfvars from the files ourselves and merge with the user-supplied tf vars.
104+ parser .OptionsWithTfVars (variableValues ),
85105 )
86106
87107 err = p .ParseFS (ctx , "." )
@@ -149,7 +169,7 @@ func tfVarFiles(path string, dir fs.FS) ([]string, error) {
149169 files = append (files , newFiles ... )
150170 }
151171
152- if filepath .Ext (entry .Name ()) == ".tfvars" {
172+ if filepath .Ext (entry .Name ()) == ".tfvars" || strings . HasSuffix ( entry . Name (), ".tfvars.json" ) {
153173 files = append (files , filepath .Join (path , entry .Name ()))
154174 }
155175 }
0 commit comments