Skip to content

Commit efa576d

Browse files
vic1707rami3l
authored andcommitted
implements quiet flag in rustup-init.sh
1 parent ad668d9 commit efa576d

File tree

1 file changed

+69
-43
lines changed

1 file changed

+69
-43
lines changed

rustup-init.sh

+69-43
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ set -u
2727

2828
# If RUSTUP_UPDATE_ROOT is unset or empty, default it.
2929
RUSTUP_UPDATE_ROOT="${RUSTUP_UPDATE_ROOT:-https://static.rust-lang.org/rustup}"
30+
# Set quiet as a global for ease of use
31+
RUSTUP_QUIET=no
3032

3133
# NOTICE: If you change anything here, please make the same changes in setup_mode.rs
3234
usage() {
@@ -114,19 +116,25 @@ main() {
114116
usage
115117
exit 0
116118
;;
119+
--quiet)
120+
RUSTUP_QUIET=yes
121+
;;
117122
*)
118123
OPTIND=1
119124
if [ "${arg%%--*}" = "" ]; then
120125
# Long option (other than --help);
121126
# don't attempt to interpret it.
122127
continue
123128
fi
124-
while getopts :hy sub_arg "$arg"; do
129+
while getopts :hqy sub_arg "$arg"; do
125130
case "$sub_arg" in
126131
h)
127132
usage
128133
exit 0
129134
;;
135+
q)
136+
RUSTUP_QUIET=yes
137+
;;
130138
y)
131139
# user wants to skip the prompt --
132140
# we don't need /dev/tty
@@ -140,18 +148,14 @@ main() {
140148
esac
141149
done
142150

143-
if $_ansi_escapes_are_valid; then
144-
printf "\33[1minfo:\33[0m downloading installer\n" 1>&2
145-
else
146-
printf '%s\n' 'info: downloading installer' 1>&2
147-
fi
151+
say 'downloading installer'
148152

149153
ensure mkdir -p "$_dir"
150154
ensure downloader "$_url" "$_file" "$_arch"
151155
ensure chmod u+x "$_file"
152156
if [ ! -x "$_file" ]; then
153-
printf '%s\n' "Cannot execute $_file (likely because of mounting /tmp as noexec)." 1>&2
154-
printf '%s\n' "Please copy the file to a location where you can execute binaries and run ./rustup-init${_ext}." 1>&2
157+
err "Cannot execute $_file (likely because of mounting /tmp as noexec)."
158+
err "Please copy the file to a location where you can execute binaries and run ./rustup-init${_ext}."
155159
exit 1
156160
fi
157161

@@ -162,6 +166,7 @@ main() {
162166
# to explicitly connect /dev/tty to the installer's stdin.
163167
if [ ! -t 1 ]; then
164168
err "Unable to run interactively. Run with -y to accept defaults, --help for additional options"
169+
exit 1;
165170
fi
166171

167172
ignore "$_file" "$@" < /dev/tty
@@ -184,14 +189,14 @@ get_current_exe() {
184189
if test -L /proc/self/exe ; then
185190
_current_exe=/proc/self/exe
186191
else
187-
printf '%s\n' "Warning: Unable to find /proc/self/exe. System architecture detection might be inaccurate." 1>&2
192+
warn "Unable to find /proc/self/exe. System architecture detection might be inaccurate."
188193
if test -n "$SHELL" ; then
189194
_current_exe=$SHELL
190195
else
191196
need_cmd /bin/sh
192197
_current_exe=/bin/sh
193198
fi
194-
printf '%s\n' "Warning: Falling back to $_current_exe." 1>&2
199+
warn "Falling back to $_current_exe."
195200
fi
196201
echo "$_current_exe"
197202
}
@@ -213,6 +218,7 @@ get_bitness() {
213218
echo 64
214219
else
215220
err "unknown platform bitness"
221+
exit 1;
216222
fi
217223
}
218224

@@ -247,6 +253,7 @@ get_endianness() {
247253
echo "${cputype}${suffix_eb}"
248254
else
249255
err "unknown platform endianness"
256+
exit 1
250257
fi
251258
}
252259

@@ -294,19 +301,14 @@ ensure_loongarch_uapi() {
294301
return 0
295302
;;
296303
234)
297-
echo >&2
298-
echo 'Your Linux kernel does not provide the ABI required by this Rust' >&2
299-
echo 'distribution. Please check with your OS provider for how to obtain a' >&2
300-
echo 'compatible Rust package for your system.' >&2
301-
echo >&2
304+
err 'Your Linux kernel does not provide the ABI required by this Rust distribution.'
305+
err 'Please check with your OS provider for how to obtain a compatible Rust package for your system.'
302306
exit 1
303307
;;
304308
*)
305-
echo "warn: Cannot determine current system's ABI flavor, continuing anyway." >&2
306-
echo >&2
307-
echo 'Note that the official Rust distribution only works with the upstream' >&2
308-
echo 'kernel ABI. Installation will fail if your running kernel happens to be' >&2
309-
echo 'incompatible.' >&2
309+
warn "Cannot determine current system's ABI flavor, continuing anyway."
310+
warn 'Note that the official Rust distribution only works with the upstream kernel ABI.'
311+
warn 'Installation will fail if your running kernel happens to be incompatible.'
310312
;;
311313
esac
312314
}
@@ -410,6 +412,7 @@ get_architecture() {
410412

411413
*)
412414
err "unrecognized OS type: $_ostype"
415+
exit 1
413416
;;
414417

