Skip to content

Commit 90d7e1b

Browse files
committed
feat(provisionersdk,provisioner): add support for .tf.json templates
1 parent f14f011 commit 90d7e1b

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

provisioner/terraform/parameters.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ func rawRichParameterNames(workdir string) ([]string, error) {
2727

2828
var coderParameterNames []string
2929
for _, entry := range entries {
30-
if !strings.HasSuffix(entry.Name(), ".tf") {
30+
if !strings.HasSuffix(entry.Name(), ".tf") || !strings.HasSuffix(entry.Name(), ".tf.json") {
3131
continue
3232
}
3333

3434
hclFilepath := path.Join(workdir, entry.Name())
3535
parser := hclparse.NewParser()
36-
parsedHCL, diags := parser.ParseHCLFile(hclFilepath)
36+
if strings.HasSuffix(entry.Name(), ".tf.json") {
37+
parsedHCL, diags := parser.ParseJSONFile(hclFilepath)
38+
} else {
39+
parsedHCL, diags := parser.ParseHCLFile(hclFilepath)
40+
}
3741
if diags.HasErrors() {
3842
return nil, hcl.Diagnostics{
3943
{

provisioner/terraform/parse.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func loadEnabledFeatures(moduleDir string) (map[string]bool, hcl.Diagnostics) {
103103

104104
var found bool
105105
for _, entry := range entries {
106-
if !strings.HasSuffix(entry.Name(), ".tf") {
106+
if !strings.HasSuffix(entry.Name(), ".tf") || !strings.HasSuffix(entry.Name(), ".tf.json") {
107107
continue
108108
}
109109

@@ -131,7 +131,11 @@ func parseFeatures(hclFilepath string) (map[string]bool, bool, hcl.Diagnostics)
131131
}
132132

133133
parser := hclparse.NewParser()
134-
parsedHCL, diags := parser.ParseHCLFile(hclFilepath)
134+
if strings.HasSuffix(hclFilepath, ".tf.json") {
135+
parsedHCL, diags := parser.ParseJSONFile(hclFilepath)
136+
} else {
137+
parsedHCL, diags := parser.ParseHCLFile(hclFilepath)
138+
}
135139
if diags.HasErrors() {
136140
return flags, false, diags
137141
}

provisionersdk/archive.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ const (
1515
TemplateArchiveLimit = 1 << 20
1616
)
1717

18-
func dirHasExt(dir string, ext string) (bool, error) {
18+
func dirHasExt(dir string, exts ...string) (bool, error) {
1919
dirEnts, err := os.ReadDir(dir)
2020
if err != nil {
2121
return false, err
2222
}
2323

2424
for _, fi := range dirEnts {
25-
if strings.HasSuffix(fi.Name(), ext) {
26-
return true, nil
25+
for _, ext := range exts {
26+
if strings.HasSuffix(fi.Name(), ext) {
27+
return true, nil
28+
}
2729
}
2830
}
2931

@@ -35,8 +37,8 @@ func Tar(w io.Writer, directory string, limit int64) error {
3537
tarWriter := tar.NewWriter(w)
3638
totalSize := int64(0)
3739

38-
const tfExt = ".tf"
39-
hasTf, err := dirHasExt(directory, tfExt)
40+
tfExts := []string{".tf", ".tf.json"}
41+
hasTf, err := dirHasExt(directory, tfExts...)
4042
if err != nil {
4143
return err
4244
}
@@ -50,7 +52,7 @@ func Tar(w io.Writer, directory string, limit int64) error {
5052
// useless.
5153
return xerrors.Errorf(
5254
"%s is not a valid template since it has no %s files",
53-
absPath, tfExt,
55+
absPath, tfExts,
5456
)
5557
}
5658

provisionersdk/archive_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ func TestTar(t *testing.T) {
3333
err = provisionersdk.Tar(io.Discard, dir, 1024)
3434
require.NoError(t, err)
3535
})
36+
t.Run("ValidJSON", func(t *testing.T) {
37+
t.Parallel()
38+
dir := t.TempDir()
39+
file, err := os.CreateTemp(dir, "*.tf.json")
40+
require.NoError(t, err)
41+
_ = file.Close()
42+
err = provisionersdk.Tar(io.Discard, dir, 1024)
43+
require.NoError(t, err)
44+
})
3645
t.Run("HiddenFiles", func(t *testing.T) {
3746
t.Parallel()
3847
dir := t.TempDir()

0 commit comments

Comments
 (0)