bin: Create and use new logging functions

These write errors to stderr and avoid unintentional backslash escape
processing in their arguments.
This commit is contained in:
Richard Hansen 2020-05-14 17:28:53 -04:00
parent a87a9bb63b
commit 5462d2109c
4 changed files with 49 additions and 54 deletions

View file

@ -8,6 +8,10 @@ REQUIRED_NODE_MINOR=13
REQUIRED_NPM_MAJOR=5
REQUIRED_NPM_MINOR=5
pecho() { printf %s\\n "$*"; }
log() { pecho "$@"; }
error() { log "ERROR: $@" >&2; }
fatal() { error "$@"; exit 1; }
is_cmd() { command -v "$@" >/dev/null 2>&1; }
require_minimal_version() {
@ -18,35 +22,30 @@ require_minimal_version() {
# Flag -s (--only-delimited on GNU cut) ensures no string is returned
# when there is no match
DETECTED_MAJOR=$(echo $VERSION_STRING | cut -s -d "." -f 1)
DETECTED_MINOR=$(echo $VERSION_STRING | cut -s -d "." -f 2)
DETECTED_MAJOR=$(pecho $VERSION_STRING | cut -s -d "." -f 1)
DETECTED_MINOR=$(pecho $VERSION_STRING | cut -s -d "." -f 2)
if [ -z "$DETECTED_MAJOR" ]; then
printf 'Cannot extract %s major version from version string "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" >&2
exit 1
fatal "Cannot extract $PROGRAM_LABEL major version from version string \"$VERSION_STRING\""
fi
if [ -z "$DETECTED_MINOR" ]; then
printf 'Cannot extract %s minor version from version string "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" >&2
exit 1
fatal "Cannot extract $PROGRAM_LABEL minor version from version string \"$VERSION_STRING\""
fi
case "$DETECTED_MAJOR" in
''|*[!0-9]*)
printf '%s major version from "%s" is not a number. Detected: "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" "$DETECTED_MAJOR" >&2
exit 1
fatal "$PROGRAM_LABEL major version from \"$VERSION_STRING\" is not a number. Detected: \"$DETECTED_MAJOR\""
;;
esac
case "$DETECTED_MINOR" in
''|*[!0-9]*)
printf '%s minor version from "%s" is not a number. Detected: "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" "$DETECTED_MINOR" >&2
exit 1
fatal "$PROGRAM_LABEL minor version from \"$VERSION_STRING\" is not a number. Detected: \"$DETECTED_MINOR\""
esac
if [ "$DETECTED_MAJOR" -lt "$REQUIRED_MAJOR" ] || ([ "$DETECTED_MAJOR" -eq "$REQUIRED_MAJOR" ] && [ "$DETECTED_MINOR" -lt "$REQUIRED_MINOR" ]); then
printf 'Your %s version "%s" is too old. %s %d.%d.x or higher is required.\n' "$PROGRAM_LABEL" "$VERSION_STRING" "$PROGRAM_LABEL" "$REQUIRED_MAJOR" "$REQUIRED_MINOR" >&2
exit 1
fatal "Your $PROGRAM_LABEL version \"$VERSION_STRING\" is too old. $PROGRAM_LABEL $REQUIRED_MAJOR.$REQUIRED_MINOR.x or higher is required."
fi
}
@ -60,16 +59,10 @@ fi
# Is node installed?
# Not checking io.js, default installation creates a symbolic link to node
is_cmd node || {
echo "Please install node.js ( https://nodejs.org )" >&2
exit 1
}
is_cmd node || fatal "Please install node.js ( https://nodejs.org )"
# Is npm installed?
is_cmd npm || {
echo "Please install npm ( https://npmjs.org )" >&2
exit 1
}
is_cmd npm || fatal "Please install npm ( https://npmjs.org )"
# Check npm version
NPM_VERSION_STRING=$(npm --version)
@ -92,11 +85,11 @@ done
# Does a $settings exist? if not copy the template
if [ ! -f $settings ]; then
echo "Copy the settings template to $settings..."
log "Copy the settings template to $settings..."
cp settings.json.template $settings || exit 1
fi
echo "Ensure that all dependencies are up to date... If this is the first time you have run Etherpad please be patient."
log "Ensure that all dependencies are up to date... If this is the first time you have run Etherpad please be patient."
(
mkdir -p node_modules
cd node_modules
@ -109,7 +102,7 @@ echo "Ensure that all dependencies are up to date... If this is the first time
}
# Remove all minified data to force node creating it new
echo "Clearing minified cache..."
log "Clearing minified cache..."
rm -f var/minified*
exit 0