#!/bin/bash # shellcheck source=kz-common.sh ############################################################################### # Scripts controleren. # # Geschreven door Karel Zimmer . ############################################################################### PROGRAM_PATH=$(realpath "$(dirname "$0")") readonly PROGRAM_PATH source "$PROGRAM_PATH"/kz-common.sh readonly PROGRAM_NAME='kz-ivp' readonly DISPLAY_NAME=${PROGRAM_NAME/kz-/kz } readonly RELEASE_YEAR=2015 ############################################################################### # Global constants ############################################################################### readonly OPTIONS_SHORT=$OPTIONS_SHORT_COMMON readonly OPTIONS_LONG=$OPTIONS_LONG_COMMON readonly USAGE="Gebruik: $DISPLAY_NAME $OPTIONS_USAGE_COMMON" readonly HELP="Gebruik: $DISPLAY_NAME [OPTIE...] Scripts controleren. Opties: $OPTIONS_HELP_COMMON" ############################################################################### # Globale variabelen ############################################################################### declare -i MAXRC=0 ############################################################################### # Functions ############################################################################### function check_input { local -i getopt_rc=0 local parsed='' parsed=$( getopt --alternative \ --options "$OPTIONS_SHORT" \ --longoptions "$OPTIONS_LONG" \ --name "$DISPLAY_NAME" \ -- "$@" ) || getopt_rc=$? if [[ $getopt_rc -ne $SUCCESS ]]; then process_usage NOERROR=true exit $ERROR fi eval set -- "$parsed" process_common_options "$@" while true; do case $1 in --) shift break ;; *) shift ;; esac done if [[ "$*" ]]; then info 'Geen argumenten toegestaan.' process_usage NOERROR=true exit $ERROR fi check_user_root } function process_input { local script='' local scriptsdir=$HOME/kz-scripts cd "$scriptsdir" for script in *; do if ! [[ -f $script ]]; then continue fi case $script in README.md) continue ;; esac check_trailing_spaces case $script in *.policy) continue ;; *.1|*.desktop) check_record_length ;; *.completion) check_record_length check_shellcheck ;; *.sh) check_tags check_record_types check_shellcheck ;; *) check_tags check_record_length check_release_year check_code ;; esac done cd "$HOME" } function check_code { if grep --quiet --line-regexp --regexp='#!''/bin/bash' "$script"; then check_shellcheck elif grep --quiet \ --line-regexp \ --regexp='#!''/usr/bin/python3' \ "$script" || [[ $script = kz_common.py ]] then check_pycodestyle else error " In $script: Onbekend script. Functie 'check_code' kan scriptcode niet controleren." MAXRC=$ERROR fi } function check_pycodestyle { local -i check_rc=0 pycodestyle "$script" || check_rc=$? if [[ $check_rc -ne $SUCCESS ]]; then MAXRC=$ERROR fi } function check_record_length { local -i max_line_length=79 local -i max_line_length_found=0 max_line_length_found=$(wc --max-line-length < "$script") if [[ $max_line_length_found -gt $max_line_length ]]; then error " In $script: Een regel is langer dan $max_line_length ($max_line_length_found). " MAXRC=$ERROR fi } function check_record_types { local wrong_record='' if [[ $script = kz-common.sh ]]; then return $SUCCESS fi # Kijk naar regels die beginnen met #1 t/m #9. # Goed: # #1 en #2 # #1 foo (bar) (met optie -a, --app tonen en verwerken) # #1-foo (bar) (met optie -a, --app niet tonen, wel verwerken) # #2 Verwijd./reset opdracht # Fout: # De rest :-) wrong_record=$( grep --regexp='#[1-9]' "$script" --line-number | grep --regexp='#[1-2]$' --invert-match | grep --regexp='#[1-2] ' --invert-match | grep --regexp='#1-' --invert-match || true ) if [[ $wrong_record ]]; then error " In $script: Foutieve regel(s) gevonden. $( printf '%s' "$wrong_record" | nl --number-width=8 --number-separator='' --body-numbering=n ) " MAXRC=$ERROR fi } function check_release_year { if ! grep --quiet \ --regexp='RELEASE_YEAR=' \ --regexp='release_year = ' \ "$script"; then error " In $script: Vrijgavejaar (RELEASE_YEAR/release_year) ontbreekt. " MAXRC=$ERROR fi } function check_shellcheck { local -i check_rc=0 shellcheck --external-sources "$script" || check_rc=$? if [[ $check_rc -ne $SUCCESS ]]; then MAXRC=$ERROR fi } function check_tags { if grep --quiet \ --word-regexp \ --regexp='FIXME'':' \ --regexp='TODO'':' \ "$script"; then warning " In $script: Gemarkeerde annotatie gevonden. $( grep --line-number \ --word-regexp \ --regexp='FIXME'':' \ --regexp='TODO'':' \ "$script" | nl --number-width=4 \ --number-separator='' \ --body-numbering=n ) " while true; do read -rp 'Doorgaan? [J/n]: ' case $REPLY in j*|J*|'') break ;; n*|N*) exit $SUCCESS break ;; *) printf '%s' "${REWRITE_LINE}" continue ;; esac done fi } function check_trailing_spaces { if grep --quiet --regexp=' ''$' "$script"; then error " In $script: Eindspaties gevonden. $( grep --line-number --regexp=' ''$' "$script" | nl --number-width=4 --number-separator='' --body-numbering=n ) " MAXRC=$ERROR fi } function term_script { NOERROR=true exit $MAXRC } ############################################################################### # Script ############################################################################### function main { init_script "$@" check_input "$@" process_input term_script } main "$@"