1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-11-24 05:37:38 +00:00

Avoid relying on hosts being in $PATH.

gh-8
This commit is contained in:
William Melody 2020-03-30 17:41:51 -07:00
parent b44b69f83d
commit 5916a18908
4 changed files with 93 additions and 45 deletions

104
hosts
View File

@ -485,7 +485,10 @@ _verify_write_permissions() {
then then
if ((_AUTO_SUDO)) if ((_AUTO_SUDO))
then then
sudo "${_ME}" "${_CMD}" "${_COMMAND_PARAMETERS[@]:-}" local _my_path
_my_path="$(cd "$(dirname "$0")"; pwd)/${_ME}"
sudo "${_my_path}" "${_CMD}" "${_COMMAND_PARAMETERS[@]:-}"
exit $? exit $?
else else
_die printf \ _die printf \
@ -713,12 +716,12 @@ add() {
if [[ -z "${_ip:-}" ]] if [[ -z "${_ip:-}" ]]
then then
"${_ME}" help add help add
exit 1 exit 1
elif [[ -z "${_hostname:-}" ]] elif [[ -z "${_hostname:-}" ]]
then then
printf "Please include a hostname\\n" printf "Please include a hostname\\n"
"${_ME}" help add help add
exit 1 exit 1
elif grep \ elif grep \
-e "^${_ip}\\t${_hostname}$" \ -e "^${_ip}\\t${_hostname}$" \
@ -794,28 +797,52 @@ 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)
if [[ -z "${1:-}" ]] 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 then
"$_ME" 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
local _difftool="diff" local _difftool="diff"
if command -v git &>/dev/null
if ! ((_use_diff))
then then
_difftool="$(git config --get merge.tool)" if command -v git &>/dev/null
then
local _git_merge_tool
_git_merge_tool="$(git config --get merge.tool)"
[[ -n "${_git_merge_tool}" ]] && _difftool="${_git_merge_tool}"
fi
fi fi
"${_difftool}" "${HOSTS_PATH}" "${_hosts_dirname}/${1}" "${_difftool}" "${HOSTS_PATH}" "${_hosts_dirname}/${_filename}"
;; ;;
delete) delete)
if [[ -z "${1:-}" ]] if [[ -z "${1:-}" ]]
then then
"$_ME" help backups help backups
exit 1 exit 1
fi fi
@ -834,7 +861,7 @@ backups() {
restore) restore)
if [[ -z "${1:-}" ]] if [[ -z "${1:-}" ]]
then then
"$_ME" help backups help backups
exit 1 exit 1
elif [[ ! -e "${_hosts_dirname}/${1}" ]] elif [[ ! -e "${_hosts_dirname}/${1}" ]]
then then
@ -846,7 +873,7 @@ backups() {
if [[ "${2:-}" != "--skip-backup" ]] if [[ "${2:-}" != "--skip-backup" ]]
then then
"${_ME}" backups create backups create
fi fi
cat "${_hosts_dirname}/${1}" > "${HOSTS_PATH}" && cat "${_hosts_dirname}/${1}" > "${HOSTS_PATH}" &&
@ -855,7 +882,7 @@ backups() {
show) show)
if [[ -z "${1:-}" ]] if [[ -z "${1:-}" ]]
then then
"$_ME" help backups help backups
exit 1 exit 1
elif [[ ! -e "${_hosts_dirname}/${1}" ]] elif [[ ! -e "${_hosts_dirname}/${1}" ]]
then then
@ -909,16 +936,16 @@ block() {
if [[ -z "${1:-}" ]] if [[ -z "${1:-}" ]]
then then
"${_ME}" help block help block
exit 1 exit 1
fi fi
for __hostname in "${@}" for __hostname in "${@}"
do do
"${_ME}" add 127.0.0.1 "${__hostname}" add 127.0.0.1 "${__hostname}"
# block IPv6 # block IPv6
"${_ME}" add "fe80::1%lo0" "${__hostname}" add "fe80::1%lo0" "${__hostname}"
"${_ME}" add "::1" "${__hostname}" add "::1" "${__hostname}"
done done
} }
@ -939,7 +966,7 @@ disable() {
if [[ -z "${_search_string:-}" ]] if [[ -z "${_search_string:-}" ]]
then then
"${_ME}" help disable help disable
exit 1 exit 1
else else
_debug printf "disable() \${_search_string}: %s\\n" "${_search_string}" _debug printf "disable() \${_search_string}: %s\\n" "${_search_string}"
@ -993,7 +1020,7 @@ Description:
List all disabled records. This is an alias for \`hosts list disabled\`. List all disabled records. This is an alias for \`hosts list disabled\`.
HEREDOC HEREDOC
disabled() { disabled() {
"${_ME}" list disabled list disabled
} }
# ------------------------------------------------------------------------ edit # ------------------------------------------------------------------------ edit
@ -1033,7 +1060,7 @@ enable() {
if [[ -z "${_search_string:-}" ]] if [[ -z "${_search_string:-}" ]]
then then
"${_ME}" help enable help enable
exit 1 exit 1
else else
_debug printf "enable() \${_search_string}: %s\\n" "${_search_string}" _debug printf "enable() \${_search_string}: %s\\n" "${_search_string}"
@ -1087,7 +1114,7 @@ Description:
List all enabled records. This is an alias for \`hosts list enabled\`. List all enabled records. This is an alias for \`hosts list enabled\`.
HEREDOC HEREDOC
enabled() { enabled() {
"${_ME}" list enabled list enabled
} }
# ------------------------------------------------------------------------ file # ------------------------------------------------------------------------ file
@ -1130,7 +1157,7 @@ list() {
then then
grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}" grep -v -e '^$' -e '^\s*\#' "${HOSTS_PATH}"
else else
"${_ME}" show "${1}" show "${1}"
fi fi
else else
# NOTE: use separate expressions since using a | for the or results in # NOTE: use separate expressions since using a | for the or results in
@ -1172,7 +1199,7 @@ remove() {
_debug printf "remove() \${1}: %s\\n" "${1:-}" _debug printf "remove() \${1}: %s\\n" "${1:-}"
_debug printf "remove() \${2}: %s\\n" "${2:-}" _debug printf "remove() \${2}: %s\\n" "${2:-}"
for __arg in "${_COMMAND_ARGV[@]:-}" for __arg in "${@:-}"
do do
case "${__arg}" in case "${__arg}" in
--force) --force)
@ -1184,23 +1211,22 @@ remove() {
esac esac
done done
_debug printf "remove() \${arguments[0]}: %s\\n" "${_arguments[0]:-}" _debug printf "remove() \${arguments[1]}: %s\\n" "${_arguments[0]:-}"
_debug printf "remove() \${arguments[1]}: %s\\n" "${_arguments[1]:-}" _debug printf "remove() \${arguments[2]}: %s\\n" "${_arguments[1]:-}"
_debug printf "remove() \${arguments[2]}: %s\\n" "${_arguments[2]:-}"
if [[ -z "${_arguments[1]:-}" ]] if [[ -z "${_arguments[0]:-}" ]]
then then
"${_ME}" help remove help remove
exit 1 exit 1
elif [[ -n "${_arguments[2]:-}" ]] elif [[ -n "${_arguments[1]:-}" ]]
then then
_search_ip="${_arguments[1]}" _search_ip="${_arguments[0]}"
_search_hostname="${_arguments[2]}" _search_hostname="${_arguments[1]}"
_is_search_pair=1 _is_search_pair=1
_debug printf "remove() \${_is_search_pair}: %s\\n" "${_is_search_pair}" _debug printf "remove() \${_is_search_pair}: %s\\n" "${_is_search_pair}"
else else
_search_string="${_arguments[1]:-}" _search_string="${_arguments[0]:-}"
_debug printf "remove() \${_search_string}: %s\\n" "${_search_string}" _debug printf "remove() \${_search_string}: %s\\n" "${_search_string}"
fi fi
@ -1302,7 +1328,7 @@ HEREDOC
search() { search() {
if _blank "${_COMMAND_ARGV[1]:-}" if _blank "${_COMMAND_ARGV[1]:-}"
then then
"${_ME}" help "search" help "search"
return 1 return 1
fi fi
@ -1342,7 +1368,7 @@ show() {
printf "%s\\n" "${_disabled_records}" printf "%s\\n" "${_disabled_records}"
fi fi
else else
"${_ME}" help show help show
exit 1 exit 1
fi fi
} }
@ -1361,16 +1387,16 @@ unblock() {
if [[ -z "${1:-}" ]] if [[ -z "${1:-}" ]]
then then
"${_ME}" help unblock help unblock
exit 1 exit 1
fi fi
for __hostname in "${@}" for __hostname in "${@}"
do do
"${_ME}" remove 127.0.0.1 "${__hostname}" --force remove 127.0.0.1 "${__hostname}" --force
# unblock IPv6 # unblock IPv6
"${_ME}" remove "fe80::1%lo0" "${__hostname}" --force remove "fe80::1%lo0" "${__hostname}" --force
"${_ME}" remove "::1" "${__hostname}" --force remove "::1" "${__hostname}" --force
done done
} }

View File

@ -75,18 +75,34 @@ No backups found. Create a new backup:
# `hosts backups compare` ##################################################### # `hosts backups compare` #####################################################
@test "\`backups compare\` with valid backup exits with status 0 and prints." { @test "\`backups compare\` with valid backup exits with status 1 and prints." {
{ {
run "${_HOSTS}" backups create run "${_HOSTS}" backups create
_backup_path="$(echo "${output}" | sed -e 's/.*\(\/tmp.*\)/\1/')" _backup_path="$(echo "${output}" | sed -e 's/.*\(\/tmp.*\)/\1/')"
_backup_basename="$(basename "${_backup_path}")" _backup_basename="$(basename "${_backup_path}")"
run "${_HOSTS}" add 0.0.0.0 example.com
} }
run "${_HOSTS}" backups show "${_backup_basename}" run "${_HOSTS}" backups compare "${_backup_basename}" --diff
printf "\${output}: '%s'\\n" "${output}" printf "\${output}: '%s'\\n" "${output}"
printf "\${lines[6]}: '%s'\\n" "${lines[6]}" printf "\${lines[1]}: '%s'\\n" "${lines[1]}"
[[ ${status} -eq 0 ]] [[ ${status} -eq 1 ]]
[[ "${lines[6]}" == '127.0.0.1 localhost' ]] [[ "${lines[1]}" == '< 0.0.0.0 example.com' ]]
}
@test "\`backups compare\` with missing backup exits with status 1" {
{
run "${_HOSTS}" backups create
_backup_path="$(echo "${output}" | sed -e 's/.*\(\/tmp.*\)/\1/')"
_backup_basename="$(basename "${_backup_path}")"
run "${_HOSTS}" add 0.0.0.0 example.com
}
run "${_HOSTS}" backups compare --diff
printf "\${status}: %s\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
[[ ${status} -eq 1 ]]
[[ "${lines[0]}" =~ 'Usage' ]]
} }
@test "\`backups compare\` with invalid backup exits with status 1" { @test "\`backups compare\` with invalid backup exits with status 1" {
@ -94,9 +110,10 @@ No backups found. Create a new backup:
run "${_HOSTS}" backups create run "${_HOSTS}" backups create
_backup_path="$(echo "${output}" | sed -e 's/.*\(\/tmp.*\)/\1/')" _backup_path="$(echo "${output}" | sed -e 's/.*\(\/tmp.*\)/\1/')"
_backup_basename="$(basename "${_backup_path}")" _backup_basename="$(basename "${_backup_path}")"
run "${_HOSTS}" add 0.0.0.0 example.com
} }
run "${_HOSTS}" backups compare "invalid-backup-name" run "${_HOSTS}" backups compare "invalid-backup-name" --diff
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 ]]

1
test/fixtures/bin/hosts vendored Executable file
View File

@ -0,0 +1 @@
#!/usr/bin/env bash

View File

@ -18,6 +18,10 @@ setup() {
cat "${BATS_TEST_DIRNAME}/fixtures/hosts" > "${_HOSTS_TEMP_PATH}" cat "${BATS_TEST_DIRNAME}/fixtures/hosts" > "${_HOSTS_TEMP_PATH}"
export HOSTS_PATH="${_HOSTS_TEMP_PATH}" export HOSTS_PATH="${_HOSTS_TEMP_PATH}"
# Use empty `hosts` script in environment to avoid depending on `hosts`
# being available in `$PATH`.
export PATH="${BATS_TEST_DIRNAME}/fixtures/bin:${PATH}"
} }
teardown() { teardown() {