From 6e751065deec982e8adf08d951a7cbe015a3aa30 Mon Sep 17 00:00:00 2001 From: William Melody Date: Sun, 17 May 2020 10:54:03 -0700 Subject: [PATCH] Improve `backups` argument handling. --- hosts | 76 +++++++++++++++++++++++------------------------ test/backups.bats | 8 +++-- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/hosts b/hosts index be4ada8..a08ba60 100755 --- a/hosts +++ b/hosts @@ -768,11 +768,29 @@ Exit status: 1 Invalid parameters or backup not found. HEREDOC backups() { - local _subcommand="${1:-}" - shift - - local _hosts_dirname + local _filename= + local _hosts_dirname= _hosts_dirname="$(dirname "${HOSTS_PATH}")" + local _skip_backup=0 + local _subcommand= + + for __arg in "${@:-}" + do + case "${__arg}" in + --skip-backup) + _skip_backup=1 + ;; + create|compare|delete|restore|show) + _subcommand="${__arg}" + ;; + *) + if [[ -z "${_filename:-}" ]] + then + _filename="${__arg}" + fi + ;; + esac + done case "${_subcommand}" in create) @@ -785,24 +803,6 @@ backups() { printf "Backed up to %s--backup-%s\\n" "${HOSTS_PATH}" "${_timestamp}" ;; compare) - local _filename - local _use_diff=0 - - for __arg in "${@:-}" - do - case "${__arg}" in - --diff) - _use_diff=1 - ;; - *) - if [[ -z "${_filename:-}" ]] - then - _filename="${__arg}" - fi - ;; - esac - done - if [[ -z "${_filename:-}" ]] then help backups @@ -816,7 +816,7 @@ backups() { diff -u "${HOSTS_PATH}" "${_hosts_dirname}/${_filename}" ;; delete) - if [[ -z "${1:-}" ]] + if [[ -z "${_filename:-}" ]] then help backups exit 1 @@ -824,49 +824,49 @@ backups() { _verify_write_permissions "$@" - if [[ "${HOSTS_PATH}" != "${_hosts_dirname}/${1:-}" ]] && - [[ -e "${_hosts_dirname}/${1:-}" ]] + if [[ "${HOSTS_PATH}" != "${_hosts_dirname}/${_filename:-}" ]] && + [[ -e "${_hosts_dirname}/${_filename:-}" ]] then - rm "${_hosts_dirname}/${1:-}" && - printf "Backup deleted: %s\\n" "${_hosts_dirname}/${1:-}" + rm "${_hosts_dirname}/${_filename:-}" && + printf "Backup deleted: %s\\n" "${_hosts_dirname}/${_filename:-}" else - printf "Backup not found: %s\\n" "${1:-}" + printf "Backup not found: %s\\n" "${_filename:-}" exit 1 fi ;; restore) - if [[ -z "${1:-}" ]] + if [[ -z "${_filename:-}" ]] then help backups exit 1 - elif [[ ! -e "${_hosts_dirname}/${1}" ]] + elif [[ ! -e "${_hosts_dirname}/${_filename}" ]] then - printf "Backup not found: %s\\n" "${1:-}" + printf "Backup not found: %s\\n" "${_filename:-}" exit 1 fi _verify_write_permissions "$@" - if [[ "${2:-}" != "--skip-backup" ]] + if ! ((_skip_backup)) then backups create fi - cat "${_hosts_dirname}/${1}" > "${HOSTS_PATH}" && - printf "Restored from backup: %s\\n" "${1}" + cat "${_hosts_dirname}/${_filename}" > "${HOSTS_PATH}" && + printf "Restored from backup: %s\\n" "${_filename}" ;; show) - if [[ -z "${1:-}" ]] + if [[ -z "${_filename:-}" ]] then help backups exit 1 - elif [[ ! -e "${_hosts_dirname}/${1}" ]] + elif [[ ! -e "${_hosts_dirname}/${_filename}" ]] then - printf "Backup not found: %s\\n" "${1:-}" + printf "Backup not found: %s\\n" "${_filename:-}" exit 1 fi - cat "${_hosts_dirname}/${1:-}" + cat "${_hosts_dirname}/${_filename:-}" ;; *) local _filenames=() diff --git a/test/backups.bats b/test/backups.bats index ebe263d..5d18a0a 100644 --- a/test/backups.bats +++ b/test/backups.bats @@ -85,7 +85,7 @@ No backups found. Create a new backup: run "${_HOSTS}" add 0.0.0.0 example.com } - run "${_HOSTS}" backups compare "${_backup_basename}" --diff + run "${_HOSTS}" backups compare "${_backup_basename}" printf "\${output}: '%s'\\n" "${output}" printf "\${lines[1]}: '%s'\\n" "${lines[1]}" [[ ${status} -eq 1 ]] @@ -100,7 +100,7 @@ No backups found. Create a new backup: run "${_HOSTS}" add 0.0.0.0 example.com } - run "${_HOSTS}" backups compare --diff + run "${_HOSTS}" backups compare printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" [[ ${status} -eq 1 ]] @@ -115,7 +115,7 @@ No backups found. Create a new backup: run "${_HOSTS}" add 0.0.0.0 example.com } - run "${_HOSTS}" backups compare "invalid-backup-name" --diff + run "${_HOSTS}" backups compare "invalid-backup-name" printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" [[ ${status} -eq 1 ]] @@ -128,6 +128,7 @@ No backups found. Create a new backup: { run "${_HOSTS}" backups create _backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')" + [[ -e "${_backup_path}" ]] _backup_basename="$(basename "${_backup_path}")" } @@ -230,6 +231,7 @@ No backups found. Create a new backup: { run "${_HOSTS}" backups create _backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')" + [[ -e "${_backup_path:-}" ]] _backup_basename="$(basename "${_backup_path}")" }