style: address shellcheck issues in install.sh (#1305)

Fixes some shellcheck issues in the install script. Also normalizes
formatting with `shfmt` program.
This commit is contained in:
Dario Vladović 2020-06-10 20:40:05 +02:00 committed by GitHub
parent 525dfef9a7
commit 30ff5913be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,27 +34,27 @@ MAGENTA="$(tput setaf 5 2>/dev/null || echo '')"
NO_COLOR="$(tput sgr0 2>/dev/null || echo '')"
info() {
printf "${BOLD}${GREY}>${NO_COLOR} $@\n"
printf "%s\n" "${BOLD}${GREY}>${NO_COLOR} $*"
}
warn() {
printf "${YELLOW}! $@${NO_COLOR}\n"
printf "%s\n" "${YELLOW}! $*${NO_COLOR}"
}
error() {
printf "${RED}x $@${NO_COLOR}\n" >&2
printf "%s\n" "${RED}x $*${NO_COLOR}" >&2
}
complete() {
printf "${GREEN}${NO_COLOR} $@\n"
printf "%s\n" "${GREEN}${NO_COLOR} $*"
}
# Gets path to a temporary file, even if
get_tmpfile(){
get_tmpfile() {
local suffix
suffix="$1"
if hash mktemp; then
printf "$(mktemp).${suffix}"
printf "%s.%s" "$(mktemp)" "${suffix}"
else
# No really good options here--let's pick a default + hope
printf "/tmp/starship.%s" "${suffix}"
@ -63,10 +63,10 @@ get_tmpfile(){
# 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(){
test_writeable() {
local path
path="${1:-}/test.txt"
if touch "${path}" 2> /dev/null; then
if touch "${path}" 2>/dev/null; then
rm "${path}"
return 0
else
@ -107,19 +107,19 @@ fetch() {
fi
}
fetch_and_unpack(){
fetch_and_unpack() {
local sudo
local tmpfile
sudo="$1"
# I'd like to separate this into a fetch() and unpack() function, but I can't
# figure out how to get bash functions to read STDIN/STDOUT from pipes
if [ "${EXT}" = "tar.gz" ]; then
fetch "${URL}" | ${sudo} tar xzf${VERBOSE} - -C "${BIN_DIR}"
fetch "${URL}" | ${sudo} tar xzf "${VERBOSE}" - -C "${BIN_DIR}"
elif [ "${EXT}" = "zip" ]; then
# According to https://unix.stackexchange.com/q/2690, zip files cannot be read
# through a pipe. We'll have to do our own file-based setup.
tmpfile="$(get_tmpfile "${EXT}")"
fetch "${URL}" > "${tmpfile}"
fetch "${URL}" >"${tmpfile}"
${sudo} unzip "${tmpfile}" -d "${BIN_DIR}"
rm "${tmpfile}"
else
@ -130,9 +130,9 @@ fetch_and_unpack(){
fi
}
elevate_priv(){
elevate_priv() {
if ! hash sudo 2>/dev/null; then
error "Could not find the command \"sudo\", needed to get permissions for install."
error 'Could not find the command "sudo", needed to get permissions for install.'
info "If you are on Windows, please run your shell as an administrator, then"
info "rerun this script. Otherwise, please run this script as root, or install"
info "sudo."
@ -145,10 +145,10 @@ elevate_priv(){
}
install() {
local msg
local sudo
local msg
local sudo
if test_writeable "${BIN_DIR}" ; then
if test_writeable "${BIN_DIR}"; then
sudo=""
msg="Installing Starship, please wait…"
else
@ -210,17 +210,17 @@ detect_arch() {
confirm() {
if [ -z "${FORCE-}" ]; then
printf "${MAGENTA}?${NO_COLOR} $@ ${BOLD}[y/N]${NO_COLOR} "
printf "%s " "${MAGENTA}?${NO_COLOR} $* ${BOLD}[y/N]${NO_COLOR}"
set +e
read -r yn < /dev/tty
read -r yn </dev/tty
rc=$?
set -e
if [ $rc -ne 0 ]; then
error "Error reading from prompt (please re-run with the \`--yes\` option)"
error 'Error reading from prompt (please re-run with the `--yes` option)'
exit 1
fi
if [ "$yn" != "y" ] && [ "$yn" != "yes" ]; then
error "Aborting (please answer \"yes\" to continue)"
error 'Aborting (please answer "yes" to continue)'
exit 1
fi
fi
@ -230,14 +230,15 @@ check_bin_dir() {
local bin_dir="$1"
if [ ! -d "$BIN_DIR" ]; then
error "Installation location $BIN_DIR does not appear to be a directory"
info "Make sure the location exists and is a directory, then try again."
exit 1
error "Installation location $BIN_DIR does not appear to be a directory"
info "Make sure the location exists and is a directory, then try again."
exit 1
fi
# https://stackoverflow.com/a/11655875
local good
good=$( IFS=:
good=$(
IFS=:
for path in $PATH; do
if [ "${path}" = "${bin_dir}" ]; then
echo 1
@ -271,22 +272,61 @@ fi
# parse argv variables
while [ "$#" -gt 0 ]; do
case "$1" in
-p|--platform) PLATFORM="$2"; shift 2;;
-b|--bin-dir) BIN_DIR="$2"; shift 2;;
-a|--arch) ARCH="$2"; shift 2;;
-B|--base-url) BASE_URL="$2"; shift 2;;
-p | --platform)
PLATFORM="$2"
shift 2
;;
-b | --bin-dir)
BIN_DIR="$2"
shift 2
;;
-a | --arch)
ARCH="$2"
shift 2
;;
-B | --base-url)
BASE_URL="$2"
shift 2
;;
-V|--verbose) VERBOSE=1; shift 1;;
-f|-y|--force|--yes) FORCE=1; shift 1;;
-V | --verbose)
VERBOSE=1
shift 1
;;
-f | -y | --force | --yes)
FORCE=1
shift 1
;;
-p=*|--platform=*) PLATFORM="${1#*=}"; shift 1;;
-b=*|--bin-dir=*) BIN_DIR="${1#*=}"; shift 1;;
-a=*|--arch=*) ARCH="${1#*=}"; shift 1;;
-B=*|--base-url=*) BASE_URL="${1#*=}"; shift 1;;
-V=*|--verbose=*) VERBOSE="${1#*=}"; shift 1;;
-f=*|-y=*|--force=*|--yes=*) FORCE="${1#*=}"; shift 1;;
-p=* | --platform=*)
PLATFORM="${1#*=}"
shift 1
;;
-b=* | --bin-dir=*)
BIN_DIR="${1#*=}"
shift 1
;;
-a=* | --arch=*)
ARCH="${1#*=}"
shift 1
;;
-B=* | --base-url=*)
BASE_URL="${1#*=}"
shift 1
;;
-V=* | --verbose=*)
VERBOSE="${1#*=}"
shift 1
;;
-f=* | -y=* | --force=* | --yes=*)
FORCE="${1#*=}"
shift 1
;;
*) error "Unknown option: $1"; exit 1;;
*)
error "Unknown option: $1"
exit 1
;;
esac
done
@ -298,7 +338,7 @@ if [ "${ARCH}" = "i386" ]; then
exit 1
fi
printf " ${UNDERLINE}Configuration${NO_COLOR}\n"
printf " %s\n" "${UNDERLINE}Configuration${NO_COLOR}"
info "${BOLD}Bin directory${NO_COLOR}: ${GREEN}${BIN_DIR}${NO_COLOR}"
info "${BOLD}Platform${NO_COLOR}: ${GREEN}${PLATFORM}${NO_COLOR}"
info "${BOLD}Arch${NO_COLOR}: ${GREEN}${ARCH}${NO_COLOR}"