-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
3,964 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package sanitize | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
func Sanitize(content []byte) ([]byte, error) { | ||
// bail early if there are no template characters in the CRD | ||
if !bytes.Contains(content, []byte("{{")) { | ||
return content, nil | ||
} | ||
|
||
var result [][]byte //nolint:prealloc // no idea what the size will be | ||
|
||
for _, line := range bytes.Split(content, []byte("\n")) { | ||
if bytes.HasPrefix(bytes.TrimLeft(line, " "), []byte("{{")) { | ||
// skip lines that begin with {{ | ||
continue | ||
} | ||
|
||
// replace {{ }} mid-lines with dummy value | ||
if begin := bytes.Index(line, []byte("{{")); begin != -1 { | ||
end := bytes.Index(line, []byte("}}")) | ||
if end == -1 { | ||
continue | ||
} | ||
|
||
var newLine []byte | ||
newLine = append(newLine, line[:begin]...) | ||
newLine = append(newLine, []byte("replaced")...) | ||
newLine = append(newLine, line[end+2:]...) | ||
line = newLine | ||
} | ||
|
||
result = append(result, line) | ||
} | ||
|
||
return bytes.Join(result, []byte("\n")), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package sanitize | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSanitize(t *testing.T) { | ||
type args struct { | ||
content []byte | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "sanitize", | ||
args: args{ | ||
content: []byte(`test: {{ Value.something }}`), | ||
}, | ||
want: []byte(`test: replaced`), | ||
}, | ||
{ | ||
name: "complex sanitize", | ||
args: args{ | ||
content: []byte(`{{ if .something }} | ||
test: {{ Value.something }} | ||
normal: value | ||
this: | ||
should: | ||
not: | ||
mess: {{ Value.should.work }} | ||
with: indentation | ||
{{- end }} | ||
`), | ||
}, | ||
want: []byte(`test: replaced | ||
normal: value | ||
this: | ||
should: | ||
not: | ||
mess: replaced | ||
with: indentation | ||
`), | ||
}, | ||
{ | ||
name: "begins with space", | ||
args: args{ | ||
content: []byte(`{{ if .something }} | ||
test: {{ Value.something }} | ||
normal: value | ||
this: | ||
should: | ||
not: | ||
mess: {{ Value.should.work }} | ||
with: indentation | ||
{{- end }} | ||
`), | ||
}, | ||
want: []byte(`test: replaced | ||
normal: value | ||
this: | ||
should: | ||
not: | ||
mess: replaced | ||
with: indentation | ||
`), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := Sanitize(tt.args.content) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Sanitize() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.