diff --git a/src/rsync_offsite_backup.sh b/src/rsync_offsite_backup.sh index 26b757b..59dbd82 100755 --- a/src/rsync_offsite_backup.sh +++ b/src/rsync_offsite_backup.sh @@ -372,29 +372,33 @@ function get_config_file() { fi } -return 0 # FIXME: Remove +function load_config() { + local conf="$(get_config_file)" -# -# Read config -# -_CONF='' -readonly SSH_KEY="$(echo "${_CONF}" | dasel --null -c '.ssh.key' -p yaml 2>/dev/null)" -readonly SSH_HOST="$(echo "${_CONF}" | dasel --null -c '.ssh.host' -p yaml 2>/dev/null)" -readonly SSH_PORT="$(echo "${_CONF}" | dasel --null -c '.ssh.port' -p yaml 2>/dev/null)" -readonly SSH_USER="$(echo "${_CONF}" | dasel --null -c '.ssh.user' -p yaml 2>/dev/null)" - -readonly PATH_SOURCE="$(echo "${_CONF}" | dasel --null -c '.path.source' -p yaml 2>/dev/null)" -readonly PATH_REMOTE="$(echo "${_CONF}" | dasel --null -c '.path.remote' -p yaml 2>/dev/null)" - -for var_name in SSH_KEY SSH_HOST SSH_PORT SSH_USER PATH_SOURCE PATH_REMOTE -do - if [[ "${!var_name}" == '' ]] - then - echo "ERROR: Config file syntax is invalid." >&2 - exit 1 - fi -done + SSH_KEY="$(_get_config_value "${conf}" '.ssh.key')" + SSH_HOST="$(_get_config_value "${conf}" '.ssh.host')" + SSH_PORT="$(_get_config_value "${conf}" '.ssh.port')" + SSH_USER="$(_get_config_value "${conf}" '.ssh.user')" + + PATH_SOURCE="$(_get_config_value "${conf}" '.path.source')" + PATH_REMOTE="$(_get_config_value "${conf}" '.path.remote')" + for var_name in SSH_KEY SSH_HOST SSH_PORT SSH_USER PATH_SOURCE PATH_REMOTE; do + if [[ "${!var_name}" == '' ]]; then + _error 'Config file syntax is invalid.' + exit 1 + fi + done + + export SSH_KEY + export SSH_HOST + export SSH_PORT + export SSH_USER + export PATH_SOURCE + export PATH_REMOTE +} + +return 0 # FIXME: Remove # # Parse ssh connection parameters @@ -408,7 +412,7 @@ RSYNC_OPTIONS+=( # # Parse info flags # -readonly info_json="$(echo "${_CONF}" | dasel -c -p yaml -r yaml -w json '.rsync.info' 2>/dev/null || echo "[\"${DEFAULT_INFO}\"]")" +readonly info_json="$(echo "${_CONF:-}" | dasel -c -p yaml -r yaml -w json '.rsync.info' 2>/dev/null || echo "[\"${DEFAULT_INFO}\"]")" RSYNC_OPTIONS+=( --info # fine-grained informational verbosity diff --git a/test/test_config.bats b/test/test_config.bats index dce4dd3..26e9c2a 100644 --- a/test/test_config.bats +++ b/test/test_config.bats @@ -8,9 +8,21 @@ setup() { } teardown() { - rm -f "${TMPFILE}" + set -e + rm -f "${TMPFILE}" "${PROJECT_ROOT:?}/test/bin/envsubst" + + set +e unset CONFIG_FILE + unset DASEL_VER + unset YQ_VER unset TMPFILE + + unset SSH_KEY + unset SSH_HOST + unset SSH_PORT + unset SSH_USER + unset PATH_SOURCE + unset PATH_REMOTE } # get_config_file -------------------------------------------------------------- @@ -48,7 +60,11 @@ teardown() { export CONFIG_FILE="${TMPFILE}" # MOCK - envsubst() { echo "lorem ipsum config:$( "${PROJECT_ROOT}/test/bin/envsubst" + chmod a+x "${PROJECT_ROOT}/test/bin/envsubst" # WHEN run get_config_file @@ -57,3 +73,59 @@ teardown() { assert_success assert_output "lorem ipsum config:${given_config_contents}" } + +# load_config ------------------------------------------------------------------ + +@test "load_config should load config file using dasel" { + # GIVEN + export DASEL_VER='irrelevant' + export YQ_VER="${__NO_VALUE__:?}" + export CONFIG_FILE="${PROJECT_ROOT:?}/src/example.config.yml" + + assert_equal "${SSH_KEY:-}" '' + assert_equal "${SSH_HOST:-}" '' + assert_equal "${SSH_PORT:-}" '' + assert_equal "${SSH_USER:-}" '' + assert_equal "${PATH_SOURCE:-}" '' + assert_equal "${PATH_REMOTE:-}" '' + + # WHEN + load_config + + # THEN + assert_equal $? 0 + # shellcheck disable=SC2088 + assert_equal "${SSH_KEY}" '~/.ssh/id_rda' + assert_equal "${SSH_HOST}" 'example.com' + assert_equal "${SSH_PORT}" '22' + assert_equal "${SSH_USER}" 'johndoe' + assert_equal "${PATH_SOURCE}" '/tmp/' + assert_equal "${PATH_REMOTE}" 'tmp/' +} + +@test "load_config should load config file using yq" { + # GIVEN + export DASEL_VER="${__NO_VALUE__:?}" + export YQ_VER='irrelevant' + export CONFIG_FILE="${PROJECT_ROOT:?}/src/example.config.yml" + + assert_equal "${SSH_KEY:-}" '' + assert_equal "${SSH_HOST:-}" '' + assert_equal "${SSH_PORT:-}" '' + assert_equal "${SSH_USER:-}" '' + assert_equal "${PATH_SOURCE:-}" '' + assert_equal "${PATH_REMOTE:-}" '' + + # WHEN + load_config + + # THEN + assert_equal $? 0 + # shellcheck disable=SC2088 + assert_equal "${SSH_KEY}" '~/.ssh/id_rda' + assert_equal "${SSH_HOST}" 'example.com' + assert_equal "${SSH_PORT}" '22' + assert_equal "${SSH_USER}" 'johndoe' + assert_equal "${PATH_SOURCE}" '/tmp/' + assert_equal "${PATH_REMOTE}" 'tmp/' +} diff --git a/test/test_helper/common-setup.bash b/test/test_helper/common-setup.bash index 26af4a2..6b89021 100644 --- a/test/test_helper/common-setup.bash +++ b/test/test_helper/common-setup.bash @@ -8,12 +8,10 @@ _common_setup() { # use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0, # as those will point to the bats executable's location or the preprocessed file respectively - # shellcheck disable=SC2154 - local project_root="$(cd "$(dirname "${BATS_TEST_FILENAME}")/.." >/dev/null 2>&1 && pwd)" - PATH="${project_root}/src:${project_root}/test/bin:${PATH}" + export PROJECT_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:?}")/.." >/dev/null 2>&1 && pwd)" + PATH="${PROJECT_ROOT}/src:${PROJECT_ROOT}/test/bin:${PATH}" - # shellcheck disable=SC2034 - PATH_BACKUP="${PATH}" + export PATH_BACKUP="${PATH}" source rsync_offsite_backup.sh } diff --git a/test/test_list_active_jobs.bats b/test/test_list_active_jobs.bats index 1748731..0c9b889 100644 --- a/test/test_list_active_jobs.bats +++ b/test/test_list_active_jobs.bats @@ -8,7 +8,6 @@ get_active_jobs_stdout=( setup() { load 'test_helper/common-setup' - load 'test_helper/mocks/stub' _common_setup }