Skip to content
This repository has been archived by the owner on Oct 27, 2023. It is now read-only.

Commit

Permalink
Add bashate and shellcheck scripts and targets
Browse files Browse the repository at this point in the history
  • Loading branch information
leo8a committed Oct 19, 2023
1 parent e6dfef5 commit 849d132
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
44 changes: 44 additions & 0 deletions hack/bashate.sh
Original file line number Diff line number Diff line change
@@ -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
36 changes: 26 additions & 10 deletions hack/shellcheck.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 849d132

Please sign in to comment.