Add support for options in install script
This adds support for serveral new command line options (taken from the usage):
- `-b, --bin-dir Override the bin installation directory`
- `-m, --man-dir Override the man installation directory`
- `-a, --arch Override the architecture identified by the installer`
- `-h, --help Display this help message`
This also (coincidentally) allows for $BIN_DIR, $MAN_DIR, and $ARCH to be set to create default settings for these.
These variables are *always* overwritten by the command line arguments.
This commit is contained in:
parent
3022cf3686
commit
9dd4d4023e
147
install.sh
147
install.sh
|
|
@ -1,11 +1,28 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# shellcheck shell=dash
|
# shellcheck shell=dash
|
||||||
|
# shellcheck disable=SC3043 # Assume `local` extension
|
||||||
|
# vim:set ts=4 sw=4 et:
|
||||||
|
|
||||||
# The official zoxide installer.
|
# The official zoxide installer.
|
||||||
#
|
#
|
||||||
# It runs on Unix shells like {a,ba,da,k,z}sh. It uses the common `local`
|
# It runs on Unix shells like {a,ba,da,k,z}sh. It uses the common `local`
|
||||||
# extension. Note: Most shells limit `local` to 1 var per line, contra bash.
|
# extension. Note: Most shells limit `local` to 1 var per line, contra bash.
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
# Note: here-docs aren't defined in posix
|
||||||
|
printf '%s\n' \
|
||||||
|
"Usage: install.sh [option]" \
|
||||||
|
"Fetch and install the latest version of zoxide, if zoxide is already" \
|
||||||
|
"installed it will be updated to the latest version." \
|
||||||
|
"" \
|
||||||
|
"Options:" \
|
||||||
|
" -b, --bin-dir Override the bin installation directory [default: ${_bin_dir}]" \
|
||||||
|
" -m, --man-dir Override the man installation directory [default: ${_man_dir}]" \
|
||||||
|
" -a, --arch Override the architecture identified by the installer [default: ${_arch}]" \
|
||||||
|
" -h, --help Display this help message" \
|
||||||
|
|| true
|
||||||
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
if [ "${KSH_VERSION-}" = 'Version JM 93t+ 2010-03-05' ]; then
|
if [ "${KSH_VERSION-}" = 'Version JM 93t+ 2010-03-05' ]; then
|
||||||
# The version of ksh93 that ships with many illumos systems does not
|
# The version of ksh93 that ships with many illumos systems does not
|
||||||
|
|
@ -16,12 +33,43 @@ main() {
|
||||||
|
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
# Detect and print host target triple.
|
local _bin_dir="${HOME}/.local/bin"
|
||||||
ensure get_architecture
|
local _bin_name
|
||||||
local _arch="${RETVAL}"
|
local _man_dir="${HOME}/.local/share/man"
|
||||||
|
|
||||||
|
parse_args "$@" # sets global variables (BIN_DIR, MAN_DIR, ARCH)
|
||||||
|
|
||||||
|
_bin_dir=${BIN_DIR:-$_bin_dir}
|
||||||
|
_man_dir=${MAN_DIR:-$_man_dir}
|
||||||
|
|
||||||
|
if [ -n "${ARCH:-}" ]; then
|
||||||
|
# if the user specifed, trust them - don't error on unrecognized hardware.
|
||||||
|
local _arch="${ARCH}"
|
||||||
|
else
|
||||||
|
# Detect and print host target triple.
|
||||||
|
ensure get_architecture
|
||||||
|
local _arch="${RETVAL}"
|
||||||
|
fi
|
||||||
assert_nz "${_arch}" "arch"
|
assert_nz "${_arch}" "arch"
|
||||||
echo "Detected architecture: ${_arch}"
|
echo "Detected architecture: ${_arch}"
|
||||||
|
|
||||||
|
case "${_arch}" in
|
||||||
|
*windows*) _bin_name="zoxide.exe" ;;
|
||||||
|
*) _bin_name="zoxide" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
local _sudo=''
|
||||||
|
if test_writeable "${_bin_dir}"; then
|
||||||
|
echo "Installing zoxide, please wait…"
|
||||||
|
else
|
||||||
|
echo "Escalated permissions are required to install to ${_bin_dir}"
|
||||||
|
elevate_priv
|
||||||
|
_sudo='sudo'
|
||||||
|
echo "Installing zoxide as root, please wait…"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Create and enter a temporary directory.
|
# Create and enter a temporary directory.
|
||||||
local _tmp_dir
|
local _tmp_dir
|
||||||
_tmp_dir="$(mktemp -d)" || err "mktemp: could not create temporary directory"
|
_tmp_dir="$(mktemp -d)" || err "mktemp: could not create temporary directory"
|
||||||
|
|
@ -47,23 +95,23 @@ main() {
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Install binary.
|
# Install binary.
|
||||||
local _bin_dir="${HOME}/.local/bin"
|
# shellcheck disable=SC2086 # The lack of quoting is intentional. This may not be the best way to do it, but it's hard to properly do in POSIX
|
||||||
local _bin_name
|
{
|
||||||
case "${_arch}" in
|
ensure ${_sudo} mkdir -p "${_bin_dir}"
|
||||||
*windows*) _bin_name="zoxide.exe" ;;
|
ensure ${_sudo} cp "${_bin_name}" "${_bin_dir}"
|
||||||
*) _bin_name="zoxide" ;;
|
ensure ${_sudo} chmod +x "${_bin_dir}/${_bin_name}"
|
||||||
esac
|
}
|
||||||
ensure mkdir -p "${_bin_dir}"
|
|
||||||
ensure cp "${_bin_name}" "${_bin_dir}"
|
|
||||||
ensure chmod +x "${_bin_dir}/${_bin_name}"
|
|
||||||
echo "Installed zoxide to ${_bin_dir}"
|
echo "Installed zoxide to ${_bin_dir}"
|
||||||
|
|
||||||
# Install manpages.
|
# Install manpages.
|
||||||
local _man_dir="${HOME}/.local/share/man"
|
# shellcheck disable=SC2086 # The lack of quoting is intentional.
|
||||||
ensure mkdir -p "${_man_dir}/man1"
|
{
|
||||||
ensure cp "man/man1/"* "${_man_dir}/man1/"
|
ensure ${_sudo} mkdir -p "${_man_dir}/man1"
|
||||||
|
ensure ${_sudo} cp "man/man1/"* "${_man_dir}/man1/"
|
||||||
|
}
|
||||||
echo "Installed manpages to ${_man_dir}"
|
echo "Installed manpages to ${_man_dir}"
|
||||||
|
|
||||||
|
|
||||||
# Print success message and check $PATH.
|
# Print success message and check $PATH.
|
||||||
echo ""
|
echo ""
|
||||||
echo "zoxide is installed!"
|
echo "zoxide is installed!"
|
||||||
|
|
@ -384,6 +432,75 @@ err() {
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
elevate_priv() {
|
||||||
|
if ! check_cmd sudo; then
|
||||||
|
echo 'Could not find the command "sudo", needed to get permissions for install.' >&2
|
||||||
|
echo "If you are on Windows, please run your shell as an administrator, then" >&2
|
||||||
|
echo "rerun this script. Otherwise, please run this script as root, or install" >&2
|
||||||
|
echo "sudo." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if ! sudo -v; then
|
||||||
|
err "Superuser not granted, aborting installation"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Test if a location is writeable by trying to write to it. Windows does not let
|
||||||
|
# you test writeability other than by writing: https://stackoverflow.com/q/1999988
|
||||||
|
test_writeable() {
|
||||||
|
path="${1:-}/test.txt"
|
||||||
|
if touch "${path}" 2>/dev/null; then
|
||||||
|
rm "${path}"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# parse the arguments passed and set the environment variables accordingly
|
||||||
|
parse_args() {
|
||||||
|
# parse argv variables
|
||||||
|
while [ "$#" -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
-b | --bin-dir)
|
||||||
|
BIN_DIR="$2"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-m | --man-dir)
|
||||||
|
MAN_DIR="$2"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-a | --arch)
|
||||||
|
ARCH="$2"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-h | --help)
|
||||||
|
usage
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
|
||||||
|
-b=* | --bin-dir=*)
|
||||||
|
BIN_DIR="${1#*=}"
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
-m=* | --man-dir=*)
|
||||||
|
MAN_DIR="${1#*=}"
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
-a=* | --arch=*)
|
||||||
|
ARCH="${1#*=}"
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
err "Unknown option: $1"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# This is put in braces to ensure that the script does not run until it is
|
# This is put in braces to ensure that the script does not run until it is
|
||||||
# downloaded completely.
|
# downloaded completely.
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue