mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
Merge pull request #22124 from hashicorp/appilon/move-IsEmptyDir
[SDK cleanup] Move IsEmptyDir to configs pkg
This commit is contained in:
commit
30fd65497a
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
0
configs/testdata/dir-empty/.gitkeep
vendored
Normal file
0
configs/testdata/dir-empty/.gitkeep
vendored
Normal file
2
go.sum
2
go.sum
@ -216,8 +216,6 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
|
||||
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws=
|
||||
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
|
||||
github.com/hashicorp/hcl2 v0.0.0-20181208003705-670926858200/go.mod h1:ShfpTh661oAaxo7VcNxg0zcZW6jvMa7Moy2oFx7e5dE=
|
||||
github.com/hashicorp/hcl2 v0.0.0-20190618163856-0b64543c968c h1:P96avlEdjyi6kpx6kTbTbuQb5GuZvVTrLK9FWKwTy6A=
|
||||
github.com/hashicorp/hcl2 v0.0.0-20190618163856-0b64543c968c/go.mod h1:FSQTwDi9qesxGBsII2VqhIzKQ4r0bHvBkOczWfD7llg=
|
||||
github.com/hashicorp/hcl2 v0.0.0-20190702185634-5b39d9ff3a9a h1:1KfDwkIXrxrfMpqwuW//ujObiYNuR2DqaETSK2NB8Ug=
|
||||
github.com/hashicorp/hcl2 v0.0.0-20190702185634-5b39d9ff3a9a/go.mod h1:FSQTwDi9qesxGBsII2VqhIzKQ4r0bHvBkOczWfD7llg=
|
||||
github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 h1:2yzhWGdgQUWZUCNK+AoO35V+HTsgEmcM4J9IkArh7PI=
|
||||
|
Loading…
Reference in New Issue
Block a user