Skip to content

Commit 742afc0

Browse files
committed
Add load_config function
1 parent d2f28c7 commit 742afc0

File tree

4 files changed

+104
-30
lines changed

4 files changed

+104
-30
lines changed

src/rsync_offsite_backup.sh

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -372,29 +372,33 @@ function get_config_file() {
372372
fi
373373
}
374374

375-
return 0 # FIXME: Remove
375+
function load_config() {
376+
local conf="$(get_config_file)"
376377

377-
#
378-
# Read config
379-
#
380-
_CONF=''
381-
readonly SSH_KEY="$(echo "${_CONF}" | dasel --null -c '.ssh.key' -p yaml 2>/dev/null)"
382-
readonly SSH_HOST="$(echo "${_CONF}" | dasel --null -c '.ssh.host' -p yaml 2>/dev/null)"
383-
readonly SSH_PORT="$(echo "${_CONF}" | dasel --null -c '.ssh.port' -p yaml 2>/dev/null)"
384-
readonly SSH_USER="$(echo "${_CONF}" | dasel --null -c '.ssh.user' -p yaml 2>/dev/null)"
385-
386-
readonly PATH_SOURCE="$(echo "${_CONF}" | dasel --null -c '.path.source' -p yaml 2>/dev/null)"
387-
readonly PATH_REMOTE="$(echo "${_CONF}" | dasel --null -c '.path.remote' -p yaml 2>/dev/null)"
388-
389-
for var_name in SSH_KEY SSH_HOST SSH_PORT SSH_USER PATH_SOURCE PATH_REMOTE
390-
do
391-
if [[ "${!var_name}" == '' ]]
392-
then
393-
echo "ERROR: Config file syntax is invalid." >&2
394-
exit 1
395-
fi
396-
done
378+
SSH_KEY="$(_get_config_value "${conf}" '.ssh.key')"
379+
SSH_HOST="$(_get_config_value "${conf}" '.ssh.host')"
380+
SSH_PORT="$(_get_config_value "${conf}" '.ssh.port')"
381+
SSH_USER="$(_get_config_value "${conf}" '.ssh.user')"
382+
383+
PATH_SOURCE="$(_get_config_value "${conf}" '.path.source')"
384+
PATH_REMOTE="$(_get_config_value "${conf}" '.path.remote')"
397385

386+
for var_name in SSH_KEY SSH_HOST SSH_PORT SSH_USER PATH_SOURCE PATH_REMOTE; do
387+
if [[ "${!var_name}" == '' ]]; then
388+
_error 'Config file syntax is invalid.'
389+
exit 1
390+
fi
391+
done
392+
393+
export SSH_KEY
394+
export SSH_HOST
395+
export SSH_PORT
396+
export SSH_USER
397+
export PATH_SOURCE
398+
export PATH_REMOTE
399+
}
400+
401+
return 0 # FIXME: Remove
398402

