From 20a7069307d809d5f94a6bc43640b29c7b23a8ad Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Sun, 1 Dec 2024 14:14:59 -0600 Subject: [PATCH] Add tidying and linting for shell code and clean some shell up --- do-release.sh | 10 +++++----- examples/golang/helpers/check-go-mod.sh | 18 +++++++++--------- git/hooks/pre-commit.sh | 4 ++-- precious.toml | 17 +++++++++++++++++ 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/do-release.sh b/do-release.sh index 0180f10..f2c7db6 100755 --- a/do-release.sh +++ b/do-release.sh @@ -1,7 +1,7 @@ #!/bin/bash -cargo release --package precious-helpers $@ -cargo release --package precious-testhelper $@ -cargo release --package precious-core $@ -cargo release --package precious-integration $@ -cargo release --package precious $@ +cargo release --package precious-helpers "$@" +cargo release --package precious-testhelper "$@" +cargo release --package precious-core "$@" +cargo release --package precious-integration "$@" +cargo release --package precious "$@" diff --git a/examples/golang/helpers/check-go-mod.sh b/examples/golang/helpers/check-go-mod.sh index 5f5c00d..76c7dbf 100644 --- a/examples/golang/helpers/check-go-mod.sh +++ b/examples/golang/helpers/check-go-mod.sh @@ -2,33 +2,33 @@ set -e -ROOT=$( git rev-parse --show-toplevel ) -BEFORE_MOD=$( md5sum "$ROOT/go.mod" ) -BEFORE_SUM=$( md5sum "$ROOT/go.sum" ) +ROOT=$(git rev-parse --show-toplevel) +BEFORE_MOD=$(md5sum "$ROOT/go.mod") +BEFORE_SUM=$(md5sum "$ROOT/go.sum") -OUTPUT=$( go mod tidy -v 2>&1 ) +OUTPUT=$(go mod tidy -v 2>&1) -AFTER_MOD=$( md5sum "$ROOT/go.mod" ) -AFTER_SUM=$( md5sum "$ROOT/go.sum" ) +AFTER_MOD=$(md5sum "$ROOT/go.mod") +AFTER_SUM=$(md5sum "$ROOT/go.sum") red=$'\e[1;31m' end=$'\e[0m' if [ "$BEFORE_MOD" != "$AFTER_MOD" ]; then - printf "${red}Running go mod tidy changed the contents of go.mod${end}\n" + printf "%sRunning go mod tidy changed the contents of go.mod%s\n" "$red" "$end" git diff "$ROOT/go.mod" changed=1 fi if [ "$BEFORE_SUM" != "$AFTER_SUM" ]; then - printf "${red}Running go mod tidy changed the contents of go.sum${end}\n" + printf "%sRunning go mod tidy changed the contents of go.sum%s\n" "$red" "$end" git diff "$ROOT/go.sum" changed=1 fi if [ -n "$changed" ]; then if [ -n "$OUTPUT" ]; then - printf "\nOutput from running go mod tidy -v:\n${OUTPUT}\n" + printf "\nOutput from running go mod tidy -v:\n%s\n" "$OUTPUT" else printf "\nThere was no output from running go mod tidy -v\n\n" fi diff --git a/git/hooks/pre-commit.sh b/git/hooks/pre-commit.sh index 63595ad..9256ae2 100755 --- a/git/hooks/pre-commit.sh +++ b/git/hooks/pre-commit.sh @@ -1,5 +1,6 @@ #!/bin/bash +declare -i status status=0 PRECIOUS=$(which precious) @@ -7,8 +8,7 @@ if [[ -z $PRECIOUS ]]; then PRECIOUS=./bin/precious fi -"$PRECIOUS" lint -s -if (( $? != 0 )); then +if ! "$PRECIOUS" lint -s; then status+=1 fi diff --git a/precious.toml b/precious.toml index 397f86a..bbacef9 100644 --- a/precious.toml +++ b/precious.toml @@ -72,6 +72,23 @@ ok-exit-codes = 0 lint-failure-exit-codes = 1 ignore-stderr = ["The .+ file is not sorted", "The .+ file is not unique"] +[commands.shellcheck] +type = "lint" +include = "**/*.sh" +cmd = "shellcheck" +ok_exit_codes = 0 +lint_failure_exit_codes = 1 + +[commands.shfmt] +type = "both" +include = "**/*.sh" +cmd = ["shfmt", "--simplify", "--indent", "4"] +lint_flags = "--diff" +tidy_flags = "--write" +ok_exit_codes = 0 +lint_failure_exit_codes = 1 +labels = ["default", "fast-tidy"] + [commands.typos] type = "lint" include = "**/*"