Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: exception sorting as makefile targets #298

Merged
merged 12 commits into from
Jun 10, 2024
22 changes: 1 addition & 21 deletions .github/workflows/exceptions-json-sort.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,4 @@ jobs:
uses: actions/checkout@v3

- name: Validte Exceptions JSON Sorting
run: |
failed_files=$( \
find pkg/kor/exceptions -name '*.json' -exec sh -c '
SORTED=$(jq "with_entries(.value |= sort_by(.Namespace, .ResourceName))" "$1")
if [ "$(jq . "$1")" != "$SORTED" ]; then
echo "$1"
fi
' sh {} \; \
| tr '\n' '\n' \
)

if [ -n "$failed_files" ]; then
echo "The following JSON files are not sorted:"
echo "=================================================="
echo "$failed_files"
echo "=================================================="

echo "Run the following command to sort all files recursively:"
echo "\`find pkg/kor/exceptions -name '*.json' -exec sh -c 'jq \"with_entries(.value |= sort_by(.Namespace, .ResourceName))\" {} > {}.tmp && mv {}.tmp {}' \\;\`"
exit 1
fi
run: make validate-exception-sorting
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
EXCEPTIONS_DIR := pkg/kor/exceptions
EXCEPTIONS_FILE_PATTERN := *.json

.PHONY: *

build:
Expand All @@ -11,3 +14,26 @@ lint-fix:

test:
go test -race -coverprofile=coverage.txt -shuffle on ./...

sort-exception-files:
@echo "Sorting exception files..."
@find $(EXCEPTIONS_DIR) -name '$(EXCEPTIONS_FILE_PATTERN)' -exec sh -c ' \
jq "with_entries(.value |= sort_by(.Namespace, .ResourceName))" {} > {}.tmp && mv {}.tmp {} \
' \;

validate-exception-sorting:
@PRINT_ERR=1; \
for file in $(wildcard $(EXCEPTIONS_DIR)/*/$(EXCEPTIONS_FILE_PATTERN)); do \
SORTED=$$(jq "with_entries(.value |= sort_by(.Namespace, .ResourceName))" "$$file"); \
CURRENT_FILE=$$(jq . "$$file"); \
if [ "$$CURRENT_FILE" != "$$SORTED" ]; then \
if [ "$$PRINT_ERR" = 1 ]; then \
echo "The following JSON files are not sorted:"; \
PRINT_ERR=0; \
fi; \
echo "\t$$file"; \
fi; \
done; \
if [ "$$PRINT_ERR" = 0 ]; then \
echo "Run the following command to sort all files recursively: make sort-exception-files"; \
fi; \
Loading