Skip to content

Commit

Permalink
Add get_config_file function
Browse files Browse the repository at this point in the history
  • Loading branch information
danie1k committed Apr 16, 2022
1 parent 13d0ae8 commit d2f28c7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/rsync_offsite_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -359,24 +359,25 @@ function get_3rd_parties_versions() {
export YQ_VER
}

function get_config_file() {
if [[ ! -r "${CONFIG_FILE}" ]]; then
_error "Given config file path is invalid: '${CONFIG_FILE}'"
exit 1
fi

if which envsubst 1>/dev/null 2>&1; then
envsubst <"${CONFIG_FILE}"
else
cat "${CONFIG_FILE}"
fi
}

return 0 # FIXME: Remove

#
# Read config
#
if [[ ! -r "${CONFIG_FILE}" ]]
then
echo "ERROR: Given config file path is invalid: '${CONFIG_FILE}'" >&2
exit 1
fi

if which envsubst 1>/dev/null 2>&1
then
readonly _CONF="$(envsubst < "${CONFIG_FILE}")"
else
readonly _CONF="$(cat "${CONFIG_FILE}")"
fi

_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)"
Expand Down
59 changes: 59 additions & 0 deletions test/test_config.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bats

setup() {
load 'test_helper/common-setup'
_common_setup

export TMPFILE="$(mktemp)"
}

teardown() {
rm -f "${TMPFILE}"
unset CONFIG_FILE
unset TMPFILE
}

# get_config_file --------------------------------------------------------------

@test "get_config_file should fail with error message for unreadable path" {
# GIVEN
export CONFIG_FILE='./inexistent/path'

# WHEN
run get_config_file

# THEN
assert_failure
assert_output --partial "Given config file path is invalid: '${CONFIG_FILE}'"
}

@test "get_config_file should output config file contents if path is valid" {
# GIVEN
given_config_contents=$'foo bar\nlorem ipsum'
echo "${given_config_contents}" >"${TMPFILE}"
export CONFIG_FILE="${TMPFILE}"

# WHEN
run get_config_file

# THEN
assert_success
assert_output "${given_config_contents}"
}

@test "get_config_file should output config file contents through envsubst" {
# GIVEN
given_config_contents=$'foo bar\nlorem ipsum'
echo "${given_config_contents}" >"${TMPFILE}"
export CONFIG_FILE="${TMPFILE}"

# MOCK
envsubst() { echo "lorem ipsum config:$(</dev/stdin)"; }

# WHEN
run get_config_file

# THEN
assert_success
assert_output "lorem ipsum config:${given_config_contents}"
}

0 comments on commit d2f28c7

Please sign in to comment.