4
4
"archive/tar"
5
5
"bytes"
6
6
"io"
7
+ "io/ioutil"
7
8
"os"
8
9
"path/filepath"
9
10
"strings"
@@ -16,12 +17,47 @@ const (
16
17
TemplateArchiveLimit = 1 << 20
17
18
)
18
19
19
- // Tar archives a directory.
20
+ func dirHasExt (dir string , ext string ) (bool , error ) {
21
+ dirEnts , err := ioutil .ReadDir (dir )
22
+ if err != nil {
23
+ return false , err
24
+ }
25
+
26
+ for _ , fi := range dirEnts {
27
+ if strings .HasSuffix (fi .Name (), ext ) {
28
+ return true , nil
29
+ }
30
+ }
31
+
32
+ return false , nil
33
+ }
34
+
35
+ // Tar archives a Terraform directory.
20
36
func Tar (directory string , limit int64 ) ([]byte , error ) {
21
37
var buffer bytes.Buffer
22
38
tarWriter := tar .NewWriter (& buffer )
23
39
totalSize := int64 (0 )
24
- err := filepath .Walk (directory , func (file string , fileInfo os.FileInfo , err error ) error {
40
+
41
+ const tfExt = ".tf"
42
+ hasTf , err := dirHasExt (directory , tfExt )
43
+ if err != nil {
44
+ return nil , err
45
+ }
46
+ if ! hasTf {
47
+ absPath , err := filepath .Abs (directory )
48
+ if err != nil {
49
+ return nil , err
50
+ }
51
+
52
+ // Show absolute path to aid in debugging. E.g. showing "." is
53
+ // useless.
54
+ return nil , xerrors .Errorf (
55
+ "%s is not a valid template since it has no %s files" ,
56
+ absPath , tfExt ,
57
+ )
58
+ }
59
+
60
+ err = filepath .Walk (directory , func (file string , fileInfo os.FileInfo , err error ) error {
25
61
if err != nil {
26
62
return err
27
63
}
0 commit comments