1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-12-27 11:52:42 +00:00

Move completion installation into the main script.

This commit is contained in:
William Melody 2020-06-07 13:13:06 -07:00
parent 17b2615820
commit 23d6a1c653
6 changed files with 323 additions and 233 deletions

View File

@ -3,8 +3,8 @@ PREFIX ?= /usr/local
install:
install $(BIN) $(PREFIX)/bin
./scripts/hosts-completion install
./$(BIN) completions install
uninstall:
rm -f $(PREFIX)/bin/$(BIN)
./scripts/hosts-completion uninstall
./$(BIN) completions uninstall

View File

@ -55,13 +55,45 @@ make install
### Manual
To install manually, simply add the `hosts` script to your `$PATH`. If
you already have a `~/bin` directory, you can use the following command:
To install as an administrator, copy and paste one of the following multi-line
commands:
```bash
# install using wget
sudo wget https://raw.github.com/xwmx/hosts/master/hosts -O /usr/local/bin/hosts &&
sudo chmod +x /usr/local/bin/hosts &&
sudo hosts completions install
# install using curl
sudo curl -L https://raw.github.com/xwmx/hosts/master/hosts -o /usr/local/bin/hosts &&
sudo chmod +x /usr/local/bin/hosts &&
sudo hosts completions install
```
###### User-only Installation
To install with just user permissions, simply add the `hosts` script to your
`$PATH`. If you already have a `~/bin` directory, for example, you can use
one of the following commands:
```bash
# download with wget
wget https://raw.github.com/xwmx/hosts/master/hosts -O ~/bin/hosts && chmod +x ~/bin/hosts
# download with curl
curl -L https://raw.github.com/xwmx/hosts/master/hosts -o ~/bin/hosts && chmod +x ~/bin/hosts
```
Installing with just user permissions doesn't install the completions, but
`hosts` works without them. If you have `sudo` access and want to install the
completion scripts, run the following command:
```bash
sudo hosts completions install
```
### Arch Linux
A package for Arch users is also
[available in the AUR](https://aur.archlinux.org/packages/hosts/).

View File

@ -22,42 +22,41 @@ Completion installed: /usr/local/share/zsh/site-functions/_hosts
If completion is working after installing through any of these methods, then
you don't need to do anything else.
## `scripts/hosts-completion`
## `hosts completions`
`hosts` includes a script for installing and uninstalling `hosts` completions
that is used in installation hooks:
[hosts-completion](../scripts/hosts-completion)
To run this script directly, navigate to this directory in your terminal, and
run:
The `hosts completions` subcommand can be used for installing and uninstalling
completion scripts. Depending on your configuration, you might need to use
`sudo` to install completion scripts easily:
```bash
./hosts-completion
```
> sudo hosts completions check
Completion scripts not found.
To install completions:
> sudo hosts completions install
Completion script installed: /usr/share/bash-completion/completions/hosts
Completion script installed: /usr/local/share/zsh/site-functions/_hosts
```bash
./hosts-completion install
```
To uninstall:
```bash
./hosts-completion uninstall
```
Use the `check` subcommand to determine if completion scripts are installed:
```bash
> ./hosts-completion check
Exists: /usr/local/etc/bash_completion.d/hosts
> sudo hosts completions check
Exists: /usr/share/bash-completion/completions/hosts
Exists: /usr/local/share/zsh/site-functions/_hosts
> sudo hosts completions uninstall
Completion script removed: /usr/share/bash-completion/completions/hosts
Completion script removed: /usr/local/share/zsh/site-functions/_hosts
```
This script will try to determine the completion installation
locations from your environment. If completion doesn't work, you might
need to try installing manually.
If you installed `hosts` manually by downloading just the `hosts` script,
the completion scripts won't be immediately available for
`hosts completions install`. You can try installing the completions with
the `--download` flag, which will get the latest version from GitHub:
```bash
sudo hosts completions install --download
```
`hosts completions` will try to determine the completion script directories
from your environment. If `hosts completions` isn't able to install
the completion scripts, you can try installing them manually.
## Manual Installation

258
hosts
View File

@ -80,6 +80,16 @@ else # macOS
export _SED_I_COMMAND=(sed -i '')
fi
# $_REPO
#
# The <user>/<repo> identifier for the git repository.
_REPO="xwmx/hosts"
# $_REPO_RAW_URL
#
# The base URL for raw files.
_REPO_RAW_URL="https://raw.githubusercontent.com/${_REPO}/master"
###############################################################################
# Debug
###############################################################################
@ -579,6 +589,7 @@ Usage:
${_ME} add <ip> <hostname> [<comment>]
${_ME} backups [create | [compare | delete | restore | show] <filename>]
${_ME} block <hostname>...
${_ME} completions (check | install [-d | --download] | uninstall)
${_ME} disable (<ip> | <hostname> | <search string>)
${_ME} disabled
${_ME} edit
@ -919,6 +930,252 @@ block() {
done
}
# completions ##################################################### completions
desc "completions" <<HEREDOC
Usage:
${_ME} completions (check | install [-d | --download] | uninstall)
Options:
-d, --download Download the completion scripts and install.
Description:
Manage completion scripts. For more information, visit:
https://github.com/${_REPO}/blob/master/etc/README.md
HEREDOC
completions() {
local _BASH_COMP_NAME="hosts"
local _ZSH_COMP_NAME="_hosts"
# Usage: _completions_check
_completions_check() {
local _bash_completion_path=
_bash_completion_path="$(_get_bash_completion_path)"
local _exists=0
if [[ -n "${_bash_completion_path:-}" ]] &&
[[ -d "${_bash_completion_path}" ]]
then
if [[ -w "${_bash_completion_path}" ]]
then
if [[ -e "${_bash_completion_path}/${_BASH_COMP_NAME}" ]]
then
_exists=1
printf "Exists: %s\\n" "${_bash_completion_path}/${_BASH_COMP_NAME}"
fi
else
printf "Permission denied: %s\\n" "${_bash_completion_path}"
fi
fi
local _zsh_completion_path="/usr/local/share/zsh/site-functions"
if [[ -d "${_zsh_completion_path}" ]]
then
if [[ -w "${_zsh_completion_path}" ]]
then
if [[ -e "${_zsh_completion_path}/${_ZSH_COMP_NAME}" ]]
then
_exists=1
printf "Exists: %s\\n" "${_zsh_completion_path}/${_ZSH_COMP_NAME}"
fi
else
printf "Permission denied: %s\\n" "${_zsh_completion_path}"
fi
fi
if ! ((_exists))
then
_exit_1 printf "Completion scripts not found.\\n"
fi
}
# Usage: _get_bash_completion_path
_get_bash_completion_path() {
local _bash_completion_path=
if [[ -n "${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:-}" ]]
then
_bash_completion_path="${_maybe_path}"
fi
fi
if [[ -z "${_bash_completion_path:-}" ]] &&
[[ -d "/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" ]]
then
_bash_completion_path="/etc/bash_completion.d"
fi
printf "%s\\n" "${_bash_completion_path:-}"
}
# Usage: _completions_install [--download]
_completions_install() {
local _download=0
if [[ "${1:-}" == "--download" ]]
then
_download=1
fi
local _my_dir=
_my_dir="$(cd "$(dirname "$(realpath "$0")")"; pwd)"
if [[ -z "${_my_dir}" ]] || [[ ! -d "${_my_dir}" ]]
then
exit 1
fi
if [[ -z "${_REPO:-}" ]] || [[ -z "${_REPO_RAW_URL:-}" ]]
then
_exit_1 printf "Source Git repository not configured.\\n"
fi
for _shell in bash zsh
do
local _completion_source="${_my_dir}/etc/${_ME}-completion.${_shell}"
if ((_download))
then
if [[ ! -f "${_completion_source}" ]]
then
_completion_source="$(mktemp)"
local _completion_url="${_REPO_RAW_URL}/etc/${_ME}-completion.${_shell}"
if ! _download_from "${_completion_url}" "${_completion_source}"
then
_exit_1 printf "Unable to download Completion script from %s\\n" \
"${_completion_source}"
fi
fi
fi
if [[ ! -f "${_completion_source}" ]]
then
cat <<HEREDOC
Unable to find source ${_shell} completion script. You can try downloading
and installing the latest version with the following command (\`sudo\` might
be necessary):
${_ME} completions install --download
More information: ${_shell}
https://github.com/${_REPO}/blob/master/etc/README.md
HEREDOC
else
local _completion_path=
local _completion_target=
if [[ "${_shell}" == "bash" ]]
then
_completion_path="$(_get_bash_completion_path)"
_completion_target="${_completion_path}/${_BASH_COMP_NAME}"
elif [[ "${_shell}" == "zsh" ]]
then
_completion_path="/usr/local/share/zsh/site-functions"
_completion_target="${_completion_path}/${_ZSH_COMP_NAME}"
fi
if [[ -n "${_completion_path:-}" ]] &&
[[ -d "${_completion_path}" ]]
then
if [[ -w "${_completion_path}" ]]
then
if [[ ! -e "${_completion_target}" ]]
then
cp \
"${_completion_source}" \
"${_completion_target}"
chmod +r "${_completion_target}"
printf "Completion script installed: %s\\n" \
"${_completion_target}"
else
printf "Exists: %s\\n" "${_completion_target}"
fi
else
printf "Permission denied: %s\\n" "${_completion_path}"
fi
fi
fi
done
}
# Usage: _completions_uninstall
_completions_uninstall() {
local _completion_path=
local _completion_target=
for _shell in bash zsh
do
if [[ "${_shell}" == "bash" ]]
then
_completion_path="$(_get_bash_completion_path)"
_completion_target="${_completion_path}/${_BASH_COMP_NAME}"
elif [[ "${_shell}" == "zsh" ]]
then
_completion_path="/usr/local/share/zsh/site-functions"
_completion_target="${_completion_path}/${_ZSH_COMP_NAME}"
fi
if [[ -n "${_completion_path:-}" ]] &&
[[ -d "${_completion_path}" ]]
then
if [[ -w "${_completion_path}" ]] &&
[[ -w "${_completion_target}" ]]
then
if [[ -f "${_completion_target}" ]]
then
rm "${_completion_target}"
printf "Completion script removed: %s\\n" \
"${_completion_target}"
fi
else
printf "Permission denied: %s\\n" "${_completion_path}"
fi
fi
done
}
local _subcommand="${1:-}"
case "${_subcommand}" in
check)
_completions_check
;;
install)
if [[ "${2:-}" =~ ^-d|--download$ ]]
then
_completions_install --download
else
_completions_install
fi
;;
uninstall)
_completions_uninstall
;;
*)
help "completions"
return 0
;;
esac
}
# disable ############################################################# disable
desc "disable" <<HEREDOC
@ -1445,6 +1702,7 @@ _SUBCOMMANDS=(
backups
block
commands
completions
disable
disabled
edit

View File

@ -12,8 +12,8 @@
},
"scripts": {
"test": "bats test",
"postinstall": "scripts/hosts-completion install",
"preuninstall": "scripts/hosts-completion uninstall"
"postinstall": "./hosts completions install",
"preuninstall": "./hosts completions uninstall"
},
"repository": {
"type": "git",

View File

@ -1,199 +0,0 @@
#!/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" ]]
then
_exists=1
printf "Exists: %s\\n" "${_bash_completion_path}/hosts"
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" ]]
then
cp \
"${_MY_DIR}/../etc/hosts-completion.bash" \
"${_bash_completion_path}/hosts"
printf "Completion installed: %s\\n" "${_bash_completion_path}/hosts"
# Cleanup old completion script.
if [[ -e "${_bash_completion_path}/hosts-completion.bash" ]]
then
rm "${_bash_completion_path}/hosts-completion.bash"
fi
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" ]]
then
rm "${_bash_completion_path}/hosts"
printf "Completion removed: %s\\n" "${_bash_completion_path}/hosts"
# Cleanup old completion script.
if [[ -e "${_bash_completion_path}/hosts-completion.bash" ]]
then
rm "${_bash_completion_path}/hosts-completion.bash"
fi
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 "$@"