From 21c9940d4d90eca8c0fb7cdb6ca9351c0547c426 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sat, 11 Sep 2021 08:49:40 -0700 Subject: [PATCH] fix: check for settings file existence or exit --- all/dev/backup/dbackup | 43 +++++++++++++++++++++++++++--- all/dev/backup/src/root_command.sh | 5 +++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/all/dev/backup/dbackup b/all/dev/backup/dbackup index 47d6169..df88aab 100755 --- a/all/dev/backup/dbackup +++ b/all/dev/backup/dbackup @@ -17,6 +17,7 @@ root_command() { local settings=${args[--settings]} + if [[ $settings ]]; then if [[ -f $settings ]]; then echo loading settings file $settings module_load yaml @@ -24,7 +25,9 @@ root_command() { echo $s_source echo $s_target echo $s_host - + else + echo settings file $settings does not exist && return 1 + fi fi if [[ $s_server_host ]]; then @@ -308,6 +311,30 @@ dbackup_usage() { fi } +# :command.normalize_input +normalize_input() { + local arg flags + + while [[ $# -gt 0 ]]; do + arg="$1" + if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then + input+=("${BASH_REMATCH[1]}") + input+=("${BASH_REMATCH[2]}") + elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then + input+=("${BASH_REMATCH[1]}") + input+=("${BASH_REMATCH[2]}") + elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then + flags="${BASH_REMATCH[1]}" + for (( i=0 ; i < ${#flags} ; i++ )); do + input+=("-${flags:i:1}") + done + else + input+=("$arg") + fi + + shift + done +} # :command.inspect_args inspect_args() { readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort) @@ -342,7 +369,7 @@ parse_requirements() { --help | -h ) long_usage=yes dbackup_usage - exit 1 + exit ;; esac @@ -528,6 +555,7 @@ parse_requirements() { esac done + # :command.catch_all_filter # :command.default_assignments # :command.whitelist_filter } @@ -536,7 +564,12 @@ parse_requirements() { initialize() { version="0.1.0" long_usage='' - set -e + set -e + + if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then + printf "bash version 4 or higher is required\n" + exit 1 + fi # :src/initialize.sh # Code here runs inside the initialize() function @@ -552,7 +585,9 @@ initialize() { run() { declare -A args declare -a other_args - parse_requirements "$@" + declare -a input + normalize_input "$@" + parse_requirements "${input[@]}" if [[ $action == "root" ]]; then root_command diff --git a/all/dev/backup/src/root_command.sh b/all/dev/backup/src/root_command.sh index 0f84cd8..1e64a23 100644 --- a/all/dev/backup/src/root_command.sh +++ b/all/dev/backup/src/root_command.sh @@ -10,6 +10,7 @@ module_load path local settings=${args[--settings]} +if [[ $settings ]]; then if [[ -f $settings ]]; then echo loading settings file $settings module_load yaml @@ -17,7 +18,9 @@ if [[ -f $settings ]]; then echo $s_source echo $s_target echo $s_host - + else + echo settings file $settings does not exist && return 1 +fi fi if [[ $s_server_host ]]; then