Skip to content

Commit 30ff591

Browse files
authored
style: address shellcheck issues in install.sh (starship#1305)
Fixes some shellcheck issues in the install script. Also normalizes formatting with `shfmt` program.
1 parent 525dfef commit 30ff591

File tree

1 file changed

+82
-42
lines changed

1 file changed

+82
-42
lines changed

install/install.sh

+82-42
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,27 @@ MAGENTA="$(tput setaf 5 2>/dev/null || echo '')"
3434
NO_COLOR="$(tput sgr0 2>/dev/null || echo '')"
3535

3636
info() {
37-
printf "${BOLD}${GREY}>${NO_COLOR} $@\n"
37+
printf "%s\n" "${BOLD}${GREY}>${NO_COLOR} $*"
3838
}
3939

4040
warn() {
41-
printf "${YELLOW}! $@${NO_COLOR}\n"
41+
printf "%s\n" "${YELLOW}! $*${NO_COLOR}"
4242
}
4343

4444
error() {
45-
printf "${RED}x $@${NO_COLOR}\n" >&2
45+
printf "%s\n" "${RED}x $*${NO_COLOR}" >&2
4646
}
4747

4848
complete() {
49-
printf "${GREEN}${NO_COLOR} $@\n"
49+
printf "%s\n" "${GREEN}${NO_COLOR} $*"
5050
}
5151

52-
# Gets path to a temporary file, even if
53-
get_tmpfile(){
52+
# Gets path to a temporary file, even if
53+
get_tmpfile() {
5454
local suffix
5555
suffix="$1"
5656
if hash mktemp; then
57-
printf "$(mktemp).${suffix}"
57+
printf "%s.%s" "$(mktemp)" "${suffix}"
5858
else
5959
# No really good options here--let's pick a default + hope
6060
printf "/tmp/starship.%s" "${suffix}"
@@ -63,10 +63,10 @@ get_tmpfile(){
6363

6464
# Test if a location is writeable by trying to write to it. Windows does not let
6565
# you test writeability other than by writing: https://stackoverflow.com/q/1999988
66-
test_writeable(){
66+
test_writeable() {
6767
local path
6868
path="${1:-}/test.txt"
69-
if touch "${path}" 2> /dev/null; then
69+
if touch "${path}" 2>/dev/null; then
7070
rm "${path}"
7171
return 0
7272
else
@@ -107,19 +107,19 @@ fetch() {
107107
fi
108108
}
109109

110-
fetch_and_unpack(){
110+
fetch_and_unpack() {
111111
local sudo
112112
local tmpfile
113113
sudo="$1"
114114
# I'd like to separate this into a fetch() and unpack() function, but I can't
115115
# figure out how to get bash functions to read STDIN/STDOUT from pipes
116116
if [ "${EXT}" = "tar.gz" ]; then
117-
fetch "${URL}" | ${sudo} tar xzf${VERBOSE} - -C "${BIN_DIR}"
117+
fetch "${URL}" | ${sudo} tar xzf "${VERBOSE}" - -C "${BIN_DIR}"
118118
elif [ "${EXT}" = "zip" ]; then
119119
# According to https://unix.stackexchange.com/q/2690, zip files cannot be read
120120
# through a pipe. We'll have to do our own file-based setup.
121121
tmpfile="$(get_tmpfile "${EXT}")"
122-
fetch "${URL}" > "${tmpfile}"
122+
fetch "${URL}" >"${tmpfile}"
123123
${sudo} unzip "${tmpfile}" -d "${BIN_DIR}"
124124
rm "${tmpfile}"
125125
else
@@ -130,9 +130,9 @@ fetch_and_unpack(){
130130
fi
131131
}
132132

133-
elevate_priv(){
133+
elevate_priv() {
134134
if ! hash sudo 2>/dev/null; then
135-
error "Could not find the command \"sudo\", needed to get permissions for install."
135+
error 'Could not find the command "sudo", needed to get permissions for install.'
136136
info "If you are on Windows, please run your shell as an administrator, then"
137137
info "rerun this script. Otherwise, please run this script as root, or install"
138138
info "sudo."
@@ -145,10 +145,10 @@ elevate_priv(){
145145
}
146146

147147
install() {
148-
local msg
149-
local sudo
148+
local msg
149+
local sudo
150150

151-
if test_writeable "${BIN_DIR}" ; then
151+
if test_writeable "${BIN_DIR}"; then
152152
sudo=""
153153
msg="Installing Starship, please wait…"
154154
else
@@ -210,17 +210,17 @@ detect_arch() {
210210

211211
confirm() {
212212
if [ -z "${FORCE-}" ]; then
213-
printf "${MAGENTA}?${NO_COLOR} $@ ${BOLD}[y/N]${NO_COLOR} "
213+
printf "%s " "${MAGENTA}?${NO_COLOR} $* ${BOLD}[y/N]${NO_COLOR}"
214214
set +e
215-
read -r yn < /dev/tty
215+
read -r yn </dev/tty
216216
rc=$?
217217
set -e
218218
if [ $rc -ne 0 ]; then
219-
error "Error reading from prompt (please re-run with the \`--yes\` option)"
219+
error 'Error reading from prompt (please re-run with the `--yes` option)'
220220
exit 1
221221
fi
222222
if [ "$yn" != "y" ] && [ "$yn" != "yes" ]; then
223-
error "Aborting (please answer \"yes\" to continue)"
223+
error 'Aborting (please answer "yes" to continue)'
224224
exit 1
225225
fi
226226
fi
@@ -230,14 +230,15 @@ check_bin_dir() {
230230
local bin_dir="$1"
231231

232232
if [ ! -d "$BIN_DIR" ]; then
233-
error "Installation location $BIN_DIR does not appear to be a directory"
234-
info "Make sure the location exists and is a directory, then try again."
235-
exit 1
233+
error "Installation location $BIN_DIR does not appear to be a directory"
234+
info "Make sure the location exists and is a directory, then try again."
235+
exit 1
236236
fi
237237

238238
# https://stackoverflow.com/a/11655875
239239
local good
240-
good=$( IFS=:
240+
good=$(
241+
IFS=:
241242
for path in $PATH; do
242243
if [ "${path}" = "${bin_dir}" ]; then
243244
echo 1
@@ -271,22 +272,61 @@ fi
271272
# parse argv variables
272273
while [ "$#" -gt 0 ]; do
273274
case "$1" in
274-
-p|--platform) PLATFORM="$2"; shift 2;;
275-
-b|--bin-dir) BIN_DIR="$2"; shift 2;;
276-
-a|--arch) ARCH="$2"; shift 2;;
277-
-B|--base-url) BASE_URL="$2"; shift 2;;
278-
279-
-V|--verbose) VERBOSE=1; shift 1;;
280-
-f|-y|--force|--yes) FORCE=1; shift 1;;
281-
282-
-p=*|--platform=*) PLATFORM="${1#*=}"; shift 1;;
283-
-b=*|--bin-dir=*) BIN_DIR="${1#*=}"; shift 1;;
284-
-a=*|--arch=*) ARCH="${1#*=}"; shift 1;;
285-
-B=*|--base-url=*) BASE_URL="${1#*=}"; shift 1;;
286-
-V=*|--verbose=*) VERBOSE="${1#*=}"; shift 1;;
287-
-f=*|-y=*|--force=*|--yes=*) FORCE="${1#*=}"; shift 1;;
288-
289-
*) error "Unknown option: $1"; exit 1;;
275+
-p | --platform)
276+
PLATFORM="$2"
277+
shift 2
278+
;;
279+
-b | --bin-dir)
280+
BIN_DIR="$2"
281+
shift 2
282+
;;
283+
-a | --arch)
284+
ARCH="$2"
285+
shift 2
286+
;;
287+
-B | --base-url)
288+
BASE_URL="$2"
289+
shift 2
290+
;;
291+
292+
-V | --verbose)
293+
VERBOSE=1
294+
shift 1
295+
;;
296+
-f | -y | --force | --yes)
297+
FORCE=1
298+
shift 1
299+
;;
300+
301+
-p=* | --platform=*)
302+
PLATFORM="${1#*=}"
303+
shift 1
304+
;;
305+
-b=* | --bin-dir=*)
306+
BIN_DIR="${1#*=}"
307+
shift 1
308+
;;
309+
-a=* | --arch=*)
310+
ARCH="${1#*=}"
311+
shift 1
312+
;;
313+
-B=* | --base-url=*)
314+
BASE_URL="${1#*=}"
315+
shift 1
316+
;;
317+
-V=* | --verbose=*)
318+
VERBOSE="${1#*=}"
319+
shift 1
320+
;;
321+
-f=* | -y=* | --force=* | --yes=*)
322+
FORCE="${1#*=}"
323+
shift 1
324+
;;
325+
326+
*)
327+
error "Unknown option: $1"
328+
exit 1
329+
;;
290330
esac
291331
done
292332

@@ -298,7 +338,7 @@ if [ "${ARCH}" = "i386" ]; then
298338
exit 1
299339
fi
300340

301-
printf " ${UNDERLINE}Configuration${NO_COLOR}\n"
341+
printf " %s\n" "${UNDERLINE}Configuration${NO_COLOR}"
302342
info "${BOLD}Bin directory${NO_COLOR}: ${GREEN}${BIN_DIR}${NO_COLOR}"
303343
info "${BOLD}Platform${NO_COLOR}: ${GREEN}${PLATFORM}${NO_COLOR}"
304344
info "${BOLD}Arch${NO_COLOR}: ${GREEN}${ARCH}${NO_COLOR}"

0 commit comments

Comments
 (0)