AdGuardDNS/scripts/make/helper.sh

76 lines
1.6 KiB
Bash
Raw Permalink Normal View History

2022-11-29 18:20:10 +03:00
#!/bin/sh
# Common script helpers
#
# This file contains common script helpers. It should be sourced in scripts
# right after the initial environment processing.
2022-12-29 15:36:26 +03:00
# This comment is used to simplify checking local copies of the script. Bump
# this number every time a remarkable change is made to this script.
#
2024-10-14 17:44:24 +03:00
# AdGuard-Project-Version: 4
2022-11-29 18:20:10 +03:00
# Deferred helpers
not_found_msg='
looks like a binary not found error.
make sure you have installed the linter binaries using:
$ make go-tools
'
readonly not_found_msg
not_found() {
2024-10-14 17:44:24 +03:00
if [ "$?" -eq '127' ]; then
2022-11-29 18:20:10 +03:00
# Code 127 is the exit status a shell uses when a command or a file is
# not found, according to the Bash Hackers wiki.
#
# See https://wiki.bash-hackers.org/dict/terms/exit_status.
echo "$not_found_msg" 1>&2
fi
}
trap not_found EXIT
# Helpers
2022-12-29 15:36:26 +03:00
# run_linter runs the given linter with two additions:
#
# 1. If the first argument is "-e", run_linter exits with a nonzero exit code
# if there is anything in the command's combined output.
#
# 2. In any case, run_linter adds the program's name to its combined output.
run_linter() (
2022-11-29 18:20:10 +03:00
set +e
2024-10-14 17:44:24 +03:00
if [ "${VERBOSE:-0}" -lt '2' ]; then
2022-11-29 18:20:10 +03:00
set +x
fi
2022-12-29 15:36:26 +03:00
cmd="${1:?run_linter: provide a command}"
2022-11-29 18:20:10 +03:00
shift
2022-12-29 15:36:26 +03:00
exit_on_output='0'
2024-10-14 17:44:24 +03:00
if [ "$cmd" = '-e' ]; then
2022-12-29 15:36:26 +03:00
exit_on_output='1'
cmd="${1:?run_linter: provide a command}"
shift
2022-11-29 18:20:10 +03:00
fi
2022-12-29 15:36:26 +03:00
readonly cmd
2024-10-14 17:44:24 +03:00
output="$("$cmd" "$@")"
2022-12-29 15:36:26 +03:00
exitcode="$?"
readonly output
2024-10-14 17:44:24 +03:00
if [ "$output" != '' ]; then
2022-12-29 15:36:26 +03:00
echo "$output" | sed -e "s/^/${cmd}: /"
2022-11-29 18:20:10 +03:00
2024-10-14 17:44:24 +03:00
if [ "$exitcode" -eq '0' ] && [ "$exit_on_output" -eq '1' ]; then
2022-11-29 18:20:10 +03:00
exitcode='1'
fi
fi
return "$exitcode"
)