1
- #! /bin/bash
1
+ #! /usr/bin/env sh
2
+
3
+ # shellcheck disable=SC2039
2
4
3
5
# Options
4
6
#
20
22
# -B, --base-url
21
23
# Override the base URL used for downloading releases
22
24
23
- set -euo pipefail
24
- printf " \n "
25
+ set -eu
26
+ printf ' \n '
25
27
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 ' ' ) "
35
37
36
38
SUPPORTED_TARGETS=" x86_64-unknown-linux-gnu x86_64-unknown-linux-musl \
37
39
i686-unknown-linux-musl aarch64-unknown-linux-musl \
@@ -41,26 +43,30 @@ SUPPORTED_TARGETS="x86_64-unknown-linux-gnu x86_64-unknown-linux-musl \
41
43
x86_64-unknown-freebsd"
42
44
43
45
info () {
44
- printf " %s\n" " ${BOLD}${GREY} >${NO_COLOR} $* "
46
+ printf ' %s\n' " ${BOLD}${GREY} >${NO_COLOR} $* "
45
47
}
46
48
47
49
warn () {
48
- printf " %s\n" " ${YELLOW} ! $* ${NO_COLOR} "
50
+ printf ' %s\n' " ${YELLOW} ! $* ${NO_COLOR} "
49
51
}
50
52
51
53
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} $* "
53
59
}
54
60
55
- complete () {
56
- printf " %s\n " " ${GREEN} ✓ ${NO_COLOR} $* "
61
+ has () {
62
+ command -v " $1 " 1> /dev/null 2>&1
57
63
}
58
64
59
65
# Gets path to a temporary file, even if
60
66
get_tmpfile () {
61
67
local suffix
62
68
suffix=" $1 "
63
- if hash mktemp; then
69
+ if has mktemp; then
64
70
printf " %s.%s" " $( mktemp) " " ${suffix} "
65
71
else
66
72
# No really good options here--let's pick a default + hope
@@ -81,64 +87,59 @@ test_writeable() {
81
87
fi
82
88
}
83
89
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 "
92
100
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
103
103
fi
104
104
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
115
114
}
116
115
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
138
139
}
139
140
140
141
elevate_priv () {
141
- if ! hash sudo 2> /dev/null ; then
142
+ if ! has sudo; then
142
143
error ' Could not find the command "sudo", needed to get permissions for install.'
143
144
info " If you are on Windows, please run your shell as an administrator, then"
144
145
info " rerun this script. Otherwise, please run this script as root, or install"
@@ -154,6 +155,8 @@ elevate_priv() {
154
155
install () {
155
156
local msg
156
157
local sudo
158
+ local archive
159
+ local ext=" $1 "
157
160
158
161
if test_writeable " ${BIN_DIR} " ; then
159
162
sudo=" "
@@ -165,7 +168,14 @@ install() {
165
168
msg=" Installing Starship as root, please wait…"
166
169
fi
167
170
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} "
169
179
}
170
180
171
181
# Currently supporting:
@@ -189,7 +199,7 @@ detect_platform() {
189
199
freebsd) platform=" unknown-freebsd" ;;
190
200
esac
191
201
192
- echo " ${platform} "
202
+ printf ' %s ' " ${platform} "
193
203
}
194
204
195
205
# Currently supporting:
@@ -212,7 +222,7 @@ detect_arch() {
212
222
arch=arm
213
223
fi
214
224
215
- echo " ${arch} "
225
+ printf ' %s ' " ${arch} "
216
226
}
217
227
218
228
detect_target () {
@@ -224,7 +234,7 @@ detect_target() {
224
234
target=" ${target} eabihf"
225
235
fi
226
236
227
- echo " ${target} "
237
+ printf ' %s ' " ${target} "
228
238
}
229
239
230
240
@@ -261,7 +271,7 @@ check_bin_dir() {
261
271
IFS=:
262
272
for path in $PATH ; do
263
273
if [ " ${path} " = " ${bin_dir} " ]; then
264
- echo 1
274
+ printf 1
265
275
break
266
276
fi
267
277
done
@@ -278,12 +288,12 @@ is_build_available() {
278
288
local target=" $3 "
279
289
280
290
local good
281
-
291
+
282
292
good=$(
283
293
IFS=" "
284
294
for t in $SUPPORTED_TARGETS ; do
285
- if [ " ${t} " == " ${target} " ]; then
286
- echo 1
295
+ if [ " ${t} " = " ${target} " ]; then
296
+ printf 1
287
297
break
288
298
fi
289
299
done
395
405
VERBOSE=
396
406
fi
397
407
398
- echo
408
+ printf ' \n '
399
409
400
410
EXT=tar.gz
401
411
if [ " ${PLATFORM} " = " pc-windows-msvc" ]; then
@@ -407,10 +417,10 @@ info "Tarball URL: ${UNDERLINE}${BLUE}${URL}${NO_COLOR}"
407
417
confirm " Install Starship ${GREEN} latest${NO_COLOR} to ${BOLD}${GREEN}${BIN_DIR}${NO_COLOR} ?"
408
418
check_bin_dir " ${BIN_DIR} "
409
419
410
- install
411
- complete " Starship installed"
420
+ install " ${EXT} "
421
+ completed " Starship installed"
412
422
413
- echo
423
+ printf ' \n '
414
424
info " Please follow the steps for your shell to complete the installation:
415
425
416
426
${BOLD}${UNDERLINE} Bash${NO_COLOR}
0 commit comments