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/fatih/color v1.9.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-hclog v0.9.2
github.com/hashicorp/go-plugin v1.3.0

View File

@ -11,6 +11,8 @@ import (
"github.com/sirupsen/logrus"
)
const separator = "_-_"
type DriftIgnore struct {
driftignorePath string
matcher gitignore.Matcher
@ -47,6 +49,8 @@ func (r *DriftIgnore) readIgnoreFile() error {
if strings.HasPrefix(line, "#") {
continue // this is a comment
}
line = strings.ReplaceAll(line, "/", separator)
lines = append(lines, gitignore.ParsePattern(line, nil))
if !strings.HasSuffix(line, "*") {
line := fmt.Sprintf("%s.*", line)
@ -64,15 +68,14 @@ func (r *DriftIgnore) readIgnoreFile() error {
}
func (r *DriftIgnore) IsResourceIgnored(res resource.Resource) bool {
strRes := fmt.Sprintf("%s.%s", res.TerraformType(), res.TerraformId())
match := r.matcher.Match([]string{strRes}, false)
return match
return r.match(fmt.Sprintf("%s.%s", res.TerraformType(), res.TerraformId()))
}
func (r *DriftIgnore) IsFieldIgnored(res resource.Resource, path []string) bool {
sprintf := fmt.Sprintf("%s.%s", res.TerraformType(), res.TerraformId())
p := strings.Join(path, ".")
full := strings.Join([]string{sprintf, p}, ".")
return r.matcher.Match([]string{full}, false)
full := fmt.Sprintf("%s.%s.%s", res.TerraformType(), res.TerraformId(), strings.Join(path, "."))
return r.match(full)
}
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",
Id: "idwith\\backslashes",
},
&resource2.FakeResource{
Type: "resource_type",
Id: "idwith/slashes",
},
},
want: []bool{
false,
@ -113,6 +117,7 @@ func TestDriftIgnore_IsResourceIgnored(t *testing.T) {
true,
true,
true,
true,
},
path: "testdata/drift_ignore_valid/.driftignore",
},
@ -190,6 +195,14 @@ func TestDriftIgnore_IsResourceIgnored(t *testing.T) {
Type: "iam_user",
Id: "id\\WithBac*slash***\\*\\",
},
&resource2.FakeResource{
Type: "some_type",
Id: "idwith/slash",
},
&resource2.FakeResource{
Type: "some_type",
Id: "idwith/slash/",
},
},
want: []bool{
true,
@ -199,7 +212,10 @@ func TestDriftIgnore_IsResourceIgnored(t *testing.T) {
true,
true,
false,
false,
true,
},
path: "testdata/drift_ignore_all_exclude/.driftignore",
},
}
for _, tt := range tests {
@ -403,6 +419,7 @@ func TestDriftIgnore_IsFieldIgnored(t *testing.T) {
Want: false,
},
},
path: "testdata/drift_ignore_all_exclude_field/.driftignore",
},
}
for _, tt := range tests {

View File

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

View File

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