Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 0450f48

Browse files
committed
move IsEmptyDir to configs package
1 parent cc97974 commit 0450f48

7 files changed

Lines changed: 55 additions & 50 deletions

File tree

command/init.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff 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

command/providers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
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,

config/loader.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff 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.
155140
func ext(path string) string {

config/loader_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff 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-
4515
func TestLoadFile_badType(t *testing.T) {
4616
_, err := LoadFile(filepath.Join(fixtureDir, "bad_type.tf.nope"))
4717
if err == nil {

configs/parser_config_dir.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package configs
22

33
import (
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+
}

configs/parser_config_dir_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

configs/testdata/dir-empty/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)