399403
#
400404
# Parse ssh connection parameters
@@ -408,7 +412,7 @@ RSYNC_OPTIONS+=(
408412
#
409413
# Parse info flags
410414
#
411-
readonly info_json="$(echo "${_CONF}" | dasel -c -p yaml -r yaml -w json '.rsync.info' 2>/dev/null || echo "[\"${DEFAULT_INFO}\"]")"
415+
readonly info_json="$(echo "${_CONF:-}" | dasel -c -p yaml -r yaml -w json '.rsync.info' 2>/dev/null || echo "[\"${DEFAULT_INFO}\"]")"
412416

413417
RSYNC_OPTIONS+=(
414418
--info # fine-grained informational verbosity

test/test_config.bats

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@ setup() {
88
}
99

1010
teardown() {
11-
rm -f "${TMPFILE}"
11+
set -e
12+
rm -f "${TMPFILE}" "${PROJECT_ROOT:?}/test/bin/envsubst"
13+
14+
set +e
1215
unset CONFIG_FILE
16+
unset DASEL_VER
17+
unset YQ_VER
1318
unset TMPFILE
19+
20+
unset SSH_KEY
21+
unset SSH_HOST
22+
unset SSH_PORT
23+
unset SSH_USER
24+
unset PATH_SOURCE
25+
unset PATH_REMOTE
1426
}
1527

1628
# get_config_file --------------------------------------------------------------
@@ -48,7 +60,12 @@ teardown() {
4860
export CONFIG_FILE="${TMPFILE}"
4961

5062
# MOCK
51-
envsubst() { echo "lorem ipsum config:$(</dev/stdin)"; }
63+
{
64+
echo '#!/bin/sh'
65+
echo 'echo -n "lorem ipsum config:"'
66+
echo 'while read line; do echo "$line"; done < "${1:-/dev/stdin}"'
67+
} > "${PROJECT_ROOT}/test/bin/envsubst"
68+
chmod a+x "${PROJECT_ROOT}/test/bin/envsubst"
5269

5370
# WHEN
5471
run get_config_file
@@ -57,3 +74,59 @@ teardown() {
5774
assert_success
5875
assert_output "lorem ipsum config:${given_config_contents}"
5976
}
77+
78+
# load_config ------------------------------------------------------------------
79+
80+
@test "load_config should load config file using dasel" {
81+
# GIVEN
82+
export DASEL_VER='irrelevant'
83+
export YQ_VER="${__NO_VALUE__:?}"
84+
export CONFIG_FILE="${PROJECT_ROOT:?}/src/example.config.yml"
85+
86+
assert_equal "${SSH_KEY:-}" ''
87+
assert_equal "${SSH_HOST:-}" ''
88+
assert_equal "${SSH_PORT:-}" ''
89+
assert_equal "${SSH_USER:-}" ''
90+
assert_equal "${PATH_SOURCE:-}" ''
91+
assert_equal "${PATH_REMOTE:-}" ''
92+
93+
# WHEN
94+
load_config
95+
96+
# THEN
97+
assert_equal $? 0
98+
# shellcheck disable=SC2088
99+
assert_equal "${SSH_KEY}" '~/.ssh/id_rda'
100+
assert_equal "${SSH_HOST}" 'example.com'
101+
assert_equal "${SSH_PORT}" '22'
102+
assert_equal "${SSH_USER}" 'johndoe'
103+
assert_equal "${PATH_SOURCE}" '/tmp/'
104+
assert_equal "${PATH_REMOTE}" 'tmp/'
105+
}
106+
107+
@test "load_config should load config file using yq" {
108+
# GIVEN
109+
export DASEL_VER="${__NO_VALUE__:?}"
110+
export YQ_VER='irrelevant'
111+
export CONFIG_FILE="${PROJECT_ROOT:?}/src/example.config.yml"
112+
113+
assert_equal "${SSH_KEY:-}" ''
114+
assert_equal "${SSH_HOST:-}" ''
115+
assert_equal "${SSH_PORT:-}" ''
116+
assert_equal "${SSH_USER:-}" ''
117+
assert_equal "${PATH_SOURCE:-}" ''
118+
assert_equal "${PATH_REMOTE:-}" ''
119+
120+
# WHEN
121+
load_config
122+
123+
# THEN
124+
assert_equal $? 0
125+
# shellcheck disable=SC2088
126+
assert_equal "${SSH_KEY}" '~/.ssh/id_rda'
127+
assert_equal "${SSH_HOST}" 'example.com'
128+
assert_equal "${SSH_PORT}" '22'
129+
assert_equal "${SSH_USER}" 'johndoe'
130+
assert_equal "${PATH_SOURCE}" '/tmp/'
131+
assert_equal "${PATH_REMOTE}" 'tmp/'
132+
}

test/test_helper/common-setup.bash

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ _common_setup() {
88
# use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0,
99
# as those will point to the bats executable's location or the preprocessed file respectively
1010

11-
# shellcheck disable=SC2154
12-
local project_root="$(cd "$(dirname "${BATS_TEST_FILENAME}")/.." >/dev/null 2>&1 && pwd)"
13-
PATH="${project_root}/src:${project_root}/test/bin:${PATH}"
11+
export PROJECT_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:?}")/.." >/dev/null 2>&1 && pwd)"
12+
PATH="${PROJECT_ROOT}/src:${PROJECT_ROOT}/test/bin:${PATH}"
1413

15-
# shellcheck disable=SC2034
16-
PATH_BACKUP="${PATH}"
14+
export PATH_BACKUP="${PATH}"
1715

1816
source rsync_offsite_backup.sh
1917
}

test/test_list_active_jobs.bats

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ get_active_jobs_stdout=(
88

99
setup() {
1010
load 'test_helper/common-setup'
11-
load 'test_helper/mocks/stub'
1211
_common_setup
1312
}
1413

0 commit comments

Comments
 (0)