Skip to content

Commit

Permalink
Add print_nice_header function
Browse files Browse the repository at this point in the history
  • Loading branch information
danie1k committed Apr 18, 2022
1 parent 02257f1 commit e1f2187
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 54 deletions.
30 changes: 11 additions & 19 deletions src/rsync_offsite_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,18 @@ function collect_rsync_options() {
export RSYNC_OPTIONS
}

return 0 # FIXME: Remove
function print_nice_header() {
local _dry_run="$([[ ${DRY_RUN_FLAG} -eq 1 ]] && echo 'yes' || echo 'no')"
local _remote_target="${SSH_USER}@${SSH_HOST}:${PATH_REMOTE}"

_header "STARTING RSYNC SESSION" \
"Job name: ${JOB_NAME}" \
"Dry run: ${_dry_run}" \
"Local source: ${PATH_SOURCE}" \
"Remote target: ${_remote_target}"
}

return 0 # FIXME: Remove

#
# Finally, tell rsync about paths to transfer
Expand All @@ -453,24 +463,6 @@ RSYNC_OPTIONS+=(
"${SSH_USER}@${SSH_HOST}:$(printf '%q' "${PATH_REMOTE}")"
)


#
# Print nice header
#
_printf_line=' '
# shellcheck disable=SC2250
_dry_run="$([[ $DRY_RUN_FLAG -eq 1 ]] && echo 'yes' || echo 'no')"
_remote_target="${SSH_USER}@${SSH_HOST}:${PATH_REMOTE}"
echo "┌────────────────────────────────────────────────────────────────────────────┐"
echo "│ STARTING RSYNC SESSION │"
echo "├────────────────────────────────────────────────────────────────────────────┤"
printf "│ Job name: %s%s│\n" "${JOB_NAME}" "${_printf_line:${#JOB_NAME}}"
printf "│ Dry run: %s%s│\n" "${_dry_run}" "${_printf_line:${#_dry_run}}"
printf "│ Local source: %s%s│\n" "${PATH_SOURCE}" "${_printf_line:${#PATH_SOURCE}}"
printf "│ Remote target: %s%s│\n" "${_remote_target}" "${_printf_line:${#_remote_target}}"
echo "└────────────────────────────────────────────────────────────────────────────┘"


#
# Dry run message
#
Expand Down
84 changes: 84 additions & 0 deletions test/test_print_.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env bats

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

# MOCK / GIVEN
export DASEL_VER='42'
export ENVSUBST_VER='42'
export RSYNC_PROTOCOL_VER='42'
export RSYNC_VER='42'
export YQ_VER='42'

export SSH_USER='foo'
export SSH_HOST='bar'
export PATH_REMOTE='lorem/'
export JOB_NAME='ipsum'
export PATH_SOURCE='/given/path/'
}

teardown() {
unset DASEL_VER
unset ENVSUBST_VER
unset RSYNC_PROTOCOL_VER
unset RSYNC_VER
unset YQ_VER

unset DRY_RUN_FLAG
unset JOB_NAME
unset PATH_REMOTE
unset PATH_SOURCE
unset SSH_HOST
unset SSH_USER
}

# print_usage ------------------------------------------------------------------

@test "print_usage result" {
# WHEN
run print_usage

# THEN
assert_success
assert_output --partial 'Usage:'
assert_output --partial 'Options:'
assert_output --partial 'dasel:'
assert_output --partial 'envsubst:'
assert_output --partial 'rsync:'
assert_output --partial 'yq:'
}

# print_nice_header ------------------------------------------------------------

@test "print_nice_header : when dry run is disabled" {
# GIVEN
export DRY_RUN_FLAG=0

# WHEN
run print_nice_header

# THEN
assert_success
assert_output --partial 'STARTING RSYNC SESSION'
assert_output --partial 'Job name: ipsum'
assert_output --partial 'Dry run: no'
assert_output --partial 'Local source: /given/path/'
assert_output --partial 'Remote target: foo@bar:lorem/'
}

@test "print_nice_header : when dry run is enabled" {
# GIVEN
export DRY_RUN_FLAG=1

# WHEN
run print_nice_header

# THEN
assert_success
assert_output --partial 'STARTING RSYNC SESSION'
assert_output --partial 'Job name: ipsum'
assert_output --partial 'Dry run: yes'
assert_output --partial 'Local source: /given/path/'
assert_output --partial 'Remote target: foo@bar:lorem/'
}
35 changes: 0 additions & 35 deletions test/test_print_usage.bats

This file was deleted.

0 comments on commit e1f2187

Please sign in to comment.