From 8353deb9a7f28bf04e7c85805b0f04f2a4f90fbc Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Thu, 19 Sep 2019 22:18:27 +0900 Subject: [PATCH] Skip hidden directories such as .terraform or .git --- tfupdate/file.go | 7 ++++--- tfupdate/file_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/tfupdate/file.go b/tfupdate/file.go index 4958a1f..2bb2918 100644 --- a/tfupdate/file.go +++ b/tfupdate/file.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/hashicorp/hcl2/hclwrite" "github.com/spf13/afero" @@ -43,7 +44,7 @@ func UpdateFile(fs afero.Fs, filename string, o Option) error { // UpdateDir updates version constraints for files in a given directory. // If a recursive flag is true, it checks and updates recursively. -// It skips a .terraform directory because it may contain module code. +// skip hidden directories such as .terraform or .git. // It also skips a file without .tf extension. func UpdateDir(fs afero.Fs, dirname string, recursive bool, o Option) error { dir, err := afero.ReadDir(fs, dirname) @@ -60,8 +61,8 @@ func UpdateDir(fs afero.Fs, dirname string, recursive bool, o Option) error { // skip directory if a recursive flag is false continue } - if entry.Name() == ".terraform" { - // skip a .terraform directory because it may contain module code. + if strings.HasPrefix(entry.Name(), ".") { + // skip hidden directories such as .terraform or .git continue } diff --git a/tfupdate/file_test.go b/tfupdate/file_test.go index c15b273..e881a31 100644 --- a/tfupdate/file_test.go +++ b/tfupdate/file_test.go @@ -268,6 +268,38 @@ terraform { provider "aws" { version = "2.11.0" } +`, + }, + { + rootdir: "a", + subdir: ".git", + filename1: "terraform.tf", + src1: ` +terraform { + required_version = "0.12.6" +} +`, + filename2: "provider.tf", + src2: ` +provider "aws" { + version = "2.11.0" +} +`, + checkdir: "a", + recursive: true, + o: Option{ + updateType: "terraform", + target: "0.12.7", + }, + want1: ` +terraform { + required_version = "0.12.6" +} +`, + want2: ` +provider "aws" { + version = "2.11.0" +} `, }, {