mirror of
https://github.com/octoleo/hosts.git
synced 2024-12-11 05:42:25 +00:00
192 lines
4.9 KiB
Bash
Executable File
192 lines
4.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
###############################################################################
|
|
# hosts / scripts / hosts-completion
|
|
#
|
|
# Install and uninstall completions for `hosts`.
|
|
#
|
|
# https://github.com/xwmx/hosts
|
|
###############################################################################
|
|
|
|
###############################################################################
|
|
# Strict Mode
|
|
#
|
|
# More Information:
|
|
# https://github.com/xwmx/bash-boilerplate#bash-strict-mode
|
|
###############################################################################
|
|
|
|
set -o nounset
|
|
set -o errexit
|
|
set -o errtrace
|
|
set -o pipefail
|
|
IFS=$'\n\t'
|
|
|
|
###############################################################################
|
|
|
|
export _MY_DIR=
|
|
_MY_DIR="$(cd "$(dirname "$0")"; pwd)"
|
|
if [[ -z "${_MY_DIR}" ]] || [[ ! -d "${_MY_DIR}" ]]
|
|
then
|
|
exit 1
|
|
fi
|
|
|
|
# Usage: _get_bash_completion_path
|
|
_get_bash_completion_path() {
|
|
local _bash_completion_path=
|
|
|
|
if [[ -n "${BASH_COMPLETION_COMPAT_DIR:-}" ]] &&
|
|
[[ -w "${BASH_COMPLETION_COMPAT_DIR:-}" ]]
|
|
then
|
|
_bash_completion_path="${BASH_COMPLETION_COMPAT_DIR}"
|
|
fi
|
|
|
|
if [[ -z "${_bash_completion_path:-}" ]]
|
|
then
|
|
local _maybe_path
|
|
_maybe_path="$(
|
|
pkg-config --variable=completionsdir bash-completion 2>/dev/null || true
|
|
)"
|
|
|
|
if [[ -n "${_maybe_path:-}" ]] &&
|
|
[[ -w "${_maybe_path:-}" ]]
|
|
then
|
|
_bash_completion_path="${_maybe_path}"
|
|
fi
|
|
fi
|
|
|
|
if [[ -z "${_bash_completion_path:-}" ]] &&
|
|
[[ -d "/usr/local/etc/bash_completion.d" ]] &&
|
|
[[ -w "/usr/local/etc/bash_completion.d" ]]
|
|
then
|
|
_bash_completion_path="/usr/local/etc/bash_completion.d"
|
|
fi
|
|
|
|
if [[ -z "${_bash_completion_path:-}" ]] &&
|
|
[[ -d "/etc/bash_completion.d" ]] &&
|
|
[[ -w "/etc/bash_completion.d" ]]
|
|
then
|
|
_bash_completion_path="/etc/bash_completion.d"
|
|
fi
|
|
|
|
printf "%s\\n" "${_bash_completion_path:-}"
|
|
}
|
|
|
|
# subcommands ################################################### subcommands #
|
|
|
|
# Usage: _check
|
|
_check() {
|
|
local _bash_completion_path=
|
|
_bash_completion_path="$(_get_bash_completion_path)"
|
|
|
|
local _exists=0
|
|
|
|
if [[ -n "${_bash_completion_path:-}" ]] &&
|
|
[[ -d "${_bash_completion_path}" ]] &&
|
|
[[ -w "${_bash_completion_path}" ]] &&
|
|
[[ -e "${_bash_completion_path}/hosts-completion.bash" ]]
|
|
then
|
|
_exists=1
|
|
printf "Exists: %s\\n" "${_bash_completion_path}/hosts-completion.bash"
|
|
fi
|
|
|
|
local _zsh_completion_path="/usr/local/share/zsh/site-functions"
|
|
|
|
if [[ -d "${_zsh_completion_path}" ]] &&
|
|
[[ -w "${_zsh_completion_path}" ]] &&
|
|
[[ -e "${_zsh_completion_path}/_hosts" ]]
|
|
then
|
|
_exists=1
|
|
printf "Exists: %s\\n" "${_zsh_completion_path}/_hosts"
|
|
fi
|
|
|
|
((_exists)) || return 1
|
|
}
|
|
|
|
# Usage: _install
|
|
_install() {
|
|
local _bash_completion_path=
|
|
_bash_completion_path="$(_get_bash_completion_path)"
|
|
|
|
if [[ -n "${_bash_completion_path:-}" ]] &&
|
|
[[ -d "${_bash_completion_path}" ]] &&
|
|
[[ -w "${_bash_completion_path}" ]] &&
|
|
[[ ! -e "${_bash_completion_path}/hosts-completion.bash" ]]
|
|
then
|
|
cp \
|
|
"${_MY_DIR}/../etc/hosts-completion.bash" \
|
|
"${_bash_completion_path}/hosts-completion.bash"
|
|
printf "Completion installed: %s\\n" \
|
|
"${_bash_completion_path}/hosts-completion.bash"
|
|
fi
|
|
|
|
local _zsh_completion_path="/usr/local/share/zsh/site-functions"
|
|
|
|
if [[ -d "${_zsh_completion_path}" ]] &&
|
|
[[ -w "${_zsh_completion_path}" ]] &&
|
|
[[ ! -e "${_zsh_completion_path}/_hosts" ]]
|
|
then
|
|
cp \
|
|
"${_MY_DIR}/../etc/hosts-completion.zsh" \
|
|
"${_zsh_completion_path}/_hosts"
|
|
printf "Completion installed: %s\\n" \
|
|
"${_zsh_completion_path}/_hosts"
|
|
fi
|
|
}
|
|
|
|
# Usage: _uninstall
|
|
_uninstall() {
|
|
local _bash_completion_path=
|
|
_bash_completion_path="$(_get_bash_completion_path)"
|
|
|
|
if [[ -n "${_bash_completion_path:-}" ]] &&
|
|
[[ -d "${_bash_completion_path}" ]] &&
|
|
[[ -w "${_bash_completion_path}" ]] &&
|
|
[[ -e "${_bash_completion_path}/hosts-completion.bash" ]]
|
|
then
|
|
rm "${_bash_completion_path}/hosts-completion.bash"
|
|
printf "Completion removed: %s\\n" \
|
|
"${_bash_completion_path}/hosts-completion.bash"
|
|
fi
|
|
|
|
local _zsh_completion_path="/usr/local/share/zsh/site-functions"
|
|
|
|
if [[ -d "${_zsh_completion_path}" ]] &&
|
|
[[ -w "${_zsh_completion_path}" ]] &&
|
|
[[ -e "${_zsh_completion_path}/_hosts" ]]
|
|
then
|
|
rm "${_zsh_completion_path}/_hosts"
|
|
printf "Completion removed: %s\\n" \
|
|
"${_zsh_completion_path}/_hosts"
|
|
fi
|
|
}
|
|
|
|
# main ################################################################# main #
|
|
|
|
# Usage: _main <args>...
|
|
_main() {
|
|
_subcommand="${1:-}"
|
|
|
|
case "${_subcommand}" in
|
|
check)
|
|
_check
|
|
;;
|
|
install)
|
|
_install
|
|
;;
|
|
uninstall)
|
|
_uninstall
|
|
;;
|
|
*)
|
|
cat <<HEREDOC
|
|
hosts-completion
|
|
|
|
Usage:
|
|
hosts-completion [check | install | uninstall]
|
|
|
|
Description:
|
|
Manage completion scripts for \`hosts\`.
|
|
HEREDOC
|
|
;;
|
|
esac
|
|
} && _main "$@"
|
|
|