diff --git a/.shellcheckrc b/.shellcheckrc new file mode 100644 index 0000000..93eb5ca --- /dev/null +++ b/.shellcheckrc @@ -0,0 +1,4 @@ +# SC2046: Quote this to prevent word splitting. +# SC2086: Double quote to prevent globbing and word splitting. +# SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. +disable=SC2046,SC2086,SC2181 diff --git a/Makefile b/Makefile index b122f73..a246a96 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,16 @@ vet: ## Run go vet against code. deps-update: ## Run go mod tidy and vendor against code. go mod tidy && go mod vendor +.PHONY: shellcheck +shellcheck: ## Run shellcheck + @echo "Running shellcheck" + hack/shellcheck.sh + +.PHONY: bashate +bashate: ## Run bashate + @echo "Running bashate" + hack/bashate.sh + ##@ Build diff --git a/hack/bashate.sh b/hack/bashate.sh new file mode 100755 index 0000000..417ec55 --- /dev/null +++ b/hack/bashate.sh @@ -0,0 +1,44 @@ +#!/bin/bash + + +# Create a temporary directory for the virtual environment +VENVDIR=$(mktemp --tmpdir -d venv.XXXXXX) || { + echo "Failed to create working directory" >&2 + exit 1 +} + +function cleanup { + # Clean up the temporary directory + rm -rf "${VENVDIR}" +} +trap cleanup EXIT + +function fatal { + # Log an error message and exit with a non-zero status + echo "ERROR: $*" >&2 + exit 1 +} + +# Create a Python virtual environment +python3 -m venv "${VENVDIR}" || fatal "Could not set up virtualenv" + +# Activate the virtual environment +# shellcheck disable=SC1091 +source "${VENVDIR}/bin/activate" || fatal "Could not activate virtualenv" + +# Install bashate in the virtual environment +pip install bashate==2.1.0 || fatal "Installation of bashate failed" + +# Run bashate on all Bash script files, excluding specified directories +find . -name '*.sh' -not -path './vendor/*' -not -path './git/*' \ + -not -path './bin/*' -not -path './testbin/*' -print0 | + xargs -0 --no-run-if-empty bashate -v -e 'E*' -i E006 + +# Check the exit status of bashate and exit with an appropriate status +if [ $? -eq 0 ]; then + echo "All checks passed successfully" + exit 0 +else + echo "Some checks failed. See error messages above." + exit 2 +fi diff --git a/hack/shellcheck.sh b/hack/shellcheck.sh index 4510073..bd51718 100755 --- a/hack/shellcheck.sh +++ b/hack/shellcheck.sh @@ -1,28 +1,44 @@ #!/bin/bash +# Set the ShellCheck version and binary path shellcheck_version="v0.7.2" shellcheck_binary="$(go env GOPATH)/bin/shellcheck" +function cleanup { + # Clean up the temporary directory + [ -n "$temp_dir" ] && rm -rf "$temp_dir" +} +trap cleanup EXIT + +function fatal { + # Log an error message and exit with a non-zero status + echo "ERROR: $*" >&2 + exit 1 +} + +# Check if ShellCheck binary is not found if [ ! -f "${shellcheck_binary}" ]; then - echo "Downloading shellcheck tool..." + echo "Downloading ShellCheck tool..." download_url="https://github.com/koalaman/shellcheck/releases/download/$shellcheck_version/shellcheck-$shellcheck_version.linux.x86_64.tar.xz" # Create a temporary directory for extraction - temp_dir="$(mktemp -d)" + temp_dir="$(mktemp -d)" || fatal "Failed to create a temporary directory" # Download and extract ShellCheck - wget -qO- "$download_url" | tar -xJ -C "$temp_dir" --strip=1 "shellcheck-$shellcheck_version/shellcheck" + wget -qO- "$download_url" | tar -xJ -C "$temp_dir" --strip=1 "shellcheck-$shellcheck_version/shellcheck" || fatal "Failed to download and extract ShellCheck" - # Move shellcheck binary to the desired location - mv "$temp_dir/shellcheck" "$shellcheck_binary" + # Move the ShellCheck binary to the desired location + mv "$temp_dir/shellcheck" "$shellcheck_binary" || fatal "Failed to move ShellCheck binary" - # Clean up temporary directory - rm -r "$temp_dir" + echo "ShellCheck has been installed" fi - # Find and check shell script files with ShellCheck find . -name '*.sh' -not -path './vendor/*' -not -path './git/*' \ - -not -path './bin/*' -not -path './testbin/*' -print0 \ - | xargs -0 --no-run-if-empty "${shellcheck_binary}" -x + -not -path './bin/*' -not -path './testbin/*' -print0 | + xargs -0 --no-run-if-empty "${shellcheck_binary}" -x || fatal "ShellCheck encountered errors" + +# All checks passed successfully +echo "All checks passed successfully" +exit 0