Some review fixes

main
Elie 2021-01-04 14:40:19 +01:00
parent 2a363841ed
commit 6aceaff771
No known key found for this signature in database
GPG Key ID: 399AF69092C727B6
4 changed files with 35 additions and 76 deletions

View File

@ -6,8 +6,6 @@ import (
"os"
"strings"
"github.com/cloudskiff/driftctl/pkg/stringutils"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/sirupsen/logrus"
)
@ -39,7 +37,7 @@ func (r *DriftIgnore) readIgnoreFile() error {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
typeVal := escapableSplit(line)
typeVal := readDriftIgnoreLine(line)
nbArgs := len(typeVal)
if nbArgs < 2 {
logrus.WithFields(logrus.Fields{
@ -108,7 +106,7 @@ func (r *DriftIgnore) IsFieldIgnored(res resource.Resource, path []string) bool
func (r *DriftIgnore) isExcluded(rules []string, changePath []string) bool {
RuleCheck:
for _, rule := range rules {
path := escapableSplit(rule)
path := readDriftIgnoreLine(rule)
if len(path) > len(changePath) {
continue // path size does not match
}
@ -123,18 +121,47 @@ RuleCheck:
return false
}
func escapableSplit(line string) []string {
/**
* Read a line of ignore
* Handle split on dots and escaping
*/
func readDriftIgnoreLine(line string) []string {
var splitted []string
lastWordEnd := 0
for i := range line {
if line[i] == '.' && ((i >= 1 && line[i-1] != '\\') || (i >= 2 && line[i-1] == '\\' && line[i-2] == '\\')) {
splitted = append(splitted, stringutils.Unescape(line[lastWordEnd:i]))
splitted = append(splitted, unescapeDriftIgnoreLine(line[lastWordEnd:i]))
lastWordEnd = i + 1
continue
}
if i == len(line)-1 {
splitted = append(splitted, stringutils.Unescape(line[lastWordEnd:]))
splitted = append(splitted, unescapeDriftIgnoreLine(line[lastWordEnd:]))
}
}
return splitted
}
func unescapeDriftIgnoreLine(line string) string {
var res string
lastEscapeEnd := 0
for i := range line {
if line[i] == '\\' {
if i+1 < len(line) && line[i+1] == '\\' {
continue
}
if i > 1 && line[i-1] == '\\' {
res += line[lastEscapeEnd:i]
lastEscapeEnd = i + 1
continue
}
res += line[lastEscapeEnd:i]
lastEscapeEnd = i + 1
continue
}
if i == len(line)-1 {
res += line[lastEscapeEnd:]
}
}
return res
}

View File

@ -283,7 +283,7 @@ func Test_escapableSplit(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := escapableSplit(tt.line); !reflect.DeepEqual(got, tt.want) {
if got := readDriftIgnoreLine(tt.line); !reflect.DeepEqual(got, tt.want) {
t.Errorf("EscapableSplit() = %v, want %v", got, tt.want)
}
})

View File

@ -1,27 +0,0 @@
package stringutils
// Remove \ that are not escaped
func Unescape(line string) string {
var res string
lastEscapeEnd := 0
for i := range line {
if line[i] == '\\' {
if i+1 < len(line) && line[i+1] == '\\' {
continue
}
if i > 1 && line[i-1] == '\\' {
res += line[lastEscapeEnd:i]
lastEscapeEnd = i + 1
continue
}
res += line[lastEscapeEnd:i]
lastEscapeEnd = i + 1
continue
}
if i == len(line)-1 {
res += line[lastEscapeEnd:]
}
}
return res
}

View File

@ -1,41 +0,0 @@
package stringutils
import (
"testing"
)
func TestUnescape(t *testing.T) {
tests := []struct {
name string
line string
want string
}{
{
name: "\\ unescaped at start",
line: "\\",
want: "",
},
{
name: "\\ escaped at start",
line: "\\\\",
want: "\\",
},
{
name: "dot escaped",
line: "\\.this\\.is\\.dotted\\.",
want: ".this.is.dotted.",
},
{
name: "mix escaped",
line: "\\.this\\.is\\\\.dotted\\.",
want: ".this.is\\.dotted.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Unescape(tt.line); got != tt.want {
t.Errorf("Unescape() = %v, want %v", got, tt.want)
}
})
}
}