chore: shellcheck: rewrite shell-check.sh and make files compliant with v0.8.0

This commit is contained in:
Stéphane Lesimple 2021-12-28 17:03:20 +00:00 committed by Stéphane Lesimple
parent 54a4dc6c65
commit ae997dd93c
7 changed files with 75 additions and 43 deletions

View file

@ -177,8 +177,8 @@ fi
# push to remote if needed # push to remote if needed
if [ -n "$PUSH_REMOTE" ] && [ "$encryption_worked" = 1 ] && [ -r "$tarfile.gpg" ] ; then if [ -n "$PUSH_REMOTE" ] && [ "$encryption_worked" = 1 ] && [ -r "$tarfile.gpg" ] ; then
_log "Pushing backup file ($tarfile.gpg) remotely..." _log "Pushing backup file ($tarfile.gpg) remotely..."
# shellcheck disable=SC2086
set +e set +e
# shellcheck disable=SC2086
scp $PUSH_OPTIONS "$tarfile.gpg" "$PUSH_REMOTE"; ret=$? scp $PUSH_OPTIONS "$tarfile.gpg" "$PUSH_REMOTE"; ret=$?
set -e set -e
if [ $ret -eq 0 ]; then if [ $ret -eq 0 ]; then

View file

@ -21,7 +21,7 @@ if [ "$1" = "--big-only" ]; then
tokill="$tokill $pid" tokill="$tokill $pid"
(( ++nb )) (( ++nb ))
fi fi
done < <(lsof -a -n -c ttyrec 2>/dev/null -- /home/ 2>/dev/null) done < <(lsof -a -n -c ttyrec -- /home/ 2>/dev/null)
if [ -n "$tokill" ]; then if [ -n "$tokill" ]; then
_log "Rotating $nb big ttyrec files..." _log "Rotating $nb big ttyrec files..."
# add || true to avoid script termination due to TOCTTOU and set -e # add || true to avoid script termination due to TOCTTOU and set -e

View file

@ -5,33 +5,57 @@ basedir=$(readlink -f "$(dirname "$0")"/../..)
# shellcheck source=lib/shell/functions.inc # shellcheck source=lib/shell/functions.inc
. "$basedir"/lib/shell/functions.inc . "$basedir"/lib/shell/functions.inc
cd "$basedir" || exit 254
# $1:
# - docker, use shellcheck's docker
# - system, use any installed shellcheck, this is the default if not specified
# - anything_else, attempt to use shellcheck's docker with this tag
# $2:
# - (empty), check all known shell files
# - anything_else, check only this file
if [ "${1:-system}" = system ]; then
unset dockertag unset dockertag
if [ "$1" = "docker" ]; then elif [ "$1" = docker ]; then
dockertag=v0.7.1 dockertag=v0.8.0
fi else
if [ -n "$2" ]; then dockertag="$1"
dockertag="$2"
fi fi
(( fails=0 )) shellcheck_opts="-Calways -W 0 -x -o deprecate-which,avoid-nullary-conditions,add-default-case"
if [ -n "$dockertag" ]; then
run_shellcheck() {
local ret
action_detail "${BLUE}$1${NOC}"
if [ -n "${dockertag:-}" ]; then
# shellcheck disable=SC2086
docker run --rm -v "$PWD:/mnt" "koalaman/shellcheck:$dockertag" $shellcheck_opts "$1"; ret=$?
else
# shellcheck disable=SC2086
shellcheck $shellcheck_opts "$1"; ret=$?
fi
return $ret
}
(( fails=0 )) || true
if [ -n "${dockertag:-}" ]; then
action_doing "Checking shell files syntax using shellcheck:$dockertag docker" action_doing "Checking shell files syntax using shellcheck:$dockertag docker"
else else
action_doing "Checking shell files syntax" action_doing "Checking shell files syntax using system shellcheck"
fi fi
cd "$basedir" || exit 254 if [ -z "${2:-}" ]; then
for i in $(find . -type f ! -name "*.swp" -print0 | xargs -r0 grep -l 'set filetype=sh') for i in $(find . -type f ! -name "*.swp" ! -name "*.orig" ! -name "*.rej" -print0 | xargs -r0 grep -l 'set filetype=sh')
do do
action_detail "${BLUE}$i${NOC}" run_shellcheck "$i"; ret=$?
if [ -n "$dockertag" ]; then if [ $ret != 0 ]; then
docker run --rm -v "$PWD:/mnt" "koalaman/shellcheck:$dockertag" -Calways -W 0 -x -o deprecate-which,avoid-nullary-conditions,add-default-case "$i"; ret=$?
else
shellcheck -x "$i"; ret=$?
fi
if [ "$ret" != 0 ]; then
(( fails++ )) (( fails++ ))
fi fi
if [ $ret = 3 ] || [ $ret = 4 ]; then
echo "${RED}WARNING: your shellcheck seems too old (code $ret), please upgrade it or use a more recent docker tag!${NOC}" >&2
fi
done done
if [ "$fails" -ne 0 ] ; then if [ "$fails" -ne 0 ] ; then
@ -40,3 +64,12 @@ else
action_done "success" action_done "success"
fi fi
exit "$fails" exit "$fails"
else
run_shellcheck "$2"; ret=$?
if [ "$ret" -ne 0 ] ; then
action_error
else
action_done
fi
exit $ret
fi

