Skip to content

Commit

Permalink
Merge pull request #11 from snarlysodboxer/multiline-comments
Browse files Browse the repository at this point in the history
Handle multiline lead comments in target files
  • Loading branch information
snarlysodboxer authored Sep 16, 2022
2 parents c1f8c90 + aee22e1 commit 721c86a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
## Status
This is alpha, but ready for use.

## Install
Download the binary for your system and then move it in place
```shell
mv ~/Downloads/preditable-yaml-darwin-amd64 /usr/local/bin/preditable-yaml
chmod ug+x /usr/local/bin/preditable-yaml
# on MacOS
sudo xattr -r -d com.apple.quarantine /usr/local/bin/preditable-yaml
```
OR just clone the repo and `go install`.

## Usage
* `go run main.go --help` OR
* `go build main.go -o predictable-yaml`
Expand Down
5 changes: 5 additions & 0 deletions hack/cross-compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

GOARCH=amd64 GOOS=linux CGO_ENABLED=0 go build -o predictable-yaml-linux-amd64
GOARCH=amd64 GOOS=darwin CGO_ENABLED=0 go build -o predictable-yaml-darwin-amd64
GOARCH=arm64 GOOS=darwin CGO_ENABLED=0 go build -o predictable-yaml-darwin-arm64
33 changes: 18 additions & 15 deletions pkg/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,24 @@ func GetFileConfigs(node *Node) FileConfigs {
if comment == "" {
continue
}
if !strings.Contains(comment, "predictable-yaml:") {
continue
}
comment = strings.ReplaceAll(comment, "#", "")
comment = strings.ReplaceAll(comment, " ", "")
comment = strings.Split(comment, ":")[1]
splitStrings := strings.Split(comment, ",")
for _, str := range splitStrings {
switch {
case str == "ignore":
fileConfigs.Ignore = true
case str == "ignore-requireds":
fileConfigs.IgnoreRequireds = true
case strings.Contains(str, "kind"):
fileConfigs.Kind = strings.Split(str, "=")[1]
commentLines := strings.Split(comment, "\n")
for _, commentLine := range commentLines {
if !strings.Contains(commentLine, "predictable-yaml:") {
continue
}
commentLine = strings.ReplaceAll(commentLine, "#", "")
commentLine = strings.ReplaceAll(commentLine, " ", "")
commentLine = strings.Split(commentLine, ":")[1]
splitStrings := strings.Split(commentLine, ",")
for _, str := range splitStrings {
switch {
case str == "ignore":
fileConfigs.Ignore = true
case str == "ignore-requireds":
fileConfigs.IgnoreRequireds = true
case strings.Contains(str, "kind"):
fileConfigs.Kind = strings.Split(str, "=")[1]
}
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/compare/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,35 @@ spec:
expectedIgnoreRequired: false,
expectedIgnore: false,
},
{
note: "multiline comments 1: with colon",
yaml: `---
# TODO: comment
# this comment
# this comment
# predictable-yaml: ignore-requireds
apiVersion: TODO
kind: Deployment
spec:
asdf: fdsa`,
expectedKind: "Deployment",
expectedIgnoreRequired: true,
expectedIgnore: false,
},
{
note: "multiline comments 2",
yaml: `---
# predictable-yaml: ignore-requireds
# this comment
# this comment
apiVersion: TODO
kind: Deployment
spec:
asdf: fdsa`,
expectedKind: "Deployment",
expectedIgnoreRequired: true,
expectedIgnore: false,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 721c86a

Please sign in to comment.