From abca35db8befa27d1e09d70abd8a8ece9b66e2ff Mon Sep 17 00:00:00 2001 From: Postmodern Date: Sun, 2 Feb 2025 00:30:47 -0800 Subject: [PATCH] Silence `wget`/`curl` output if stdout is not a TTY (closes #397). --- share/ruby-install/util.sh | 21 ++++++++++++++++----- test/util-tests/download_test.sh | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/share/ruby-install/util.sh b/share/ruby-install/util.sh index 3a580d52..98c8dd5a 100644 --- a/share/ruby-install/util.sh +++ b/share/ruby-install/util.sh @@ -40,17 +40,28 @@ function download() local url="$1" local dest="$2" + if [[ -z "$downloader" ]]; then + error "Could not find wget or curl" + return 1 + fi + + local quiet + + if [[ ! -t 1 ]]; then + quiet=1 + fi + [[ -d "$dest" ]] && dest="$dest/${url##*/}" [[ -f "$dest" ]] && return mkdir -p "${dest%/*}" || return $? case "$downloader" in - wget) run wget -c -O "$dest.part" "$url" || return $? ;; - curl) run curl -f -L -C - -o "$dest.part" "$url" || return $? ;; - "") - error "Could not find wget or curl" - return 1 + wget) + run wget ${quiet:+-q} -c -O "$dest.part" "$url" || return $? + ;; + curl) + run curl ${quiet:+-s -S} -f -L -C - -o "$dest.part" "$url" || return $? ;; esac diff --git a/test/util-tests/download_test.sh b/test/util-tests/download_test.sh index 05f996b5..cf1e587b 100755 --- a/test/util-tests/download_test.sh +++ b/test/util-tests/download_test.sh @@ -37,6 +37,15 @@ function test_download_using_wget() assertTrue "did not download the file" '[[ -f "$test_dest" ]]' } +function test_download_using_wget_when_stdout_is_not_a_tty() +{ + command -v wget >/dev/null || return 0 + + local output="$(downloader="wget" download "$test_url" "$test_dest")" + + assertEquals "did not silence wget output" "" "$output" +} + function test_download_using_curl() { command -v curl >/dev/null || return 0 @@ -46,6 +55,15 @@ function test_download_using_curl() assertTrue "did not download the file" '[[ -f "$test_dest" ]]' } +function test_download_using_curl_when_stdout_is_not_a_tty() +{ + command -v curl >/dev/null || return 0 + + local output="$(downloader="curl" download "$test_url" "$test_dest")" + + assertEquals "did not silence curl output" "" "$output" +} + function tearDown() { rm -rf "$test_dir"