From 0450f487fa7902563443add6cb1883102654ed19 Mon Sep 17 00:00:00 2001 From: Alex Pilon Date: Thu, 18 Jul 2019 13:07:10 -0400 Subject: [PATCH] move IsEmptyDir to configs package --- command/init.go | 5 ++--- command/providers.go | 4 ++-- config/loader.go | 15 --------------- config/loader_test.go | 30 ----------------------------- configs/parser_config_dir.go | 21 ++++++++++++++++++++ configs/parser_config_dir_test.go | 30 +++++++++++++++++++++++++++++ configs/testdata/dir-empty/.gitkeep | 0 7 files changed, 55 insertions(+), 50 deletions(-) create mode 100644 configs/testdata/dir-empty/.gitkeep diff --git a/command/init.go b/command/init.go index c4e9554df..6a7cb968e 100644 --- a/command/init.go +++ b/command/init.go @@ -15,7 +15,6 @@ import ( "github.com/hashicorp/errwrap" "github.com/hashicorp/terraform/backend" backendInit "github.com/hashicorp/terraform/backend/init" - "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/configs/configupgrade" @@ -125,7 +124,7 @@ func (c *InitCommand) Run(args []string) int { if flagFromModule != "" { src := flagFromModule - empty, err := config.IsEmptyDir(path) + empty, err := configs.IsEmptyDir(path) if err != nil { c.Ui.Error(fmt.Sprintf("Error validating destination directory: %s", err)) return 1 @@ -157,7 +156,7 @@ func (c *InitCommand) Run(args []string) int { // If our directory is empty, then we're done. We can't get or setup // the backend with an empty directory. - empty, err := config.IsEmptyDir(path) + empty, err := configs.IsEmptyDir(path) if err != nil { diags = diags.Append(fmt.Errorf("Error checking configuration: %s", err)) return 1 diff --git a/command/providers.go b/command/providers.go index dcfcadf41..6730719fa 100644 --- a/command/providers.go +++ b/command/providers.go @@ -5,7 +5,7 @@ import ( "path/filepath" "sort" - "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/moduledeps" "github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/tfdiags" @@ -46,7 +46,7 @@ func (c *ProvidersCommand) Run(args []string) int { var diags tfdiags.Diagnostics - empty, err := config.IsEmptyDir(configPath) + empty, err := configs.IsEmptyDir(configPath) if err != nil { diags = diags.Append(tfdiags.Sourceless( tfdiags.Error, diff --git a/config/loader.go b/config/loader.go index 6e3478167..612e25b9e 100644 --- a/config/loader.go +++ b/config/loader.go @@ -135,21 +135,6 @@ func LoadDir(root string) (*Config, error) { return result, nil } -// IsEmptyDir returns true if the directory given has no Terraform -// configuration files. -func IsEmptyDir(root string) (bool, error) { - if _, err := os.Stat(root); err != nil && os.IsNotExist(err) { - return true, nil - } - - fs, os, err := dirFiles(root) - if err != nil { - return false, err - } - - return len(fs) == 0 && len(os) == 0, nil -} - // Ext returns the Terraform configuration extension of the given // path, or a blank string if it is an invalid function. func ext(path string) string { diff --git a/config/loader_test.go b/config/loader_test.go index a11da589a..f151aafd1 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -12,36 +12,6 @@ func TestErrNoConfigsFound_impl(t *testing.T) { var _ error = new(ErrNoConfigsFound) } -func TestIsEmptyDir(t *testing.T) { - val, err := IsEmptyDir(fixtureDir) - if err != nil { - t.Fatalf("err: %s", err) - } - if val { - t.Fatal("should not be empty") - } -} - -func TestIsEmptyDir_noExist(t *testing.T) { - val, err := IsEmptyDir(filepath.Join(fixtureDir, "nopenopenope")) - if err != nil { - t.Fatalf("err: %s", err) - } - if !val { - t.Fatal("should be empty") - } -} - -func TestIsEmptyDir_noConfigs(t *testing.T) { - val, err := IsEmptyDir(filepath.Join(fixtureDir, "dir-empty")) - if err != nil { - t.Fatalf("err: %s", err) - } - if !val { - t.Fatal("should be empty") - } -} - func TestLoadFile_badType(t *testing.T) { _, err := LoadFile(filepath.Join(fixtureDir, "bad_type.tf.nope")) if err == nil { diff --git a/configs/parser_config_dir.go b/configs/parser_config_dir.go index 3014cb4b4..752d6d9ca 100644 --- a/configs/parser_config_dir.go +++ b/configs/parser_config_dir.go @@ -2,6 +2,7 @@ package configs import ( "fmt" + "os" "path/filepath" "strings" @@ -140,3 +141,23 @@ func IsIgnoredFile(name string) bool { strings.HasSuffix(name, "~") || // vim strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#") // emacs } + +// IsEmptyDir returns true if the given filesystem path contains no Terraform +// configuration files. +// +// Unlike the methods of the Parser type, this function always consults the +// real filesystem, and thus it isn't appropriate to use when working with +// configuration loaded from a plan file. +func IsEmptyDir(path string) (bool, error) { + if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { + return true, nil + } + + p := NewParser(nil) + fs, os, err := p.dirFiles(path) + if err != nil { + return false, err + } + + return len(fs) == 0 && len(os) == 0, nil +} diff --git a/configs/parser_config_dir_test.go b/configs/parser_config_dir_test.go index ab7312812..c3c284aa6 100644 --- a/configs/parser_config_dir_test.go +++ b/configs/parser_config_dir_test.go @@ -138,3 +138,33 @@ func TestParserLoadConfigDirFailure(t *testing.T) { } } + +func TestIsEmptyDir(t *testing.T) { + val, err := IsEmptyDir(filepath.Join("testdata", "valid-files")) + if err != nil { + t.Fatalf("err: %s", err) + } + if val { + t.Fatal("should not be empty") + } +} + +func TestIsEmptyDir_noExist(t *testing.T) { + val, err := IsEmptyDir(filepath.Join("testdata", "nopenopenope")) + if err != nil { + t.Fatalf("err: %s", err) + } + if !val { + t.Fatal("should be empty") + } +} + +func TestIsEmptyDir_noConfigs(t *testing.T) { + val, err := IsEmptyDir(filepath.Join("testdata", "dir-empty")) + if err != nil { + t.Fatalf("err: %s", err) + } + if !val { + t.Fatal("should be empty") + } +} diff --git a/configs/testdata/dir-empty/.gitkeep b/configs/testdata/dir-empty/.gitkeep new file mode 100644 index 000000000..e69de29bb