From c209a1d3a13871d50b736765026bd1a1e078d614 Mon Sep 17 00:00:00 2001 From: Doug Bird Date: Tue, 12 Jun 2018 15:11:55 -0700 Subject: [PATCH] refactoring - rename bin/update-formats to bin/update-formats.sh - fix unnecessary bashisms in bin/update-formats.sh --- README.md | 6 +- bin/update-formats | 161 ------------------------------------------ bin/update-formats.sh | 115 ++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 164 deletions(-) delete mode 100755 bin/update-formats create mode 100755 bin/update-formats.sh diff --git a/README.md b/README.md index 2428f4d..f2b5e8d 100644 --- a/README.md +++ b/README.md @@ -127,17 +127,17 @@ The lists can be conveniently used in PHP or Node projects including this packag An export module with a constant having an array value comprised of every IANA TLD. ## Updating the TLD lists - * [bin/update-formats](/bin/update-formats) + * [bin/update-formats](/bin/update-formats.sh) ```sh - $ bin/update-formats + $ bin/update-formats.sh ``` This should be all you need to update all the list formats using the latest data from IANA. It uses multiple "helper" scripts to generate the full set of native format lists. - The individual "helper" scripts do not need to be directly executed when [update-formats](/bin/update-formats) + The individual "helper" scripts do not need to be directly executed when [update-formats.sh](/bin/update-formats.sh) runs successfully. ## Legal diff --git a/bin/update-formats b/bin/update-formats deleted file mode 100755 index 0a4a9cd..0000000 --- a/bin/update-formats +++ /dev/null @@ -1,161 +0,0 @@ -#!/usr/bin/env bash -################################################################################ -## "update-formats" -## Updates the 'tlds.csv' file from iana.org and re-generates the native -## format files -################################################################################ -################################################################################ -################################################################################ -################################################################################ -################################################################################ -################################################################################ -################################################################################ -################################################################################ -################################################################################ -################################################################################ -## script localization -################################################################################ -ME_NAME="update-formats" -ME_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -HELPER_DIR=$ME_DIR/helpers -################################################################################ -## enforce dependencies -################################################################################ -DEPENDENCY_SET=(node php) -DEPENDENCY_STATUS=0 -for DEP_CMD in "${DEPENDENCY_SET[@]}" -do - which $DEP_CMD > /dev/null 2>&1 - DEP_STATUS=$? - if [ "$DEP_STATUS" -ne "0" ]; then - >&2 echo -e "$ME_NAME: (NOTICE) failed dependency check for '$DEP_CMD', command is missing or inaccessible" - DEPENDENCY_STATUS=1 - fi -done -if [ "$DEPENDENCY_STATUS" -ne "0" ]; then - >&2 echo -e "$ME_NAME: (FATAL) one or more dependency checks failed" - exit 1 -fi -################################################################################ -## read options and flags -################################################################################ -QUIET_MODE=0 -typeset -A SCRIPT_OPTS_MAP -SCRIPT_OPTS_MAP=( - [quiet]=q -) -SCRIPT_OPTS="::-:q" -#== parse options ==# -while getopts ${SCRIPT_OPTS} OPTION ; do - #== translate long options to short ==# - OPTREF="-$OPTARG" - if [[ "x$OPTION" == "x-" ]]; then - LONG_OPTION=$OPTARG - LONG_OPTARG=$(echo $LONG_OPTION | grep "=" | cut -d'=' -f2) - LONG_OPTIND=-1 - [[ "x$LONG_OPTARG" = "x" ]] && LONG_OPTIND=$OPTIND || LONG_OPTION=$(echo $OPTARG | cut -d'=' -f1) - [[ $LONG_OPTIND -ne -1 ]] && eval LONG_OPTARG="\$$LONG_OPTIND" - OPTION=${SCRIPT_OPTS_MAP[$LONG_OPTION]} - [[ "x$OPTION" = "x" ]] && OPTION="?" OPTARG="-$LONG_OPTION" - OPTREF="--$LONG_OPTION" - if [[ $( echo "${SCRIPT_OPTS}" | grep -c "${OPTION}:" ) -eq 1 ]]; then - if [[ "x${LONG_OPTARG}" = "x" ]] || [[ "${LONG_OPTARG}" = -* ]]; then - OPTION=":" OPTARG="-$LONG_OPTION" - else - OPTARG="$LONG_OPTARG"; - if [[ $LONG_OPTIND -ne -1 ]]; then - [[ $OPTIND -le $Optnum ]] && OPTIND=$(( $OPTIND+1 )) - shift $OPTIND - OPTIND=1 - fi - fi - fi - fi - - #== options follow by another option instead of argument ==# - if [[ "x${OPTION}" != "x:" ]] && [[ "x${OPTION}" != "x?" ]] && [[ "${OPTARG}" = -* ]]; then - OPTARG="$OPTION" OPTION=":" - fi - - #== manage options ==# - case "$OPTION" in - q) - QUIET_MODE=1 - ;; - : ) >&2 echo "${ME_NAME}: (FATAL) $OPTREF option requires a value" && echo -e "$ME_USAGE" && exit 1 ;; - ? ) >&2 echo "${ME_NAME}: (FATAL) $OPTREF is an unknown option" && echo -e "$ME_USAGE" && exit 1 ;; - esac -done -shift $((${OPTIND} - 1)) -################################################################################ -## display welcome message -################################################################################ -if [ "$QUIET_MODE" -ne "1" ]; then - echo "update-formats" - echo " (c) 2017 Doug Bird, All Rights Reserved." - echo " see README.md for licensing and other information" - echo " https://github.com/katmore/tld-enum#readme" - echo "" - echo " Updates the 'tlds.csv' file from iana.org and re-generates the native format files" - echo "" -fi - -################################################################################ -## run the 'generate-tlds-csv.js' helper -################################################################################ -CUR_HELPER_LABEL="new 'tlds.csv'" -CUR_HELPER_CMD=generate-tlds-csv.js -echo -e "generate $CUR_HELPER_LABEL: started\n" -$HELPER_DIR/$CUR_HELPER_CMD -q -CMD_STATUS=$? -# terminate if helper failed -if [ "$CMD_STATUS" -ne "0" ]; then - >&2 echo "$ME_NAME: (FATAL) helper for $CUR_HELPER_LABEL failed ($CUR_HELPER_CMD exit status $CMD_STATUS)" - exit $CMD_STATUS -fi -echo -e "\ngenerate new $CUR_HELPER_LABEL: success" - -################################################################################ -## run the 'generate-php-tld-enum.php' helper -################################################################################ -CUR_HELPER_LABEL="new PHP format files" -CUR_HELPER_CMD=generate-php-tld-enum.php -echo -e "generate $CUR_HELPER_LABEL: started\n" -$HELPER_DIR/$CUR_HELPER_CMD -q -CMD_STATUS=$? -# terminate if helper failed -if [ "$CMD_STATUS" -ne "0" ]; then - >&2 echo "$ME_NAME: (FATAL) helper for $CUR_HELPER_LABEL failed ($CUR_HELPER_CMD exit status $CMD_STATUS)" - exit $CMD_STATUS -fi -echo -e "\ngenerate new $CUR_HELPER_LABEL: success" - -################################################################################ -## run the 'generate-js-tld-enum.js' helper -################################################################################ -CUR_HELPER_LABEL="new JavaScript format files" -CUR_HELPER_CMD=generate-js-tld-enum.js -echo -e "generate $CUR_HELPER_LABEL: started\n" -$HELPER_DIR/$CUR_HELPER_CMD -q -CMD_STATUS=$? -# terminate if helper failed -if [ "$CMD_STATUS" -ne "0" ]; then - >&2 echo "$ME_NAME: (FATAL) helper for $CUR_HELPER_LABEL failed ($CUR_HELPER_CMD exit status $CMD_STATUS)" - exit $CMD_STATUS -fi -echo -e "\ngenerate new $CUR_HELPER_LABEL: success" - -################################################################################ -## run the 'generate-json-tld-enum.js' helper -################################################################################ -CUR_HELPER_LABEL="new JSON format files" -CUR_HELPER_CMD=generate-json-tld-enum.js -echo -e "generate $CUR_HELPER_LABEL: started\n" -$HELPER_DIR/$CUR_HELPER_CMD -q -CMD_STATUS=$? -# terminate if helper failed -if [ "$CMD_STATUS" -ne "0" ]; then - >&2 echo "$ME_NAME: (FATAL) helper for $CUR_HELPER_LABEL failed ($CUR_HELPER_CMD exit status $CMD_STATUS)" - exit $CMD_STATUS -fi -echo -e "\ngenerate new $CUR_HELPER_LABEL: success" diff --git a/bin/update-formats.sh b/bin/update-formats.sh new file mode 100755 index 0000000..ceeb618 --- /dev/null +++ b/bin/update-formats.sh @@ -0,0 +1,115 @@ +#!/bin/sh +################################################################################ +## "update-formats" +## Updates the 'tlds.csv' file from iana.org and re-generates the native +## format files +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +## 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 +################################################################################ +QUIET_MODE=0 +HELP_MODE=0 +OPTION_STATUS=0 +while getopts :?qhua-: arg; do { case $arg in + q) QUIET_MODE=1;; + h|u|a) HELP_MODE=1;; + -) LONG_OPTARG="${OPTARG#*=}"; case $OPTARG in + quiet) QUIET_MODE=1;; + help|usage|about) HELP_MODE=1;; + *) >&2 echo "$ME_NAME: unrecognized long option --$OPTARG"; OPTION_STATUS=2;; + esac ;; + *) >&2 echo "$ME_NAME: unrecognized option -$OPTARG"; OPTION_STATUS=2;; +esac } done +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 +################################################################################ +if ( [ "$QUIET_MODE" != "1" ] || [ "$HELP_MODE" = "1" ] ); then + echo "TLD update utility" + echo "" + echo "Updates the tlds.csv file from IANA and re-generates the native format files." + echo "" + echo "(c) 2017-2018 Doug Bird, All Rights Reserved." + echo "see README.md for licensing and other information" + echo "https://github.com/katmore/tld-enum#readme" + echo "" +fi + +################################################################################ +## 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." + exit 0 +fi + +################################################################################ +## enforce dependencies +################################################################################ +DEPENDENCY_STATUS=0 +depcheck() { dep_cmd=$1; dep_label=$2 + $dep_cmd > /dev/null 2>&1 || { + >&2 echo "$ME_NAME: failed dependency check for '$dep_label', command is missing or inaccessible" + DEPENDENCY_STATUS=1 +} } +depcheck "node -v" "node" +depcheck "php -v" "php" +if [ "$DEPENDENCY_STATUS" != "0" ]; then + >&2 echo "$ME_NAME: (FATAL) one or more dependency checks failed" + 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" +} + +################################################################################ +## run the 'generate-tlds-csv.js' helper +################################################################################ +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"