@@ -8,12 +8,14 @@ import (
8
8
"log/slog"
9
9
"os"
10
10
"path/filepath"
11
+ "strings"
11
12
12
13
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser"
13
14
"github.com/aquasecurity/trivy/pkg/log"
14
15
"github.com/hashicorp/hcl/v2"
15
16
"github.com/zclconf/go-cty/cty"
16
17
18
+ "github.com/coder/preview/tfvars"
17
19
"github.com/coder/preview/types"
18
20
)
19
21
@@ -25,6 +27,7 @@ type Input struct {
25
27
PlanJSON json.RawMessage
26
28
ParameterValues map [string ]string
27
29
Owner types.WorkspaceOwner
30
+ TFVars map [string ]cty.Value
28
31
}
29
32
30
33
type Output struct {
@@ -50,6 +53,17 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
50
53
}
51
54
}
52
55
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
+
53
67
planHook , err := PlanJSONHook (dir , input )
54
68
if err != nil {
55
69
return nil , hcl.Diagnostics {
@@ -71,17 +85,23 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
71
85
},
72
86
}
73
87
}
74
- var _ = ownerHook
88
+
89
+ // Override with user supplied variables
90
+ for k , v := range input .TFVars {
91
+ variableValues [k ] = v
92
+ }
75
93
76
94
// moduleSource is "" for a local module
77
95
p := parser .New (dir , "" ,
78
96
parser .OptionStopOnHCLError (false ),
79
97
parser .OptionWithDownloads (false ),
80
98
parser .OptionWithSkipCachedModules (true ),
81
- parser .OptionWithTFVarsPaths (varFiles ... ),
82
99
parser .OptionWithEvalHook (planHook ),
83
100
parser .OptionWithEvalHook (ownerHook ),
84
101
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 ),
85
105
)
86
106
87
107
err = p .ParseFS (ctx , "." )
@@ -149,7 +169,7 @@ func tfVarFiles(path string, dir fs.FS) ([]string, error) {
149
169
files = append (files , newFiles ... )
150
170
}
151
171
152
- if filepath .Ext (entry .Name ()) == ".tfvars" {
172
+ if filepath .Ext (entry .Name ()) == ".tfvars" || strings . HasSuffix ( entry . Name (), ".tfvars.json" ) {
153
173
files = append (files , filepath .Join (path , entry .Name ()))
154
174
}
155
175
}
0 commit comments