View file

@ -14,9 +14,9 @@ LINUX_DISTRO=unknown
DISTRO_VERSION='' DISTRO_VERSION=''
DISTRO_LIKE='' DISTRO_LIKE=''
if [ -e /etc/os-release ]; then if [ -e /etc/os-release ]; then
LINUX_DISTRO=$(grep '^ID=' /etc/os-release | cut -d= -f2 | tr -d '"') LINUX_DISTRO=$(grep '^ID=' /etc/os-release | cut -d= -f2 | tr -d '"' || true)
DISTRO_LIKE=$(grep '^ID_LIKE=' /etc/os-release | cut -d= -f2 | tr -d '"') DISTRO_LIKE=$(grep '^ID_LIKE=' /etc/os-release | cut -d= -f2 | tr -d '"' || true)
DISTRO_VERSION=$(grep '^VERSION_ID=' /etc/os-release | cut -d= -f2 | tr -d '"') DISTRO_VERSION=$(grep '^VERSION_ID=' /etc/os-release | cut -d= -f2 | tr -d '"' || true)
fi fi
if [ -z "$LINUX_DISTRO" ] || [ -z "$DISTRO_VERSION" ]; then if [ -z "$LINUX_DISTRO" ] || [ -z "$DISTRO_VERSION" ]; then
if command -v lsb_release >/dev/null 2>&1; then if command -v lsb_release >/dev/null 2>&1; then
@ -24,22 +24,21 @@ if [ -z "$LINUX_DISTRO" ] || [ -z "$DISTRO_VERSION" ]; then
DISTRO_VERSION=$(lsb_release -sr) DISTRO_VERSION=$(lsb_release -sr)
elif [ -e /etc/debian_version ]; then elif [ -e /etc/debian_version ]; then
LINUX_DISTRO=debian LINUX_DISTRO=debian
DISTRO_VERSION=$(cat /etc/debian_version) DISTRO_VERSION=$(< /etc/debian_version)
# special case while bullseye is not yet released
if [ "$DISTRO_VERSION" = "bullseye/sid" ]; then
DISTRO_VERSION=11
fi
elif [ -e /etc/redhat-release ]; then elif [ -e /etc/redhat-release ]; then
LINUX_DISTRO=redhat LINUX_DISTRO=redhat
fi fi
fi fi
if [ "$DISTRO_VERSION" = "buster/sid" ]; then
DISTRO_VERSION=10 # special case while bookworm is not yet released
if [ "$DISTRO_VERSION" = "bookworm/sid" ]; then
DISTRO_VERSION=12
fi fi
LINUX_DISTRO=$(echo "$LINUX_DISTRO" | tr '[:upper:]' '[:lower:]' | tr -d ' ') LINUX_DISTRO=$(echo "$LINUX_DISTRO" | tr '[:upper:]' '[:lower:]' | tr -d ' ')
# shellcheck disable=SC2034 # shellcheck disable=SC2034
DISTRO_VERSION_MAJOR=$(echo "$DISTRO_VERSION" | grep -Eo '^[0-9]+' || true) DISTRO_VERSION_MAJOR=$(echo "$DISTRO_VERSION" | grep -Eo '^[0-9]+' || true)
[ -z "$DISTRO_LIKE" ] && DISTRO_LIKE="$LINUX_DISTRO" : "${DISTRO_LIKE:=$LINUX_DISTRO}"
# no longer needed, but keep if for a few versions so that non-restarted daemons are still happy # no longer needed, but keep if for a few versions so that non-restarted daemons are still happy
# shellcheck disable=SC2034 # shellcheck disable=SC2034

View file

@ -228,7 +228,7 @@ install_main() {
r) action_package rpm; exit 0;; r) action_package rpm; exit 0;;
a) action_auto; exit 0;; a) action_auto; exit 0;;
h) install_usage; exit 0;; h) install_usage; exit 0;;
?) echo "Invalid option: -$OPTARG"; usage; exit 1;; *) echo "Invalid option: -$OPTARG"; usage; exit 1;;
esac esac
done done
install_usage install_usage

View file

@ -10,4 +10,4 @@ while [ "$1" != "--" ]; do
shift shift
done done
shift shift
eval "$@" eval "$*"

View file

@ -612,7 +612,7 @@ runtests()
} }
COUNTONLY=0 COUNTONLY=0
echo === running unit tests === echo '=== running unit tests ==='
# a while read loop doesn't work well here: # a while read loop doesn't work well here:
# shellcheck disable=SC2044 # shellcheck disable=SC2044
for f in $(find "$basedir/tests/unit/" -mindepth 1 -maxdepth 1 -type f -name "*.pl" -print) for f in $(find "$basedir/tests/unit/" -mindepth 1 -maxdepth 1 -type f -name "*.pl" -print)
@ -627,11 +627,11 @@ done
COUNTONLY=1 COUNTONLY=1
testno=0 testno=0
echo === counting functional tests === echo '=== counting functional tests ==='
runtests runtests
testcount=$testno testcount=$testno
echo === will run $testcount functional tests === echo "=== will run $testcount functional tests ==="
COUNTONLY=0 COUNTONLY=0
testno=0 testno=0
runtests runtests