From b40b7ce01a25214f6bbb5e324049b5f6f33b23c9 Mon Sep 17 00:00:00 2001 From: Kushal Pisavadia Date: Tue, 9 Dec 2014 13:11:49 +0000 Subject: [PATCH] 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. --- config/loader.go | 11 +++++++++- config/loader_test.go | 7 +++++++ .../dir-temporary-files/#emacs-two.tf# | 20 +++++++++++++++++++ .../dir-temporary-files/.#emacs-one.tf | 17 ++++++++++++++++ .../dir-temporary-files/vim-one.tf~ | 17 ++++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 config/test-fixtures/dir-temporary-files/#emacs-two.tf# create mode 100644 config/test-fixtures/dir-temporary-files/.#emacs-one.tf create mode 100644 config/test-fixtures/dir-temporary-files/vim-one.tf~ diff --git a/config/loader.go b/config/loader.go index 740d0ec5f..a1bd196d1 100644 --- a/config/loader.go +++ b/config/loader.go @@ -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 +} diff --git a/config/loader_test.go b/config/loader_test.go index 5bb77b931..e43b5a256 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -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 diff --git a/config/test-fixtures/dir-temporary-files/#emacs-two.tf# b/config/test-fixtures/dir-temporary-files/#emacs-two.tf# new file mode 100644 index 000000000..acbb4f2f9 --- /dev/null +++ b/config/test-fixtures/dir-temporary-files/#emacs-two.tf# @@ -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" + } +} diff --git a/config/test-fixtures/dir-temporary-files/.#emacs-one.tf b/config/test-fixtures/dir-temporary-files/.#emacs-one.tf new file mode 100644 index 000000000..1e049a87f --- /dev/null +++ b/config/test-fixtures/dir-temporary-files/.#emacs-one.tf @@ -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}" +} diff --git a/config/test-fixtures/dir-temporary-files/vim-one.tf~ b/config/test-fixtures/dir-temporary-files/vim-one.tf~ new file mode 100644 index 000000000..1e049a87f --- /dev/null +++ b/config/test-fixtures/dir-temporary-files/vim-one.tf~ @@ -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}" +}