fix slash in gitignore syntax being special

main
Martin Guibert 2021-06-21 18:59:45 +02:00
parent 437c20e0b7
commit bb2e1e0f0f
5 changed files with 31 additions and 9 deletions

2
go.mod
View File

@ -8,7 +8,7 @@ require (
github.com/eapache/go-resiliency v1.2.0 github.com/eapache/go-resiliency v1.2.0
github.com/fatih/color v1.9.0 github.com/fatih/color v1.9.0
github.com/getsentry/sentry-go v0.10.0 github.com/getsentry/sentry-go v0.10.0
github.com/go-git/go-git/v5 v5.1.0 // indirect github.com/go-git/go-git/v5 v5.1.0
github.com/hashicorp/go-getter v1.5.1 github.com/hashicorp/go-getter v1.5.1
github.com/hashicorp/go-hclog v0.9.2 github.com/hashicorp/go-hclog v0.9.2
github.com/hashicorp/go-plugin v1.3.0 github.com/hashicorp/go-plugin v1.3.0

View File

@ -11,6 +11,8 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
const separator = "_-_"
type DriftIgnore struct { type DriftIgnore struct {
driftignorePath string driftignorePath string
matcher gitignore.Matcher matcher gitignore.Matcher
@ -47,6 +49,8 @@ func (r *DriftIgnore) readIgnoreFile() error {
if strings.HasPrefix(line, "#") { if strings.HasPrefix(line, "#") {
continue // this is a comment continue // this is a comment
} }
line = strings.ReplaceAll(line, "/", separator)
lines = append(lines, gitignore.ParsePattern(line, nil)) lines = append(lines, gitignore.ParsePattern(line, nil))
if !strings.HasSuffix(line, "*") { if !strings.HasSuffix(line, "*") {
line := fmt.Sprintf("%s.*", line) line := fmt.Sprintf("%s.*", line)
@ -64,15 +68,14 @@ func (r *DriftIgnore) readIgnoreFile() error {
} }
func (r *DriftIgnore) IsResourceIgnored(res resource.Resource) bool { func (r *DriftIgnore) IsResourceIgnored(res resource.Resource) bool {
strRes := fmt.Sprintf("%s.%s", res.TerraformType(), res.TerraformId()) return r.match(fmt.Sprintf("%s.%s", res.TerraformType(), res.TerraformId()))
match := r.matcher.Match([]string{strRes}, false)
return match
} }
func (r *DriftIgnore) IsFieldIgnored(res resource.Resource, path []string) bool { func (r *DriftIgnore) IsFieldIgnored(res resource.Resource, path []string) bool {
sprintf := fmt.Sprintf("%s.%s", res.TerraformType(), res.TerraformId()) full := fmt.Sprintf("%s.%s.%s", res.TerraformType(), res.TerraformId(), strings.Join(path, "."))
p := strings.Join(path, ".") return r.match(full)
full := strings.Join([]string{sprintf, p}, ".") }
return r.matcher.Match([]string{full}, false)
func (r *DriftIgnore) match(strRes string) bool {
return r.matcher.Match([]string{strings.ReplaceAll(strRes, "/", separator)}, false)
} }

View File

@ -102,6 +102,10 @@ func TestDriftIgnore_IsResourceIgnored(t *testing.T) {
Type: "resource_type", Type: "resource_type",
Id: "idwith\\backslashes", Id: "idwith\\backslashes",
}, },
&resource2.FakeResource{
Type: "resource_type",
Id: "idwith/slashes",
},
}, },
want: []bool{ want: []bool{
false, false,
@ -113,6 +117,7 @@ func TestDriftIgnore_IsResourceIgnored(t *testing.T) {
true, true,
true, true,
true, true,
true,
}, },
path: "testdata/drift_ignore_valid/.driftignore", path: "testdata/drift_ignore_valid/.driftignore",
}, },
@ -190,6 +195,14 @@ func TestDriftIgnore_IsResourceIgnored(t *testing.T) {
Type: "iam_user", Type: "iam_user",
Id: "id\\WithBac*slash***\\*\\", Id: "id\\WithBac*slash***\\*\\",
}, },
&resource2.FakeResource{
Type: "some_type",
Id: "idwith/slash",
},
&resource2.FakeResource{
Type: "some_type",
Id: "idwith/slash/",
},
}, },
want: []bool{ want: []bool{
true, true,
@ -199,7 +212,10 @@ func TestDriftIgnore_IsResourceIgnored(t *testing.T) {
true, true,
true, true,
false, false,
false,
true,
}, },
path: "testdata/drift_ignore_all_exclude/.driftignore",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -403,6 +419,7 @@ func TestDriftIgnore_IsFieldIgnored(t *testing.T) {
Want: false, Want: false,
}, },
}, },
path: "testdata/drift_ignore_all_exclude_field/.driftignore",
}, },
} }
for _, tt := range tests { for _, tt := range tests {

View File

@ -1,2 +1,3 @@
* *
!iam_user.* !iam_user.*
!some_type.idwith/slash

View File

@ -3,5 +3,6 @@ wildcard_resource.*
resource_type.id\.with\.dots resource_type.id\.with\.dots
resource_type.idwith\\ resource_type.idwith\\
resource_type.idwith\\backslashes resource_type.idwith\\backslashes
resource_type.idwith/slashes
# this is a comment # this is a comment