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

This commit is contained in:
Colin Adler
2023-06-02 00:03:59 +00:00
committed by GitHub
parent 9dbbe82cf8
commit 9b8e5c2d8a
2 changed files with 17 additions and 6 deletions
+8 -6
View File
@@ -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
}
}
}
@@ -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
}
@@ -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,
)
}
+9
View File
@@ -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()