Do not read temporary editor files, fixes #548

This fixes a bug where Terraform would error with the following:

```
Error loading config: Error reading
/Users/rhenrichs/work/example/.#example.tf: open
/Users/rhenrichs/work/example/.#example.tf: no such file or directory
```

The solution implemented here ignores the common emacs and vim
temporary file formats.

Note: the potential danger with merging this is that Terraform could
quickly have requests to ignore other file formats.
This commit is contained in:
Kushal Pisavadia 2014-12-09 13:11:49 +00:00 committed by Kushal Pisavadia
parent f1cb297c59
commit b40b7ce01a
5 changed files with 71 additions and 1 deletions

View File

@ -162,7 +162,7 @@ func dirFiles(dir string) ([]string, []string, error) {
// Only care about files that are valid to load
name := fi.Name()
extValue := ext(name)
if extValue == "" {
if extValue == "" || isTemporaryFile(name) {
continue
}
@ -182,3 +182,12 @@ func dirFiles(dir string) ([]string, []string, error) {
return files, overrides, nil
}
// isTemporaryFile returns true or false depending on whether the
// provided file name is a temporary file for the following editors:
// emacs or vim.
func isTemporaryFile(name string) bool {
return strings.HasSuffix(name, "~") || // vim
strings.HasPrefix(name, ".#") || // emacs
(strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#")) // emacs
}

View File

@ -383,6 +383,13 @@ func TestLoad_createBeforeDestroy(t *testing.T) {
}
}
func TestLoad_temporary_files(t *testing.T) {
_, err := LoadDir(filepath.Join(fixtureDir, "dir-temporary-files"))
if err == nil {
t.Fatalf("Expected to see an error stating no config files found")
}
}
const basicOutputsStr = `
web_ip
vars

View File

@ -0,0 +1,20 @@
provider "do" {
api_key = "${var.foo}"
}
resource "aws_security_group" "firewall" {
count = 5
}
resource aws_instance "web" {
ami = "${var.foo}"
security_groups = [
"foo",
"${aws_security_group.firewall.foo}"
]
network_interface {
device_index = 0
description = "Main network interface"
}
}

View File

@ -0,0 +1,17 @@
variable "foo" {
default = "bar"
description = "bar"
}
provider "aws" {
access_key = "foo"
secret_key = "bar"
}
resource "aws_instance" "db" {
security_groups = "${aws_security_group.firewall.*.id}"
}
output "web_ip" {
value = "${aws_instance.web.private_ip}"
}

View File

@ -0,0 +1,17 @@
variable "foo" {
default = "bar"
description = "bar"
}
provider "aws" {
access_key = "foo"
secret_key = "bar"
}
resource "aws_instance" "db" {
security_groups = "${aws_security_group.firewall.*.id}"
}
output "web_ip" {
value = "${aws_instance.web.private_ip}"
}