Improve `backups` argument handling.

This commit is contained in:
William Melody 2020-05-17 10:54:03 -07:00
parent 3e326dad13
commit 6e751065de
2 changed files with 43 additions and 41 deletions

76
hosts
View File

@ -768,11 +768,29 @@ Exit status:
1 Invalid parameters or backup not found. 1 Invalid parameters or backup not found.
HEREDOC HEREDOC
backups() { backups() {
local _subcommand="${1:-}" local _filename=
shift local _hosts_dirname=
local _hosts_dirname
_hosts_dirname="$(dirname "${HOSTS_PATH}")" _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 case "${_subcommand}" in
create) create)
@ -785,24 +803,6 @@ backups() {
printf "Backed up to %s--backup-%s\\n" "${HOSTS_PATH}" "${_timestamp}" printf "Backed up to %s--backup-%s\\n" "${HOSTS_PATH}" "${_timestamp}"
;; ;;
compare) 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:-}" ]] if [[ -z "${_filename:-}" ]]
then then
help backups help backups
@ -816,7 +816,7 @@ backups() {
diff -u "${HOSTS_PATH}" "${_hosts_dirname}/${_filename}" diff -u "${HOSTS_PATH}" "${_hosts_dirname}/${_filename}"
;; ;;
delete) delete)
if [[ -z "${1:-}" ]] if [[ -z "${_filename:-}" ]]
then then
help backups help backups
exit 1 exit 1
@ -824,49 +824,49 @@ backups() {
_verify_write_permissions "$@" _verify_write_permissions "$@"
if [[ "${HOSTS_PATH}" != "${_hosts_dirname}/${1:-}" ]] && if [[ "${HOSTS_PATH}" != "${_hosts_dirname}/${_filename:-}" ]] &&
[[ -e "${_hosts_dirname}/${1:-}" ]] [[ -e "${_hosts_dirname}/${_filename:-}" ]]
then then
rm "${_hosts_dirname}/${1:-}" && rm "${_hosts_dirname}/${_filename:-}" &&
printf "Backup deleted: %s\\n" "${_hosts_dirname}/${1:-}" printf "Backup deleted: %s\\n" "${_hosts_dirname}/${_filename:-}"
else else
printf "Backup not found: %s\\n" "${1:-}" printf "Backup not found: %s\\n" "${_filename:-}"
exit 1 exit 1
fi fi
;; ;;
restore) restore)
if [[ -z "${1:-}" ]] if [[ -z "${_filename:-}" ]]
then then
help backups help backups
exit 1 exit 1
elif [[ ! -e "${_hosts_dirname}/${1}" ]] elif [[ ! -e "${_hosts_dirname}/${_filename}" ]]
then then
printf "Backup not found: %s\\n" "${1:-}" printf "Backup not found: %s\\n" "${_filename:-}"
exit 1 exit 1
fi fi
_verify_write_permissions "$@" _verify_write_permissions "$@"
if [[ "${2:-}" != "--skip-backup" ]] if ! ((_skip_backup))
then then
backups create backups create
fi fi
cat "${_hosts_dirname}/${1}" > "${HOSTS_PATH}" && cat "${_hosts_dirname}/${_filename}" > "${HOSTS_PATH}" &&
printf "Restored from backup: %s\\n" "${1}" printf "Restored from backup: %s\\n" "${_filename}"
;; ;;
show) show)
if [[ -z "${1:-}" ]] if [[ -z "${_filename:-}" ]]
then then
help backups help backups
exit 1 exit 1
elif [[ ! -e "${_hosts_dirname}/${1}" ]] elif [[ ! -e "${_hosts_dirname}/${_filename}" ]]
then then
printf "Backup not found: %s\\n" "${1:-}" printf "Backup not found: %s\\n" "${_filename:-}"
exit 1 exit 1
fi fi
cat "${_hosts_dirname}/${1:-}" cat "${_hosts_dirname}/${_filename:-}"
;; ;;
*) *)
local _filenames=() local _filenames=()

View File

@ -85,7 +85,7 @@ No backups found. Create a new backup:
run "${_HOSTS}" add 0.0.0.0 example.com 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 "\${output}: '%s'\\n" "${output}"
printf "\${lines[1]}: '%s'\\n" "${lines[1]}" printf "\${lines[1]}: '%s'\\n" "${lines[1]}"
[[ ${status} -eq 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}" add 0.0.0.0 example.com
} }
run "${_HOSTS}" backups compare --diff run "${_HOSTS}" backups compare
printf "\${status}: %s\\n" "${status}" printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]] [[ ${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}" 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 "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]] [[ ${status} -eq 1 ]]
@ -128,6 +128,7 @@ No backups found. Create a new backup:
{ {
run "${_HOSTS}" backups create run "${_HOSTS}" backups create
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')" _backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
[[ -e "${_backup_path}" ]]
_backup_basename="$(basename "${_backup_path}")" _backup_basename="$(basename "${_backup_path}")"
} }
@ -230,6 +231,7 @@ No backups found. Create a new backup:
{ {
run "${_HOSTS}" backups create run "${_HOSTS}" backups create
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')" _backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
[[ -e "${_backup_path:-}" ]]
_backup_basename="$(basename "${_backup_path}")" _backup_basename="$(basename "${_backup_path}")"
} }