File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,7 +15,6 @@ import (
1515 "github.com/hashicorp/errwrap"
1616 "github.com/hashicorp/terraform/backend"
1717 backendInit "github.com/hashicorp/terraform/backend/init"
18- "github.com/hashicorp/terraform/config"
1918 "github.com/hashicorp/terraform/configs"
2019 "github.com/hashicorp/terraform/configs/configschema"
2120 "github.com/hashicorp/terraform/configs/configupgrade"
@@ -125,7 +124,7 @@ func (c *InitCommand) Run(args []string) int {
125124 if flagFromModule != "" {
126125 src := flagFromModule
127126
128- empty , err := config .IsEmptyDir (path )
127+ empty , err := configs .IsEmptyDir (path )
129128 if err != nil {
130129 c .Ui .Error (fmt .Sprintf ("Error validating destination directory: %s" , err ))
131130 return 1
@@ -157,7 +156,7 @@ func (c *InitCommand) Run(args []string) int {
157156
158157 // If our directory is empty, then we're done. We can't get or setup
159158 // the backend with an empty directory.
160- empty , err := config .IsEmptyDir (path )
159+ empty , err := configs .IsEmptyDir (path )
161160 if err != nil {
162161 diags = diags .Append (fmt .Errorf ("Error checking configuration: %s" , err ))
163162 return 1
Original file line number Diff line number Diff line change 55 "path/filepath"
66 "sort"
77
8- "github.com/hashicorp/terraform/config "
8+ "github.com/hashicorp/terraform/configs "
99 "github.com/hashicorp/terraform/moduledeps"
1010 "github.com/hashicorp/terraform/terraform"
1111 "github.com/hashicorp/terraform/tfdiags"
@@ -46,7 +46,7 @@ func (c *ProvidersCommand) Run(args []string) int {
4646
4747 var diags tfdiags.Diagnostics
4848
49- empty , err := config .IsEmptyDir (configPath )
49+ empty , err := configs .IsEmptyDir (configPath )
5050 if err != nil {
5151 diags = diags .Append (tfdiags .Sourceless (
5252 tfdiags .Error ,
Original file line number Diff line number Diff line change @@ -135,21 +135,6 @@ func LoadDir(root string) (*Config, error) {
135135 return result , nil
136136}
137137
138- // IsEmptyDir returns true if the directory given has no Terraform
139- // configuration files.
140- func IsEmptyDir (root string ) (bool , error ) {
141- if _ , err := os .Stat (root ); err != nil && os .IsNotExist (err ) {
142- return true , nil
143- }
144-
145- fs , os , err := dirFiles (root )
146- if err != nil {
147- return false , err
148- }
149-
150- return len (fs ) == 0 && len (os ) == 0 , nil
151- }
152-
153138// Ext returns the Terraform configuration extension of the given
154139// path, or a blank string if it is an invalid function.
155140func ext (path string ) string {
Original file line number Diff line number Diff line change @@ -12,36 +12,6 @@ func TestErrNoConfigsFound_impl(t *testing.T) {
1212 var _ error = new (ErrNoConfigsFound )
1313}
1414
15- func TestIsEmptyDir (t * testing.T ) {
16- val , err := IsEmptyDir (fixtureDir )
17- if err != nil {
18- t .Fatalf ("err: %s" , err )
19- }
20- if val {
21- t .Fatal ("should not be empty" )
22- }
23- }
24-
25- func TestIsEmptyDir_noExist (t * testing.T ) {
26- val , err := IsEmptyDir (filepath .Join (fixtureDir , "nopenopenope" ))
27- if err != nil {
28- t .Fatalf ("err: %s" , err )
29- }
30- if ! val {
31- t .Fatal ("should be empty" )
32- }
33- }
34-
35- func TestIsEmptyDir_noConfigs (t * testing.T ) {
36- val , err := IsEmptyDir (filepath .Join (fixtureDir , "dir-empty" ))
37- if err != nil {
38- t .Fatalf ("err: %s" , err )
39- }
40- if ! val {
41- t .Fatal ("should be empty" )
42- }
43- }
44-
4515func TestLoadFile_badType (t * testing.T ) {
4616 _ , err := LoadFile (filepath .Join (fixtureDir , "bad_type.tf.nope" ))
4717 if err == nil {
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package configs
22
33import (
44 "fmt"
5+ "os"
56 "path/filepath"
67 "strings"
78
@@ -140,3 +141,23 @@ func IsIgnoredFile(name string) bool {
140141 strings .HasSuffix (name , "~" ) || // vim
141142 strings .HasPrefix (name , "#" ) && strings .HasSuffix (name , "#" ) // emacs
142143}
144+
145+ // IsEmptyDir returns true if the given filesystem path contains no Terraform
146+ // configuration files.
147+ //
148+ // Unlike the methods of the Parser type, this function always consults the
149+ // real filesystem, and thus it isn't appropriate to use when working with
150+ // configuration loaded from a plan file.
151+ func IsEmptyDir (path string ) (bool , error ) {
152+ if _ , err := os .Stat (path ); err != nil && os .IsNotExist (err ) {
153+ return true , nil
154+ }
155+
156+ p := NewParser (nil )
157+ fs , os , err := p .dirFiles (path )
158+ if err != nil {
159+ return false , err
160+ }
161+
162+ return len (fs ) == 0 && len (os ) == 0 , nil
163+ }
Original file line number Diff line number Diff line change @@ -138,3 +138,33 @@ func TestParserLoadConfigDirFailure(t *testing.T) {
138138 }
139139
140140}
141+
142+ func TestIsEmptyDir (t * testing.T ) {
143+ val , err := IsEmptyDir (filepath .Join ("testdata" , "valid-files" ))
144+ if err != nil {
145+ t .Fatalf ("err: %s" , err )
146+ }
147+ if val {
148+ t .Fatal ("should not be empty" )
149+ }
150+ }
151+
152+ func TestIsEmptyDir_noExist (t * testing.T ) {
153+ val , err := IsEmptyDir (filepath .Join ("testdata" , "nopenopenope" ))
154+ if err != nil {
155+ t .Fatalf ("err: %s" , err )
156+ }
157+ if ! val {
158+ t .Fatal ("should be empty" )
159+ }
160+ }
161+
162+ func TestIsEmptyDir_noConfigs (t * testing.T ) {
163+ val , err := IsEmptyDir (filepath .Join ("testdata" , "dir-empty" ))
164+ if err != nil {
165+ t .Fatalf ("err: %s" , err )
166+ }
167+ if ! val {
168+ t .Fatal ("should be empty" )
169+ }
170+ }
Original file line number Diff line number Diff line change @@ -216,8 +216,6 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
216216github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws =
217217github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f /go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w =
218218github.com/hashicorp/hcl2 v0.0.0-20181208003705-670926858200 /go.mod h1:ShfpTh661oAaxo7VcNxg0zcZW6jvMa7Moy2oFx7e5dE =
219- github.com/hashicorp/hcl2 v0.0.0-20190618163856-0b64543c968c h1:P96avlEdjyi6kpx6kTbTbuQb5GuZvVTrLK9FWKwTy6A =
220- github.com/hashicorp/hcl2 v0.0.0-20190618163856-0b64543c968c /go.mod h1:FSQTwDi9qesxGBsII2VqhIzKQ4r0bHvBkOczWfD7llg =
221219github.com/hashicorp/hcl2 v0.0.0-20190702185634-5b39d9ff3a9a h1:1KfDwkIXrxrfMpqwuW//ujObiYNuR2DqaETSK2NB8Ug =
222220github.com/hashicorp/hcl2 v0.0.0-20190702185634-5b39d9ff3a9a /go.mod h1:FSQTwDi9qesxGBsII2VqhIzKQ4r0bHvBkOczWfD7llg =
223221github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 h1:2yzhWGdgQUWZUCNK+AoO35V+HTsgEmcM4J9IkArh7PI =
You can’t perform that action at this time.
0 commit comments