1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-11-24 21:57:35 +00:00

Manage completion with scripts/hosts-completion.

This commit is contained in:
William Melody 2020-04-14 09:26:53 -07:00
parent 8cbbdc3550
commit 3d65bccdb5
8 changed files with 254 additions and 142 deletions

View File

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

View File

@ -44,6 +44,15 @@ To install with [bpkg](https://github.com/bpkg/bpkg):
bpkg install xwmx/hosts
```
#### Make
To install with [Make](https://en.wikipedia.org/wiki/Make_(software)),
clone this repository, navigate to the clone's root directory, and run:
```bash
make install
```
### Manual
To install manually, simply add the `hosts` script to your `$PATH`. If
@ -59,10 +68,11 @@ A package for Arch users is also
### Tab Completion
`hosts` supports tab completion for bash and zsh. If you install `hosts`
with Homebrew, the completion scripts will be installed automatically.
When installing `hosts` through other methods, [completion can be
enabled with a few commands](etc/README.md).
#### Tab Completion
Bash and Zsh tab completion is enabled when `hosts` is installed using
Homebrew, npm, bpkg, or Make. If you are installing `hosts` manually,
[completion can be enabled with a few commands](etc/README.md).
## Usage

View File

@ -1,27 +1,61 @@
# Installation instructions
# Completion Installation
## Homebrew
Installing via Homebrew with `brew install xwmx/taps/hosts` will also
install the completion scripts. The extra steps to install `hosts` completion
scripts outlined below are *not needed*.
install the completion scripts.
A one-time setup might be needed to [enable completion for all Homebrew
programs](https://docs.brew.sh/Shell-Completion).
## npm
## npm, bpkg, Make
Installing via npm should install the completion scripts. If completion
isn't working, try the instructions below.
When `hosts` is installed with `npm`, `bpkg`, or Make, an install hook will
check the environment and attempt to install completions. If it's successful,
you should see a message similar to:
## Scripts
```bash
Completion installed: /usr/local/etc/bash_completion.d/hosts-completion.bash
Completion installed: /usr/local/share/zsh/site-functions/_hosts
```
`hosts` includes scripts for installing and uninstalling completions.
If completion is working after installing through any of these methods, then
you don't need to do anything else.
- [install-completion.bash](../scripts/install-completion.bash)
- [uninstall-completion.bash](../scripts/uninstall-completion.bash)
## `scripts/hosts-completion`
These scripts will try to determine the completion installation
`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:
```bash
./hosts-completion
```
To install completions:
```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-completion.bash
Exists: /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.

View File

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

View File

@ -1,45 +0,0 @@
#!/usr/bin/env bash
###############################################################################
# uninstall.bash
###############################################################################
###############################################################################
# Strict Mode
###############################################################################
set -o nounset
set -o errexit
trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR
set -o errtrace
set -o pipefail
IFS=$'\n\t'
_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
_bash_completion_path="$(
pkg-config --variable=completionsdir bash-completion 2>/dev/null || true
)"
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:-}"
} && _get_bash_completion_path

191
scripts/hosts-completion Executable file
View File

@ -0,0 +1,191 @@
#!/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 "$@"

View File

@ -1,42 +0,0 @@
#!/usr/bin/env bash
###############################################################################
# install-completion.bash
###############################################################################
set -o nounset
set -o errexit
trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR
set -o errtrace
set -o pipefail
IFS=$'\n\t'
_MY_DIR="$(cd "$(dirname "$0")"; pwd)"
if [[ -z "${_MY_DIR}" ]] && [[ ! -d "${_MY_DIR}" ]]
then
exit 1
fi
_install_completion() {
local _bash_completion_path=
_bash_completion_path="$("${_MY_DIR}/get-bash-completion-path.bash")"
if [[ -n "${_bash_completion_path:-}" ]] &&
[[ -d "${_bash_completion_path}" ]] &&
[[ ! -e "${_bash_completion_path}/notes-completion.bash" ]]
then
cp \
"${_MY_DIR}/../etc/notes-completion.bash" \
"${_bash_completion_path}/notes-completion.bash"
fi
local _zsh_completion_path="/usr/local/share/zsh/site-functions"
if [[ -d "${_zsh_completion_path}" ]] &&
[[ ! -e "${_zsh_completion_path}/_notes" ]]
then
cp \
"${_MY_DIR}/../etc/notes-completion.zsh" \
"${_zsh_completion_path}/_notes"
fi
} && _install_completion "$@"

View File

@ -1,38 +0,0 @@
#!/usr/bin/env bash
###############################################################################
# uninstall-completion.bash
###############################################################################
set -o nounset
set -o errexit
trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR
set -o errtrace
set -o pipefail
IFS=$'\n\t'
_MY_DIR="$(cd "$(dirname "$0")"; pwd)"
if [[ -z "${_MY_DIR}" ]] && [[ ! -d "${_MY_DIR}" ]]
then
exit 1
fi
_uninstall_completion() {
local _bash_completion_path=
_bash_completion_path="$("${_MY_DIR}/get-bash-completion-path.bash")"
if [[ -n "${_bash_completion_path:-}" ]] &&
[[ -d "${_bash_completion_path}" ]] &&
[[ -e "${_bash_completion_path}/notes-completion.bash" ]]
then
rm "${_bash_completion_path}/notes-completion.bash"
fi
local _zsh_completion_path="/usr/local/share/zsh/site-functions"
if [[ -d "${_zsh_completion_path}" ]] &&
[[ -e "${_zsh_completion_path}/_notes" ]]
then
rm "${_zsh_completion_path}/_notes"
fi
} && _uninstall_completion "$@"