From a51013c3f47a8efc833080f01dc391e1020a93e9 Mon Sep 17 00:00:00 2001 From: Laura Martin Date: Fri, 6 Nov 2020 11:53:45 +0000 Subject: [PATCH] Use regex to filter filenames I ran into an issue where I have a directory of templates I use that I name `foo.tf.template`. I found that Atlantis was running against this directory and failing, which was unexpected behaviour. This is because we're just checking if `.tf` is contained within a filename, rather than checking if it's the suffix of the filename. Instead, we can use regex to ensure that we're only filtering on actual Terraform files, inclusive of `.tf` and `.tfvars` suffixes. There are obviously some alternative ways I could get around this: rename my templates to `foo.template` without the `.tf`. I also really liked the idea of a `.atlantisignore` file previously cited[1], but it was rejected in favour of explicitly defining exactly which directories to run it in. We have a large number of directories so this wasn't appealing to me. I felt this behaviour was sufficiently unexpected that it was worth making a more explicit filter. [1] https://github.com/runatlantis/atlantis/issues/26 --- server/events/project_finder.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/events/project_finder.go b/server/events/project_finder.go index 2c2dc92394..0eca4fe2f8 100644 --- a/server/events/project_finder.go +++ b/server/events/project_finder.go @@ -17,6 +17,7 @@ import ( "os" "path" "path/filepath" + "regexp" "strings" "github.com/runatlantis/atlantis/server/events/yaml/valid" @@ -147,11 +148,14 @@ func (p *DefaultProjectFinder) DetermineProjectsViaConfig(log *logging.SimpleLog // filterToTerraform filters non-terraform files from files. func (p *DefaultProjectFinder) filterToTerraform(files []string) []string { var filtered []string + fileNameRe, _ := regexp.Compile(`^.*(\.tf|\.tfvars)$`) + for _, fileName := range files { - if !p.shouldIgnore(fileName) && (strings.Contains(fileName, ".tf") || filepath.Base(fileName) == "terragrunt.hcl") { + if !p.shouldIgnore(fileName) && (fileNameRe.MatchString(fileName) || filepath.Base(fileName) == "terragrunt.hcl") { filtered = append(filtered, fileName) } } + return filtered }