Skip to content

Commit

Permalink
Add load_config function
Browse files Browse the repository at this point in the history
  • Loading branch information
danie1k committed Apr 16, 2022
1 parent d2f28c7 commit 127c97e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 30 deletions.
48 changes: 26 additions & 22 deletions src/rsync_offsite_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
76 changes: 74 additions & 2 deletions test/test_config.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------------------------------------------------------
Expand Down Expand Up @@ -48,7 +60,11 @@ teardown() {
export CONFIG_FILE="${TMPFILE}"

# MOCK
envsubst() { echo "lorem ipsum config:$(</dev/stdin)"; }
{
echo '#!/bin/sh'
echo 'echo "lorem ipsum config:$(</dev/stdin)"'
} > "${PROJECT_ROOT}/test/bin/envsubst"
chmod a+x "${PROJECT_ROOT}/test/bin/envsubst"

# WHEN
run get_config_file
Expand All @@ -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/'
}
8 changes: 3 additions & 5 deletions test/test_helper/common-setup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion test/test_list_active_jobs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ get_active_jobs_stdout=(

setup() {
load 'test_helper/common-setup'
load 'test_helper/mocks/stub'
_common_setup
}

Expand Down

0 comments on commit 127c97e

Please sign in to comment.