diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ce65798 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,14 @@ +name: Test this action + +on: push + +jobs: + test: + name: Test action + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - run: | + sh test.sh diff --git a/README.md b/README.md index 23d8d6f..404bc62 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,10 @@ jobs: path: ./a-custom-path ``` +## Contributors + +Most of the work was done by @Rotendahl, I mainly copy-pasted stuff from stack overflow :) + diff --git a/action.yml b/action.yml index 8ce51d8..8fcccec 100644 --- a/action.yml +++ b/action.yml @@ -11,14 +11,14 @@ runs: image: "Dockerfile" args: - ${{ inputs.path }} + - ${{ inputs.exclude }} inputs: path: description: Path to check required: false default: "." -# -# # TODO return exact number? -# outputs: -# files-found: -# description: Number of files with CRLF endings + exclude: + description: Space-separated list of dirs to exclude + required: false + default: "" diff --git a/entrypoint.sh b/entrypoint.sh old mode 100644 new mode 100755 index 97602a3..d4cee7e --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,27 +1,30 @@ -echo "searching for CRLF endings in: $1" +#!/bin/bash + +printf "searching for CRLF endings in: $1\n" # TODO allow multiple paths with $@ instead of $1 BOLD_RED='\033[1;31m' BOLD_GREEN='\033[1;32m' NC='\033[0m' -ERROR_COUNT=0 +FILES_TYPES="$(\ + find . ! -path "./.git/*" -not -type d -exec file "{}" ";" +)" -if \ - grep \ - --recursive \ - --files-with-matches \ - --binary \ - --binary-files=without-match \ - --max-count=1 \ - --exclude-dir="\.git" \ - $'\r' \ - $1 \ - ; then - # TODO exit status should be number of files with wrong endings found - echo -e "${BOLD_RED}Found at least a file with CRLF endings.${NC}" - exit 1 -fi +FILES_WITH_CRLF=$(echo "$FILES_TYPES" | grep " CRLF " | cut -d " " -f 1 | cut -d ":" -f 1) -echo -e "${BOLD_GREEN}No files with CRLF endings found.${NC}" -exit 0 +for word in $2 +do + FILES_WITH_CRLF=$(echo "$FILES_WITH_CRLF" | grep "^./$word" --invert-match) +done + +if [ -z "$FILES_WITH_CRLF" ] +then + echo "${BOLD_GREEN}No files with CRLF endings found.${NC}" + exit 0 +else + NR_FILES=$(echo "$FILES_WITH_CRLF" | wc -l) + echo "${BOLD_RED}Found $NR_FILES files with CRLF endings.${NC}" + echo "$FILES_WITH_CRLF" + exit $NR_FILES +fi diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..dc753e8 --- /dev/null +++ b/test.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# this is a sorry excuse for a test, but hey, it kinda works + +mkdir -p "temp" + +printf "\r\n" > "temp/bad" +printf "\r\n" > "temp/ignored" +printf "\n" > "temp/good" + +./entrypoint.sh . "temp/ignored" + +RESULT="$?" + +rm -r "temp" + +# is there a better way to do this? +[ $RESULT -eq 1 ] +exit $?