- fix "quiet mode"
- improve code comments
This commit is contained in:
Doug Bird 2018-06-12 15:43:04 -07:00
parent c209a1d3a1
commit 7ccec55531
1 changed files with 60 additions and 54 deletions

View File

@ -1,27 +1,19 @@
#!/bin/sh
################################################################################
## "update-formats"
## Updates the 'tlds.csv' file from iana.org and re-generates the native
## format files
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
## script localization
################################################################################
# Updates the tlds.csv file from IANA and re-generates the native format files
#
# @author D. Bird <retran@gmail.com>
#
#
# script localization
#
ME_NAME=$(basename $0)
ME_DIR="/$0"; ME_DIR=${ME_DIR%/*}; ME_DIR=${ME_DIR:-.}; ME_DIR=${ME_DIR#/}/; ME_DIR=$(cd "$ME_DIR"; pwd)
HELPER_DIR=$ME_DIR/helpers
################################################################################
## read options and flags
################################################################################
#
# read options and flags
#
QUIET_MODE=0
HELP_MODE=0
OPTION_STATUS=0
@ -39,9 +31,9 @@ shift $((OPTIND-1)) # remove parsed options and args from $@ list
[ "$OPTION_STATUS" != "0" ] && { >&2 echo "$ME_NAME: (FATAL) one or more invalid options"; exit $OPTION_STATUS; }
[ -z "$@" ] || { >&2 echo "$ME_NAME: (FATAL) one or more unrecognized positional arguments ($@)"; exit 2; }
################################################################################
## display welcome message
################################################################################
#
# display welcome message
#
if ( [ "$QUIET_MODE" != "1" ] || [ "$HELP_MODE" = "1" ] ); then
echo "TLD update utility"
echo ""
@ -53,22 +45,22 @@ if ( [ "$QUIET_MODE" != "1" ] || [ "$HELP_MODE" = "1" ] ); then
echo ""
fi
################################################################################
## apply help mode
################################################################################
#
# apply help mode
#
if [ "$HELP_MODE" = "1" ]; then
echo "usage:"
echo " $ME_NAME [-h] | [-q]"
echo ""
echo "options:"
echo " -h,--help: Print a help message and exit."
echo " -q,--quiet: Print less output."
echo " -q,--quiet: Print less messages."
exit 0
fi
################################################################################
## enforce dependencies
################################################################################
#
# enforce dependencies
#
DEPENDENCY_STATUS=0
depcheck() { dep_cmd=$1; dep_label=$2
$dep_cmd > /dev/null 2>&1 || {
@ -82,34 +74,48 @@ if [ "$DEPENDENCY_STATUS" != "0" ]; then
exit 1
fi
################################################################################
## helper execution wrapper function
################################################################################
helper() { helper_cmd=$1; helper_label=$2
printf "generate $helper_label: started\n\n"
$HELPER_DIR/$helper_cmd -q || { cmd_status=$?
>&2 echo "$ME_NAME: (FATAL) helper for $helper_label failed ($helper_cmd exit status $cmd_status)"
exit $cmd_status
}
printf "\ngenerate new $helper_label: success\n"
#
# helper execution wrapper function
#
helper() { helper_file=$1; helper_label=$2
if [ "$QUIET_MODE" = "1" ]; then
cmd_output=$("$HELPER_DIR/$helper_file" 2>&1)
cmd_status=$?
[ "$cmd_status" != "0" ] && [ -n "$cmd_output" ] && >&2 printf "$cmd_output\n"
else
printf "generate $helper_label: started\n\n"
"$HELPER_DIR/$helper_file" -q
cmd_status=$?
[ "$cmd_status" = "0" ] && printf "\ngenerate new $helper_label: success\n"
fi
if [ "$cmd_status" != "0" ]; then
>&2 echo "$ME_NAME: (FATAL) helper for $helper_label failed ($helper_file exit status $cmd_status)"
exit 1
fi
}
################################################################################
## run the 'generate-tlds-csv.js' helper
################################################################################
#
# execute the helpers
#
helper generate-tlds-csv.js "new 'tlds.csv'"
################################################################################
## run the 'generate-php-tld-enum.php' helper
################################################################################
helper generate-php-tld-enum.php "new PHP format files"
################################################################################
## run the 'generate-js-tld-enum.js' helper
################################################################################
helper generate-js-tld-enum.js "new JavaScript format files"
################################################################################
## run the 'generate-json-tld-enum.js' helper
################################################################################
helper generate-json-tld-enum.js "new JSON format files"
echo "$ME_NAME: all formats updates were successful"