timescaledb/scripts/check_file_license.sh
Alexander Kuzmenkov 4a17d4c402 Add shellcheck to CI
Writing a shell script correctly can be hard even for a skilled
programmer. shellcheck is a static analysis tool that helps catch
common errors in shell scripts. We now have 36 executable scripts in
our repository, for which shellcheck reports 126 errors (calculated
like find . -type f -executable -exec bash -c '[ "$(file --brief
--mime-type "$1")" == "text/x-shellscript" ]' sh {} \; -exec shellcheck
-f gcc --exclude=SC2086 {} \; | cut -d: -f1 | sort | uniq | wc -l).
This commit fixes these warnings and adds a GitHub actions workflow
that runs shellcheck on all the executable shell scripts in the
repository. The warning SC2086: Double quote to prevent globbing and
word splitting is disabled globally, because it has little practical
consequences, sometimes leads to false positives, and is general is too
widespread because people forget to quote.
2021-11-15 14:54:14 +03:00

96 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
get_c_license() {
awk 'BEGIN {ORS=""}{if($1 == "*/") {print; exit;}} {print}' $1
}
get_sql_license() {
awk 'BEGIN {ORS=""}{if($1 == "") {print; exit;}} {print}' $1
}
check_file() {
FLAG=${1}
FILE=${2}
SCRIPTPATH="$( cd "$(dirname "${0}")" || exit ; pwd -P )"
TIMESCALE_LOCATION=$(dirname ${SCRIPTPATH})
LICENSE_FILE=
LICENSE_STRING=
FIRST_COMMENT=
case ${FLAG} in
('-c')
LICENSE_FILE="${SCRIPTPATH}/c_license_header-apache.h"
LICENSE_STRING=$(get_c_license ${LICENSE_FILE})
FIRST_COMMENT=$(get_c_license ${FILE})
;;
('-e')
LICENSE_FILE="${SCRIPTPATH}/c_license_header-timescale.h"
LICENSE_STRING=$(get_c_license ${LICENSE_FILE})
FIRST_COMMENT=$(get_c_license ${FILE})
;;
('-i')
LICENSE_FILE="${SCRIPTPATH}/license_apache.spec"
LICENSE_STRING=$(get_sql_license ${LICENSE_FILE})
FIRST_COMMENT=$(get_sql_license ${FILE})
;;
('-j')
LICENSE_FILE="${SCRIPTPATH}/license_tsl.spec"
LICENSE_STRING=$(get_sql_license ${LICENSE_FILE})
FIRST_COMMENT=$(get_sql_license ${FILE})
;;
('-s')
LICENSE_FILE="${SCRIPTPATH}/sql_license_apache.sql"
LICENSE_STRING=$(get_sql_license ${LICENSE_FILE})
FIRST_COMMENT=$(get_sql_license ${FILE})
;;
('-t')
LICENSE_FILE="${SCRIPTPATH}/sql_license_tsl.sql"
LICENSE_STRING=$(get_sql_license ${LICENSE_FILE})
FIRST_COMMENT=$(get_sql_license ${FILE})
;;
('-p')
LICENSE_FILE="${SCRIPTPATH}/license_tsl.spec"
LICENSE_STRING=$(get_sql_license ${LICENSE_FILE})
FIRST_COMMENT=$(get_sql_license ${FILE})
;;
("--")
return 0;
;;
(*)
echo "Unkown flag" ${1}
return 1;
esac
if [[ "${FIRST_COMMENT}" != "${LICENSE_STRING}" ]]; then
echo ${FILE#"$TIMESCALE_LOCATION/"} "lacks a license header. Add";
echo
cat ${LICENSE_FILE}
echo
echo "to the top of the file";
return 1;
fi
}
args=$(getopt "c:e:i:j:p:s:t:" "$@"); errcode=$?; set -- $args
if [[ ${errcode} != 0 ]]; then
echo 'Usage: check_file_license ((-c|-e|-i|-j|-s|-t) <filename> ...)'
return 2
fi
ERRORCODE=0
while [[ ${1} ]]; do
if [[ ${1} == "--" ]]; then
break;
fi
check_file ${1} ${2}
FILE_ERR=${?}
ERRORCODE=$((FILE_ERR | ERRORCODE));
shift; shift;
done
exit ${ERRORCODE};