Skip to content

Commit d1b2723

Browse files
dmccafferydavidkna
andauthored
feat(install): make install script posix compliant (starship#2228)
- update shebang to support posix-compliant shells (like dash) - replace all usages of echo with printf (to normalise) - command variable to fetch_cmd as it overloads a posix - rename complete function to completed as it overloads the bash builtin for completion Co-authored-by: David Knaack <[email protected]>
1 parent 6ef15b6 commit d1b2723

File tree

1 file changed

+90
-80
lines changed

1 file changed

+90
-80
lines changed

install/install.sh

+90-80
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#!/bin/bash
1+
#!/usr/bin/env sh
2+
3+
# shellcheck disable=SC2039
24

35
# Options
46
#
@@ -20,18 +22,18 @@
2022
# -B, --base-url
2123
# Override the base URL used for downloading releases
2224

23-
set -euo pipefail
24-
printf "\n"
25+
set -eu
26+
printf '\n'
2527

26-
BOLD="$(tput bold 2>/dev/null || echo '')"
27-
GREY="$(tput setaf 0 2>/dev/null || echo '')"
28-
UNDERLINE="$(tput smul 2>/dev/null || echo '')"
29-
RED="$(tput setaf 1 2>/dev/null || echo '')"
30-
GREEN="$(tput setaf 2 2>/dev/null || echo '')"
31-
YELLOW="$(tput setaf 3 2>/dev/null || echo '')"
32-
BLUE="$(tput setaf 4 2>/dev/null || echo '')"
33-
MAGENTA="$(tput setaf 5 2>/dev/null || echo '')"
34-
NO_COLOR="$(tput sgr0 2>/dev/null || echo '')"
28+
BOLD="$(tput bold 2>/dev/null || printf '')"
29+
GREY="$(tput setaf 0 2>/dev/null || printf '')"
30+
UNDERLINE="$(tput smul 2>/dev/null || printf '')"
31+
RED="$(tput setaf 1 2>/dev/null || printf '')"
32+
GREEN="$(tput setaf 2 2>/dev/null || printf '')"
33+
YELLOW="$(tput setaf 3 2>/dev/null || printf '')"
34+
BLUE="$(tput setaf 4 2>/dev/null || printf '')"
35+
MAGENTA="$(tput setaf 5 2>/dev/null || printf '')"
36+
NO_COLOR="$(tput sgr0 2>/dev/null || printf '')"
3537

3638
SUPPORTED_TARGETS="x86_64-unknown-linux-gnu x86_64-unknown-linux-musl \
3739
i686-unknown-linux-musl aarch64-unknown-linux-musl \
@@ -41,26 +43,30 @@ SUPPORTED_TARGETS="x86_64-unknown-linux-gnu x86_64-unknown-linux-musl \
4143
x86_64-unknown-freebsd"
4244

4345
info() {
44-
printf "%s\n" "${BOLD}${GREY}>${NO_COLOR} $*"
46+
printf '%s\n' "${BOLD}${GREY}>${NO_COLOR} $*"
4547
}
4648

4749
warn() {
48-
printf "%s\n" "${YELLOW}! $*${NO_COLOR}"
50+
printf '%s\n' "${YELLOW}! $*${NO_COLOR}"
4951
}
5052

5153
error() {
52-
printf "%s\n" "${RED}x $*${NO_COLOR}" >&2
54+
printf '%s\n' "${RED}x $*${NO_COLOR}" >&2
55+
}
56+
57+
completed() {
58+
printf '%s\n' "${GREEN}${NO_COLOR} $*"
5359
}
5460

55-
complete() {
56-
printf "%s\n" "${GREEN}${NO_COLOR} $*"
61+
has() {
62+
command -v "$1" 1>/dev/null 2>&1
5763
}
5864

5965
# Gets path to a temporary file, even if
6066
get_tmpfile() {
6167
local suffix
6268
suffix="$1"
63-
if hash mktemp; then
69+
if has mktemp; then
6470
printf "%s.%s" "$(mktemp)" "${suffix}"
6571
else
6672
# No really good options here--let's pick a default + hope
@@ -81,64 +87,59 @@ test_writeable() {
8187
fi
8288
}
8389

84-
fetch() {
85-
local command
86-
if hash curl 2>/dev/null; then
87-
set +e
88-
command="curl --silent --fail --location $1"
89-
curl --silent --fail --location "$1"
90-
rc=$?
91-
set -e
90+
download() {
91+
file="$1"
92+
url="$2"
93+
94+
if has curl; then
95+
cmd="curl --fail --silent --location --output $file $url"
96+
elif has wget; then
97+
cmd="wget --quiet --output-document=$file $url"
98+
elif has fetch; then
99+
cmd="fetch --quiet --output=$file $url"
92100
else
93-
if hash wget 2>/dev/null; then
94-
set +e
95-
command="wget -O- -q $1"
96-
wget -O- -q "$1"
97-
rc=$?
98-
set -e
99-
else
100-
error "No HTTP download program (curl, wget) found…"
101-
exit 1
102-
fi
101+
error "No HTTP download program (curl, wget, fetch) found, exiting…"
102+
return 1
103103
fi
104104

105-
if [ $rc -ne 0 ]; then
106-
printf "\n" >&2
107-
error "Command failed (exit code $rc): ${BLUE}${command}${NO_COLOR}"
108-
printf "\n" >&2
109-
info "This is likely due to Starship not yet supporting your configuration." >&2
110-
info "If you would like to see a build for your configuration," >&2
111-
info "please create an issue requesting a build for ${MAGENTA}${ARCH}-${PLATFORM}${NO_COLOR}:" >&2
112-
info "${BOLD}${UNDERLINE}https://github.com/starship/starship/issues/new/${NO_COLOR}\n" >&2
113-
exit $rc
114-
fi
105+
$cmd && return 0 || rc=$?
106+
107+
error "Command failed (exit code $rc): ${BLUE}${cmd}${NO_COLOR}"
108+
printf "\n" >&2
109+
info "This is likely due to Starship not yet supporting your configuration."
110+
info "If you would like to see a build for your configuration,"
111+
info "please create an issue requesting a build for ${MAGENTA}${TARGET}${NO_COLOR}:"
112+
info "${BOLD}${UNDERLINE}https://github.com/starship/starship/issues/new/${NO_COLOR}"
113+
return $rc
115114
}
116115

117-
fetch_and_unpack() {
118-
local sudo
119-
local tmpfile
120-
sudo="$1"
121-
# I'd like to separate this into a fetch() and unpack() function, but I can't
122-
# figure out how to get bash functions to read STDIN/STDOUT from pipes
123-
if [ "${EXT}" = "tar.gz" ]; then
124-
fetch "${URL}" | ${sudo} tar xz"${VERBOSE}"f - -C "${BIN_DIR}"
125-
elif [ "${EXT}" = "zip" ]; then
126-
# According to https://unix.stackexchange.com/q/2690, zip files cannot be read
127-
# through a pipe. We'll have to do our own file-based setup.
128-
tmpfile="$(get_tmpfile "${EXT}")"
129-
fetch "${URL}" >"${tmpfile}"
130-
${sudo} unzip "${tmpfile}" -d "${BIN_DIR}"
131-
rm "${tmpfile}"
132-
else
133-
error "Unknown package extension."
134-
info "This almost certainly results from a bug in this script--please file a"
135-
info "bug report at https://github.com/starship/starship/issues"
136-
exit 1
137-
fi
116+
unpack() {
117+
local archive=$1
118+
local bin_dir=$2
119+
local sudo=${3-}
120+
121+
case "$archive" in
122+
*.tar.gz)
123+
flags=$(test -n "${VERBOSE-}" && echo "-v" || echo "")
124+
${sudo} tar "${flags}" -xzf "${archive}" -C "${bin_dir}"
125+
return 0
126+
;;
127+
*.zip)
128+
flags=$(test -z "${VERBOSE-}" && echo "-qq" || echo "")
129+
UNZIP="${flags}" ${sudo} unzip "${archive}" -d "${bin_dir}"
130+
return 0
131+
;;
132+
esac
133+
134+
error "Unknown package extension."
135+
printf "\n"
136+
info "This almost certainly results from a bug in this script--please file a"
137+
info "bug report at https://github.com/starship/starship/issues"
138+
return 1
138139
}
139140

140141
elevate_priv() {
141-
if ! hash sudo 2>/dev/null; then
142+
if ! has sudo; then
142143
error 'Could not find the command "sudo", needed to get permissions for install.'
143144
info "If you are on Windows, please run your shell as an administrator, then"
144145
info "rerun this script. Otherwise, please run this script as root, or install"
@@ -154,6 +155,8 @@ elevate_priv() {
154155
install() {
155156
local msg
156157
local sudo
158+
local archive
159+
local ext="$1"
157160

158161
if test_writeable "${BIN_DIR}"; then
159162
sudo=""
@@ -165,7 +168,14 @@ install() {
165168
msg="Installing Starship as root, please wait…"
166169
fi
167170
info "$msg"
168-
fetch_and_unpack "${sudo}"
171+
172+
archive=$(get_tmpfile "$ext")
173+
174+
# download to the temp file
175+
download "${archive}" "${URL}"
176+
177+
# unpack the temp file to the bin dir, using sudo if required
178+
unpack "${archive}" "${BIN_DIR}" "${sudo}"
169179
}
170180

171181
# Currently supporting:
@@ -189,7 +199,7 @@ detect_platform() {
189199
freebsd) platform="unknown-freebsd" ;;
190200
esac
191201

192-
echo "${platform}"
202+
printf '%s' "${platform}"
193203
}
194204

195205
# Currently supporting:
@@ -212,7 +222,7 @@ detect_arch() {
212222
arch=arm
213223
fi
214224

215-
echo "${arch}"
225+
printf '%s' "${arch}"
216226
}
217227

218228
detect_target() {
@@ -224,7 +234,7 @@ detect_target() {
224234
target="${target}eabihf"
225235
fi
226236

227-
echo "${target}"
237+
printf '%s' "${target}"
228238
}
229239

230240

@@ -261,7 +271,7 @@ check_bin_dir() {
261271
IFS=:
262272
for path in $PATH; do
263273
if [ "${path}" = "${bin_dir}" ]; then
264-
echo 1
274+
printf 1
265275
break
266276
fi
267277
done
@@ -278,12 +288,12 @@ is_build_available() {
278288
local target="$3"
279289

280290
local good
281-
291+
282292
good=$(
283293
IFS=" "
284294
for t in $SUPPORTED_TARGETS; do
285-
if [ "${t}" == "${target}" ]; then
286-
echo 1
295+
if [ "${t}" = "${target}" ]; then
296+
printf 1
287297
break
288298
fi
289299
done
@@ -395,7 +405,7 @@ else
395405
VERBOSE=
396406
fi
397407

398-
echo
408+
printf '\n'
399409

400410
EXT=tar.gz
401411
if [ "${PLATFORM}" = "pc-windows-msvc" ]; then
@@ -407,10 +417,10 @@ info "Tarball URL: ${UNDERLINE}${BLUE}${URL}${NO_COLOR}"
407417
confirm "Install Starship ${GREEN}latest${NO_COLOR} to ${BOLD}${GREEN}${BIN_DIR}${NO_COLOR}?"
408418
check_bin_dir "${BIN_DIR}"
409419

410-
install
411-
complete "Starship installed"
420+
install "${EXT}"
421+
completed "Starship installed"
412422

413-
echo
423+
printf '\n'
414424
info "Please follow the steps for your shell to complete the installation:
415425
416426
${BOLD}${UNDERLINE}Bash${NO_COLOR}

0 commit comments

Comments
 (0)