415418
esac
@@ -489,6 +492,7 @@ get_architecture() {
489492
;;
490493
*)
491494
err "unknown CPU type: $_cputype"
495+
exit 1
492496

493497
esac
494498

@@ -501,14 +505,10 @@ get_architecture() {
501505
else {
502506
# 32-bit executable for amd64 = x32
503507
if is_host_amd64_elf "$_current_exe"; then {
504-
echo "This host is running an x32 userland; as it stands, x32 support is poor," 1>&2
505-
echo "and there isn't a native toolchain -- you will have to install" 1>&2
506-
echo "multiarch compatibility with i686 and/or amd64, then select one" 1>&2
507-
echo "by re-running this script with the RUSTUP_CPUTYPE environment variable" 1>&2
508-
echo "set to i686 or x86_64, respectively." 1>&2
509-
echo 1>&2
510-
echo "You will be able to add an x32 target after installation by running" 1>&2
511-
echo " rustup target add x86_64-unknown-linux-gnux32" 1>&2
508+
err "This host is running an x32 userland, for which no native toolchain is provided."
509+
err "You will have to install multiarch compatibility with i686 or amd64."
510+
err "To do so, set the RUSTUP_CPUTYPE environment variable set to i686 or amd64 and re-run this script."
511+
err "You will be able to add an x32 target after installation by running \`rustup target add x86_64-unknown-linux-gnux32\`."
512512
exit 1
513513
}; else
514514
_cputype=i686
@@ -531,6 +531,7 @@ get_architecture() {
531531
;;
532532
riscv64gc)
533533
err "riscv64 with 32-bit userland unsupported"
534+
exit 1
534535
;;
535536
esac
536537
fi
@@ -551,18 +552,34 @@ get_architecture() {
551552
RETVAL="$_arch"
552553
}
553554

555+
__print() {
556+
if $_ansi_escapes_are_valid; then
557+
printf '\33[1m%s:\33[0m %s\n' "$1" "$2" >&2
558+
else
559+
printf '%s: %s\n' "$1" "$2" >&2
560+
fi
561+
}
562+
563+
warn() {
564+
__print 'warn' "$1" >&2
565+
}
566+
554567
say() {
555-
printf 'rustup: %s\n' "$1"
568+
if [ "$RUSTUP_QUIET" = "no" ]; then
569+
__print 'info' "$1" >&2
570+
fi
556571
}
557572

573+
# NOTE: you are required to exit yourself
574+
# we don't do it here because of multiline errors
558575
err() {
559-
say "$1" >&2
560-
exit 1
576+
__print 'error' "$1" >&2
561577
}
562578

563579
need_cmd() {
564580
if ! check_cmd "$1"; then
565581
err "need '$1' (command not found)"
582+
exit 1
566583
fi
567584
}
568585

@@ -571,14 +588,20 @@ check_cmd() {
571588
}
572589

573590
assert_nz() {
574-
if [ -z "$1" ]; then err "assert_nz $2"; fi
591+
if [ -z "$1" ]; then
592+
err "assert_nz $2"
593+
exit 1
594+
fi
575595
}
576596

577597
# Run a command that should never fail. If the command fails execution
578598
# will immediately terminate with an error showing the failing
579599
# command.
580600
ensure() {
581-
if ! "$@"; then err "command failed: $*"; fi
601+
if ! "$@"; then
602+
err "command failed: $*"
603+
exit 1
604+
fi
582605
}
583606

584607
# This is just for indicating that commands' results are being
@@ -618,9 +641,9 @@ downloader() {
618641
_err=$(curl $_retry --proto '=https' --tlsv1.2 --ciphers "$_ciphersuites" --silent --show-error --fail --location "$1" --output "$2" 2>&1)
619642
_status=$?
620643
else
621-
echo "warn: Not enforcing strong cipher suites for TLS, this is potentially less secure"
644+
warn "Not enforcing strong cipher suites for TLS, this is potentially less secure"
622645
if ! check_help_for "$3" curl --proto --tlsv1.2; then
623-
echo "warn: Not enforcing TLS v1.2, this is potentially less secure"
646+
warn "Not enforcing TLS v1.2, this is potentially less secure"
624647
_err=$(curl $_retry --silent --show-error --fail --location "$1" --output "$2" 2>&1)
625648
_status=$?
626649
else
@@ -629,15 +652,16 @@ downloader() {
629652
fi
630653
fi
631654
if [ -n "$_err" ]; then
632-
echo "$_err" >&2
655+
warn "$_err"
633656
if echo "$_err" | grep -q 404$; then
634657
err "installer for platform '$3' not found, this may be unsupported"
658+
exit 1
635659
fi
636660
fi
637661
return $_status
638662
elif [ "$_dld" = wget ]; then
639663
if [ "$(wget -V 2>&1|head -2|tail -1|cut -f1 -d" ")" = "BusyBox" ]; then
640-
echo "warn: using the BusyBox version of wget. Not enforcing strong cipher suites for TLS or TLS v1.2, this is potentially less secure"
664+
warn "using the BusyBox version of wget. Not enforcing strong cipher suites for TLS or TLS v1.2, this is potentially less secure"
641665
_err=$(wget "$1" -O "$2" 2>&1)
642666
_status=$?
643667
else
@@ -647,9 +671,9 @@ downloader() {
647671
_err=$(wget --https-only --secure-protocol=TLSv1_2 --ciphers "$_ciphersuites" "$1" -O "$2" 2>&1)
648672
_status=$?
649673
else
650-
echo "warn: Not enforcing strong cipher suites for TLS, this is potentially less secure"
674+
warn "Not enforcing strong cipher suites for TLS, this is potentially less secure"
651675
if ! check_help_for "$3" wget --https-only --secure-protocol; then
652-
echo "warn: Not enforcing TLS v1.2, this is potentially less secure"
676+
warn "Not enforcing TLS v1.2, this is potentially less secure"
653677
_err=$(wget "$1" -O "$2" 2>&1)
654678
_status=$?
655679
else
@@ -659,14 +683,16 @@ downloader() {
659683
fi
660684
fi
661685
if [ -n "$_err" ]; then
662-
echo "$_err" >&2
686+
warn "$_err"
663687
if echo "$_err" | grep -q ' 404 Not Found$'; then
664688
err "installer for platform '$3' not found, this may be unsupported"
689+
exit 1
665690
fi
666691
fi
667692
return $_status
668693
else
669694
err "Unknown downloader" # should not reach here
695+
exit 1
670696
fi
671697
}
672698

@@ -696,7 +722,7 @@ check_help_for() {
696722
# fail to find these options to force fallback
697723
if [ "$(sw_vers -productVersion | cut -d. -f2)" -lt 13 ]; then
698724
# Older than 10.13
699-
echo "warn: Detected macOS platform older than 10.13"
725+
warn "Detected macOS platform older than 10.13"
700726
return 1
701727
fi
702728
;;
@@ -705,8 +731,8 @@ check_help_for() {
705731
;;
706732
*)
707733
# Unknown product version, warn and continue
708-
echo "warn: Detected unknown macOS major version: $(sw_vers -productVersion)"
709-
echo "Warning TLS capabilities detection may fail"
734+
warn "Detected unknown macOS major version: $(sw_vers -productVersion)"
735+
warn "TLS capabilities detection may fail"
710736
;;
711737
esac
712738
fi

0 commit comments

Comments
 (0)