From 279c2029ab0a80a0e6c36485bb203389cb033ed7 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Thu, 26 Sep 2019 17:36:51 -0400 Subject: [PATCH 01/11] Add support for .terraformignore feature --- slug.go | 34 ++-- slug_test.go | 12 ++ terraformignore.go | 247 ++++++++++++++++++++++++++ terraformignore_test.go | 114 ++++++++++++ testdata/archive-dir/.terraformignore | 20 +++ testdata/archive-dir/baz.txt | 1 + 6 files changed, 409 insertions(+), 19 deletions(-) create mode 100644 terraformignore.go create mode 100644 terraformignore_test.go create mode 100644 testdata/archive-dir/.terraformignore create mode 100644 testdata/archive-dir/baz.txt diff --git a/slug.go b/slug.go index 8dd4078..29b9919 100644 --- a/slug.go +++ b/slug.go @@ -33,11 +33,15 @@ func Pack(src string, w io.Writer, dereference bool) (*Meta, error) { // Tar the file contents. tarW := tar.NewWriter(gzipW) + // Load the ignore rule configuration, which will use + // defaults if no .terraformignore is configured + ignoreRules := parseIgnoreFile(src) + // Track the metadata details as we go. meta := &Meta{} // Walk the tree of files. - err := filepath.Walk(src, packWalkFn(src, src, src, tarW, meta, dereference)) + err := filepath.Walk(src, packWalkFn(src, src, src, tarW, meta, dereference, ignoreRules)) if err != nil { return nil, err } @@ -55,17 +59,12 @@ func Pack(src string, w io.Writer, dereference bool) (*Meta, error) { return meta, nil } -func packWalkFn(root, src, dst string, tarW *tar.Writer, meta *Meta, dereference bool) filepath.WalkFunc { +func packWalkFn(root, src, dst string, tarW *tar.Writer, meta *Meta, dereference bool, ignoreRules []rule) filepath.WalkFunc { return func(path string, info os.FileInfo, err error) error { if err != nil { return err } - // Skip the .git directory. - if info.IsDir() && info.Name() == ".git" { - return filepath.SkipDir - } - // Get the relative path from the current src directory. subpath, err := filepath.Rel(src, path) if err != nil { @@ -75,20 +74,16 @@ func packWalkFn(root, src, dst string, tarW *tar.Writer, meta *Meta, dereference return nil } - // Ignore the .terraform directory itself. - if info.IsDir() && info.Name() == ".terraform" { - return nil - } - - // Ignore any files in the .terraform directory. - if !info.IsDir() && filepath.Dir(subpath) == ".terraform" { + if m := matchIgnoreRule(subpath, ignoreRules); m { return nil } - // Skip .terraform subdirectories, except for the modules subdirectory. - if strings.HasPrefix(subpath, ".terraform"+string(filepath.Separator)) && - !strings.HasPrefix(subpath, filepath.Clean(".terraform/modules")) { - return filepath.SkipDir + // Catch directories so we don't end up with empty directories, + // the files are ignored correctly + if info.IsDir() { + if m := matchIgnoreRule(subpath+string(os.PathSeparator), ignoreRules); m { + return nil + } } // Get the relative path from the initial root directory. @@ -159,7 +154,8 @@ func packWalkFn(root, src, dst string, tarW *tar.Writer, meta *Meta, dereference // If the target is a directory we can recurse into the target // directory by calling the packWalkFn with updated arguments. if info.IsDir() { - return filepath.Walk(target, packWalkFn(root, target, path, tarW, meta, dereference)) + // I'm wondering if this section is unnecessary? + return filepath.Walk(target, packWalkFn(root, target, path, tarW, meta, dereference, ignoreRules)) } // Dereference this symlink by updating the header with the target file diff --git a/slug_test.go b/slug_test.go index e8cdf61..b72c617 100644 --- a/slug_test.go +++ b/slug_test.go @@ -115,6 +115,18 @@ func TestPack(t *testing.T) { t.Fatal("expected to include foo.terraform/bar.txt") } + // Make sure baz.txt is excluded. + bazTxt := false + for _, file := range fileList { + if file == filepath.Clean("baz.txt") { + bazTxt = true + break + } + } + if bazTxt { + t.Fatal("should not include baz.txt") + } + // Check the metadata expect := &Meta{ Files: fileList, diff --git a/terraformignore.go b/terraformignore.go new file mode 100644 index 0000000..fe53d04 --- /dev/null +++ b/terraformignore.go @@ -0,0 +1,247 @@ +package slug + +import ( + "bufio" + "fmt" + "io" + "os" + "path/filepath" + "regexp" + "strings" + "text/scanner" +) + +func parseIgnoreFile(rootPath string) []rule { + // Look for .terraformignore at our root path/src + file, err := os.Open(filepath.Join(rootPath, ".terraformignore")) + defer file.Close() + + // If there's any kind of file error, punt and use the default ignore patterns + if err != nil { + // Only show the error debug if an error *other* than IsNotExist + if !os.IsNotExist(err) { + fmt.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) + } + return defaultExclusions + } + return readRules(file) +} + +func readRules(input io.Reader) []rule { + rules := defaultExclusions + scanner := bufio.NewScanner(input) + scanner.Split(bufio.ScanLines) + + for scanner.Scan() { + pattern := scanner.Text() + // Ignore blank lines + if len(pattern) == 0 { + continue + } + // Trim spaces + pattern = strings.TrimSpace(pattern) + // Ignore comments + if pattern[0] == '#' { + continue + } + // New rule structure + rule := rule{} + // Exclusions + if pattern[0] == '!' { + rule.excluded = true + pattern = pattern[1:] + } + if len(pattern) > 1 && pattern[0] == '/' && !rule.excluded { + pattern = pattern[1:] + } + rule.val = pattern + rule.dirs = strings.Split(pattern, string(os.PathSeparator)) + rules = append(rules, rule) + } + + if err := scanner.Err(); err != nil { + fmt.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) + return defaultExclusions + } + return rules +} + +func matchIgnoreRule(path string, rules []rule) bool { + matched := false + path = filepath.FromSlash(path) + dir, filename := filepath.Split(path) + dirSplit := strings.Split(dir, string(os.PathSeparator)) + + for _, rule := range rules { + match, _ := rule.match(path) + + // If no match, try the filename alone + if !match { + match, _ = rule.match(filename) + } + + if !match { + // Filename check for current directory + if rule.val[0:1] == "/" && dir == "" { + rule.val = rule.val[1:] + rule.compile() + match, _ = rule.match(filename) + } + } + + if !match { + // Directory checks + // Does some combination of its parents match our rule? + for i := 0; i < len(dirSplit); i++ { + // From the left + match, _ = rule.match(strings.Join(dirSplit[:i], string(os.PathSeparator)) + string(os.PathSeparator)) + // We found a match! stop whilst ahead + if match { + break + } + // From the right + match, _ = rule.match(strings.Join(dirSplit[i:], string(os.PathSeparator))) + if match { + break + } + } + } + + if !match { + // Directory check for current directory + // This is a case of say, ignoring terraform.d but NOT ./terraform.d/ + // Since this munges the regex for this pattern, must happen after other directory checks + if rule.val[0] == '/' { + rule.val = rule.val[1:] + rule.compile() + match, _ = rule.match(dir) + } + } + + if match { + matched = !rule.excluded + } + } + + if matched { + fmt.Printf("Skipping excluded path: %s \n", path) + } + + return matched +} + +type rule struct { + val string // the value of the rule itself + excluded bool // ! is present, an exclusion rule + dirs []string // directories of the rule + regex *regexp.Regexp // regular expression to match for the rule +} + +func (r *rule) match(path string) (bool, error) { + if r.regex == nil { + if err := r.compile(); err != nil { + return false, filepath.ErrBadPattern + } + } + + b := r.regex.MatchString(path) + return b, nil +} + +func (r *rule) compile() error { + regStr := "^" + pattern := r.val + // Go through the pattern and convert it to a regexp. + // Use a scanner to support utf-8 chars. + var scan scanner.Scanner + scan.Init(strings.NewReader(pattern)) + + sl := string(os.PathSeparator) + escSL := sl + if sl == `\` { + escSL += `\` + } + + for scan.Peek() != scanner.EOF { + ch := scan.Next() + if ch == '*' { + if scan.Peek() == '*' { + // is some flavor of "**" + scan.Next() + + // Treat **/ as ** so eat the "/" + if string(scan.Peek()) == sl { + scan.Next() + } + + if scan.Peek() == scanner.EOF { + // is "**EOF" - to align with .gitignore just accept all + regStr += ".*" + } else { + // is "**" + // Note that this allows for any # of /'s (even 0) because + // the .* will eat everything, even /'s + regStr += "(.*" + escSL + ")?" + } + } else { + // is "*" so map it to anything but "/" + regStr += "[^" + escSL + "]*" + } + } else if ch == '?' { + // "?" is any char except "/" + regStr += "[^" + escSL + "]" + } else if ch == '.' || ch == '$' { + // Escape some regexp special chars that have no meaning + // in golang's filepath.Match + regStr += `\` + string(ch) + } else if ch == '\\' { + // escape next char. Note that a trailing \ in the pattern + // will be left alone (but need to escape it) + if sl == `\` { + // On windows map "\" to "\\", meaning an escaped backslash, + // and then just continue because filepath.Match on + // Windows doesn't allow escaping at all + regStr += escSL + continue + } + if scan.Peek() != scanner.EOF { + regStr += `\` + string(scan.Next()) + } else { + regStr += `\` + } + } else { + regStr += string(ch) + } + } + + regStr += "$" + re, err := regexp.Compile(regStr) + if err != nil { + return err + } + + r.regex = re + return nil +} + +/* + Default rules: + .git/ + .terraform/ + !.terraform/modules/ +*/ + +var defaultExclusions = []rule{ + { + val: ".git/", + excluded: false, + }, + { + val: ".terraform/", + excluded: false, + }, + { + val: ".terraform/modules/", + excluded: true, + }, +} diff --git a/terraformignore_test.go b/terraformignore_test.go new file mode 100644 index 0000000..a5a13ac --- /dev/null +++ b/terraformignore_test.go @@ -0,0 +1,114 @@ +package slug + +import ( + "testing" +) + +func TestTerraformIgnore(t *testing.T) { + // path to directory without .terraformignore + p := parseIgnoreFile("testdata/external-dir") + if len(p) != 3 { + t.Fatal("A directory without .terraformignore should get the default patterns") + } + + // load the .terraformignore file's patterns + ignoreRules := parseIgnoreFile("testdata/archive-dir") + type file struct { + // the actual path, should be file path format /dir/subdir/file.extension + path string + // should match + match bool + } + paths := []file{ + { + path: ".terraform/", + match: true, + }, + { + path: "included.txt", + match: false, + }, + { + path: ".terraform/foo/bar", + match: true, + }, + { + path: ".terraform/foo/bar/more/directories/so/many", + match: true, + }, + { + path: ".terraform/foo/ignored-subdirectory/", + match: true, + }, + { + path: "baz.txt", + match: true, + }, + { + path: "parent/foo/baz.txt", + match: true, + }, + { + path: "parent/foo/bar.tf", + match: true, + }, + { + path: "parent/bar/bar.tf", + match: false, + }, + // baz.txt is ignored, but a file name including it should not be + { + path: "something/with-baz.txt", + match: false, + }, + { + path: "something/baz.x", + match: false, + }, + // Getting into * patterns + { + path: "foo/ignored-doc.md", + match: true, + }, + // Should match [a-z] group + { + path: "bar/something-a.txt", + match: true, + }, + // ignore sub- terraform.d paths + { + path: "some-module/terraform.d/x", + match: true, + }, + // but not the root one + { + path: "terraform.d/", + match: false, + }, + { + path: "terraform.d/foo", + match: false, + }, + // We ignore the directory, but a file of the same name could exist + { + path: "terraform.d", + match: false, + }, + // boop.text is ignored everywhere + { + path: "baz/boop.txt", + match: true, + }, + // except at current directory + { + path: "boop.txt", + match: false, + }, + } + for i, p := range paths { + match := matchIgnoreRule(p.path, ignoreRules) + if match != p.match { + t.Fatalf("%s at index %d should be %t", p.path, i, p.match) + } + } +} diff --git a/testdata/archive-dir/.terraformignore b/testdata/archive-dir/.terraformignore new file mode 100644 index 0000000..3503ae9 --- /dev/null +++ b/testdata/archive-dir/.terraformignore @@ -0,0 +1,20 @@ +# comments are ignored + # extra spaces are irrelevant +# ignore a file + baz.txt +# below is an empty line + +# ignore a directory +terraform.d/ +# exclude ignoring a directory at the root +!/terraform.d/ +# ignore a file at a subpath +**/foo/bar.tf +# ignore files with specific endings +foo/*.md +# character groups +bar/something-[a-z].txt +# ignore a file +boop.txt +# but not one at the current directory +!/boop.txt \ No newline at end of file diff --git a/testdata/archive-dir/baz.txt b/testdata/archive-dir/baz.txt new file mode 100644 index 0000000..3f95386 --- /dev/null +++ b/testdata/archive-dir/baz.txt @@ -0,0 +1 @@ +baz \ No newline at end of file From e8e7151728b3f39bb5d99a0fdc9e3caff8e81de3 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Wed, 9 Oct 2019 11:37:00 -0400 Subject: [PATCH 02/11] Remove 'from the right' check for directories: we should only look at relative paths from the left per git's behavior --- terraformignore.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/terraformignore.go b/terraformignore.go index fe53d04..562ced5 100644 --- a/terraformignore.go +++ b/terraformignore.go @@ -2,8 +2,8 @@ package slug import ( "bufio" - "fmt" "io" + "log" "os" "path/filepath" "regexp" @@ -20,7 +20,7 @@ func parseIgnoreFile(rootPath string) []rule { if err != nil { // Only show the error debug if an error *other* than IsNotExist if !os.IsNotExist(err) { - fmt.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) + log.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) } return defaultExclusions } @@ -60,7 +60,7 @@ func readRules(input io.Reader) []rule { } if err := scanner.Err(); err != nil { - fmt.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) + log.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) return defaultExclusions } return rules @@ -99,11 +99,6 @@ func matchIgnoreRule(path string, rules []rule) bool { if match { break } - // From the right - match, _ = rule.match(strings.Join(dirSplit[i:], string(os.PathSeparator))) - if match { - break - } } } @@ -124,7 +119,7 @@ func matchIgnoreRule(path string, rules []rule) bool { } if matched { - fmt.Printf("Skipping excluded path: %s \n", path) + log.Printf("Skipping excluded path: %s \n", path) } return matched From ca5cb9b12a98dc1edfca51f3a6ac95071581a896 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Wed, 9 Oct 2019 15:13:50 -0400 Subject: [PATCH 03/11] Believe this was an error in the tests --- testdata/archive-dir/.terraformignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testdata/archive-dir/.terraformignore b/testdata/archive-dir/.terraformignore index 3503ae9..0dacd52 100644 --- a/testdata/archive-dir/.terraformignore +++ b/testdata/archive-dir/.terraformignore @@ -5,7 +5,7 @@ # below is an empty line # ignore a directory -terraform.d/ +**/terraform.d/ # exclude ignoring a directory at the root !/terraform.d/ # ignore a file at a subpath From 70074d64c6a18c3da9a6312e24312d1e1e335d43 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Wed, 9 Oct 2019 15:43:02 -0400 Subject: [PATCH 04/11] Don't cut off that root path, could be an ignore --- terraformignore.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/terraformignore.go b/terraformignore.go index 562ced5..9f005fd 100644 --- a/terraformignore.go +++ b/terraformignore.go @@ -51,9 +51,6 @@ func readRules(input io.Reader) []rule { rule.excluded = true pattern = pattern[1:] } - if len(pattern) > 1 && pattern[0] == '/' && !rule.excluded { - pattern = pattern[1:] - } rule.val = pattern rule.dirs = strings.Split(pattern, string(os.PathSeparator)) rules = append(rules, rule) From b03503a77720f4dce0e2307eb2f3d174865473bb Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Thu, 10 Oct 2019 16:53:38 -0400 Subject: [PATCH 05/11] Switch to fmt --- terraformignore.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/terraformignore.go b/terraformignore.go index 9f005fd..3c1d6a5 100644 --- a/terraformignore.go +++ b/terraformignore.go @@ -3,7 +3,7 @@ package slug import ( "bufio" "io" - "log" + "fmt" "os" "path/filepath" "regexp" @@ -20,7 +20,7 @@ func parseIgnoreFile(rootPath string) []rule { if err != nil { // Only show the error debug if an error *other* than IsNotExist if !os.IsNotExist(err) { - log.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) + fmt.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) } return defaultExclusions } @@ -57,7 +57,7 @@ func readRules(input io.Reader) []rule { } if err := scanner.Err(); err != nil { - log.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) + fmt.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) return defaultExclusions } return rules @@ -116,7 +116,7 @@ func matchIgnoreRule(path string, rules []rule) bool { } if matched { - log.Printf("Skipping excluded path: %s \n", path) + fmt.Printf("Skipping excluded path: %s \n", path) } return matched From 75e54e0ea06cf36e1b0a3d3dbae8858566c9d3d9 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Mon, 14 Oct 2019 11:29:39 -0400 Subject: [PATCH 06/11] Modify pattern to all .terraformignore and module dirs --- terraformignore.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/terraformignore.go b/terraformignore.go index 3c1d6a5..13da406 100644 --- a/terraformignore.go +++ b/terraformignore.go @@ -2,8 +2,8 @@ package slug import ( "bufio" - "io" "fmt" + "io" "os" "path/filepath" "regexp" @@ -137,6 +137,7 @@ func (r *rule) match(path string) (bool, error) { } b := r.regex.MatchString(path) + debug(path, path, r.regex, b) return b, nil } @@ -229,11 +230,20 @@ var defaultExclusions = []rule{ excluded: false, }, { - val: ".terraform/", + val: "**/.terraform/", excluded: false, }, { - val: ".terraform/modules/", + val: "**/.terraform/modules/", excluded: true, }, } + +func debug(path string, message ...interface{}) { + debugPath := os.Getenv("TF_IGNORE_DEBUG") + if debugPath != "" { + if strings.Contains(path, debugPath) { + fmt.Println(message...) + } + } +} From 9121920affb1d231ac863a688e4bf253c87f10d7 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Mon, 14 Oct 2019 12:01:56 -0400 Subject: [PATCH 07/11] Modify path in ingestion to catch descendants Modify the pattern as we ingest it to add ** on either side as applicable, so that we match children. This removes the need for many of the "if" match statements. One possible issue is that in the case of an exclusion, it appears that now the directory does not match but its children then will; this generates the same behavior as far as the tests are concerned. This is noticed in that .terraform/modules gets "excluded" but then its children are included, so there is no problem, but it looks iffy in the debug logs --- terraformignore.go | 60 ++++++++++++---------------------------------- 1 file changed, 15 insertions(+), 45 deletions(-) diff --git a/terraformignore.go b/terraformignore.go index 13da406..5b342a6 100644 --- a/terraformignore.go +++ b/terraformignore.go @@ -51,6 +51,17 @@ func readRules(input io.Reader) []rule { rule.excluded = true pattern = pattern[1:] } + // If it is a directory, add ** so we catch descendants + if pattern[len(pattern)-1] == os.PathSeparator { + pattern = pattern + "**" + } + // If it starts with /, it is absolute + if pattern[0] == os.PathSeparator { + pattern = pattern[1:] + } else { + // Otherwise prepend **/ + pattern = "**" + string(os.PathSeparator) + pattern + } rule.val = pattern rule.dirs = strings.Split(pattern, string(os.PathSeparator)) rules = append(rules, rule) @@ -66,50 +77,9 @@ func readRules(input io.Reader) []rule { func matchIgnoreRule(path string, rules []rule) bool { matched := false path = filepath.FromSlash(path) - dir, filename := filepath.Split(path) - dirSplit := strings.Split(dir, string(os.PathSeparator)) - for _, rule := range rules { match, _ := rule.match(path) - // If no match, try the filename alone - if !match { - match, _ = rule.match(filename) - } - - if !match { - // Filename check for current directory - if rule.val[0:1] == "/" && dir == "" { - rule.val = rule.val[1:] - rule.compile() - match, _ = rule.match(filename) - } - } - - if !match { - // Directory checks - // Does some combination of its parents match our rule? - for i := 0; i < len(dirSplit); i++ { - // From the left - match, _ = rule.match(strings.Join(dirSplit[:i], string(os.PathSeparator)) + string(os.PathSeparator)) - // We found a match! stop whilst ahead - if match { - break - } - } - } - - if !match { - // Directory check for current directory - // This is a case of say, ignoring terraform.d but NOT ./terraform.d/ - // Since this munges the regex for this pattern, must happen after other directory checks - if rule.val[0] == '/' { - rule.val = rule.val[1:] - rule.compile() - match, _ = rule.match(dir) - } - } - if match { matched = !rule.excluded } @@ -218,7 +188,7 @@ func (r *rule) compile() error { } /* - Default rules: + Default rules as they would appear in .terraformignore: .git/ .terraform/ !.terraform/modules/ @@ -226,15 +196,15 @@ func (r *rule) compile() error { var defaultExclusions = []rule{ { - val: ".git/", + val: "**/.git/**", excluded: false, }, { - val: "**/.terraform/", + val: "**/.terraform/**", excluded: false, }, { - val: "**/.terraform/modules/", + val: "**/.terraform/modules/**", excluded: true, }, } From f82250cc11ed3d93805b2bcd69c1676e1410f5d8 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Mon, 14 Oct 2019 13:30:25 -0400 Subject: [PATCH 08/11] Change ze tests --- testdata/.DS_Store | Bin 0 -> 6148 bytes testdata/archive-dir/.terraformignore | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 testdata/.DS_Store diff --git a/testdata/.DS_Store b/testdata/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d281a79494208c039821e5a5cdcd3a9921c0b682 GIT binary patch literal 6148 zcmeHKK~4iP478z#m3rx|9PI21u# z54|9SY$+Khu^sP7o5V!K)7^4HG$Nu3O^`+D5t;5?x-#PdkV}kPdZHCQQ=*riK!0&c zav#zjdB4({+W9|jR+ITdYPPMk&sa98t{0oMfMxY|c>f$9Kkm-QZPM}r>+Sj^i&jFe zIs?vtGvEw30~a%ZJ6oh0Dthk>I0MeW4+C;O1T?{Dm=(*>fv%JQKzT+NfiAU#_yogf zm=&=CVRZ$nD_e=d>W=wfcF`~^syne2A8b2+7cX3PNBmICiKC+T&VVz}W#C$eBf0-) z_+@&F{H}|SoB?OxpE1CLdR|ZQQFgbUe4gC30qq`5MC`IC5a^vp00wf7+>}LK52C{^ X8fHa_BJ;%^=nsKHh0Rb(@- literal 0 HcmV?d00001 diff --git a/testdata/archive-dir/.terraformignore b/testdata/archive-dir/.terraformignore index 0dacd52..3503ae9 100644 --- a/testdata/archive-dir/.terraformignore +++ b/testdata/archive-dir/.terraformignore @@ -5,7 +5,7 @@ # below is an empty line # ignore a directory -**/terraform.d/ +terraform.d/ # exclude ignoring a directory at the root !/terraform.d/ # ignore a file at a subpath From cba1965414fda0aacbe091819c95644b479f5628 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Mon, 14 Oct 2019 13:46:34 -0400 Subject: [PATCH 09/11] Remove that comment --- slug.go | 1 - 1 file changed, 1 deletion(-) diff --git a/slug.go b/slug.go index 29b9919..059d267 100644 --- a/slug.go +++ b/slug.go @@ -154,7 +154,6 @@ func packWalkFn(root, src, dst string, tarW *tar.Writer, meta *Meta, dereference // If the target is a directory we can recurse into the target // directory by calling the packWalkFn with updated arguments. if info.IsDir() { - // I'm wondering if this section is unnecessary? return filepath.Walk(target, packWalkFn(root, target, path, tarW, meta, dereference, ignoreRules)) } From d18f3a80fafa85b0461b2a9c19e9944963370e57 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Tue, 15 Oct 2019 15:34:02 -0400 Subject: [PATCH 10/11] Standard error --- terraformignore.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/terraformignore.go b/terraformignore.go index 5b342a6..aed10fe 100644 --- a/terraformignore.go +++ b/terraformignore.go @@ -20,7 +20,7 @@ func parseIgnoreFile(rootPath string) []rule { if err != nil { // Only show the error debug if an error *other* than IsNotExist if !os.IsNotExist(err) { - fmt.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) + fmt.Fprintf(os.Stderr, "Error reading .terraformignore, default exclusions will apply: %v \n", err) } return defaultExclusions } @@ -68,7 +68,7 @@ func readRules(input io.Reader) []rule { } if err := scanner.Err(); err != nil { - fmt.Printf("Error reading .terraformignore, default exclusions will apply: %v \n", err) + fmt.Fprintf(os.Stderr, "Error reading .terraformignore, default exclusions will apply: %v \n", err) return defaultExclusions } return rules @@ -86,7 +86,7 @@ func matchIgnoreRule(path string, rules []rule) bool { } if matched { - fmt.Printf("Skipping excluded path: %s \n", path) + debug(path, "Skipping excluded path: ", path) } return matched From c0436a457e6d50218ef3c931ef87c24877a3ebd7 Mon Sep 17 00:00:00 2001 From: Pam Selle Date: Tue, 15 Oct 2019 16:20:58 -0400 Subject: [PATCH 11/11] Tweaks to debug approach, allow all level and a path level --- terraformignore.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/terraformignore.go b/terraformignore.go index aed10fe..864dc92 100644 --- a/terraformignore.go +++ b/terraformignore.go @@ -86,7 +86,7 @@ func matchIgnoreRule(path string, rules []rule) bool { } if matched { - debug(path, "Skipping excluded path: ", path) + debug(true, path, "Skipping excluded path:", path) } return matched @@ -107,7 +107,7 @@ func (r *rule) match(path string) (bool, error) { } b := r.regex.MatchString(path) - debug(path, path, r.regex, b) + debug(false, path, path, r.regex, b) return b, nil } @@ -209,10 +209,16 @@ var defaultExclusions = []rule{ }, } -func debug(path string, message ...interface{}) { +func debug(printAll bool, path string, message ...interface{}) { + logLevel := os.Getenv("TF_IGNORE") == "trace" debugPath := os.Getenv("TF_IGNORE_DEBUG") - if debugPath != "" { - if strings.Contains(path, debugPath) { + isPath := debugPath != "" + if isPath { + isPath = strings.Contains(path, debugPath) + } + + if logLevel { + if printAll || isPath { fmt.Println(message...) } }