From 30ff5913bea6cb209ba55f3064c109853d45d521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Vladovi=C4=87?= Date: Wed, 10 Jun 2020 20:40:05 +0200 Subject: [PATCH] style: address shellcheck issues in install.sh (#1305) Fixes some shellcheck issues in the install script. Also normalizes formatting with `shfmt` program. --- install/install.sh | 118 ++++++++++++++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 39 deletions(-) diff --git a/install/install.sh b/install/install.sh index baab5505..466fa204 100755 --- a/install/install.sh +++ b/install/install.sh @@ -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(){ +# Gets path to a temporary file, even if +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