Skip to content

feat(provisionersdk): add support for .tf.json templates #7744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions provisionersdk/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ const (
TemplateArchiveLimit = 1 << 20
)

func dirHasExt(dir string, ext string) (bool, error) {
func dirHasExt(dir string, exts ...string) (bool, error) {
dirEnts, err := os.ReadDir(dir)
if err != nil {
return false, err
}

for _, fi := range dirEnts {
if strings.HasSuffix(fi.Name(), ext) {
return true, nil
for _, ext := range exts {
if strings.HasSuffix(fi.Name(), ext) {
return true, nil
}
}
}

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

const tfExt = ".tf"
hasTf, err := dirHasExt(directory, tfExt)
tfExts := []string{".tf", ".tf.json"}
hasTf, err := dirHasExt(directory, tfExts...)
if err != nil {
return err
}
Expand All @@ -50,7 +52,7 @@ func Tar(w io.Writer, directory string, limit int64) error {
// useless.
return xerrors.Errorf(
"%s is not a valid template since it has no %s files",
absPath, tfExt,
absPath, tfExts,
)
}

Expand Down
9 changes: 9 additions & 0 deletions provisionersdk/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func TestTar(t *testing.T) {
err = provisionersdk.Tar(io.Discard, dir, 1024)
require.NoError(t, err)
})
t.Run("ValidJSON", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
file, err := os.CreateTemp(dir, "*.tf.json")
require.NoError(t, err)
_ = file.Close()
err = provisionersdk.Tar(io.Discard, dir, 1024)
require.NoError(t, err)
})
t.Run("HiddenFiles", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
Expand Down