From 3a126c820b4a6600847697a271ed1e30c1ffbba3 Mon Sep 17 00:00:00 2001 From: Benjamin Lupton Date: Fri, 1 Sep 2023 18:57:15 +0800 Subject: [PATCH] setup-util-bash: install via bottle if available add support for bearer token to down and setup-util --- commands/down | 103 +++++++++++++++++++++++++++++---------- commands/setup-util | 8 +-- commands/setup-util-bash | 96 ++++++++++++++++-------------------- 3 files changed, 124 insertions(+), 83 deletions(-) diff --git a/commands/down b/commands/down index ba01a2620..a812b74e6 100755 --- a/commands/down +++ b/commands/down @@ -57,6 +57,9 @@ function down() ( Treat the download as a zip fie, and only extract files that match the glob pattern . This option is handled by the [unziptar] command. + --bearer-token= + If provided, include this in a bearer token header. + --directory= Place downloaded file(s) inside . If ommitted, the current working directory will be used. @@ -89,13 +92,14 @@ function down() ( } # process - local item url='' tool='' unzip_format='' unzip_filter='' directory='' file='' filepath='' retry='2' quiet='yes' + local item url='' tool='' unzip_format='' unzip_filter='' directory='' file='' filepath='' retry='2' quiet='yes' option_bearer_token='' quiet="$(echo-quiet "$quiet" -- "$@")" while test "$#" -ne 0; do item="$1" shift case "$item" in '--help' | '-h') help ;; + '--bearer-token='*) option_bearer_token="${item#*--bearer-token=}" ;; '--no-quiet'* | '--quiet'* | '--no-verbose'* | '--verbose'*) ;; # handled by echo-quiet '--tool='*) tool="${item#*--tool=}" ;; '--unzip-format='*) unzip_format="${item#*--unzip-format=}" ;; @@ -172,13 +176,7 @@ function down() ( # tool helpers function do_aria2c { - local aria2c_options=() - if test -n "$download_file"; then - aria2c_options+=( - --out="$download_file" - ) - fi - aria2c_options+=( + local aria2c_options=( --dir="$download_directory" --file-allocation=none --allow-overwrite=true @@ -187,38 +185,76 @@ function down() ( --show-console-readout=false --summary-interval=1 --no-conf - "$url" ) + if test -n "$option_bearer_token"; then + aria2c_options+=( + "--header=Authorization: Bearer $option_bearer_token" + ) + fi + if test -n "$download_file"; then + aria2c_options+=( + --out="$download_file" + ) + fi + aria2c_options+=("$url") aria2c "${aria2c_options[@]}" } function do_curl { - pushd "$download_directory" >/dev/null + local curl_options=('-L') + if test -n "$option_bearer_token"; then + curl_options+=( + '--header' + "Authorization: Bearer $option_bearer_token" + ) + fi if test -n "$download_file"; then - curl -L "$url" -o "$download_file" + curl_options+=(-o "$download_file") else - curl -OL "$url" + curl_options+=(-O) fi - popd >/dev/null + curl_options+=("$url") + ( + cd "$download_directory" + curl "${curl_options[@]}" + ) } function do_got { # https://github.com/melbahja/got#command-line-tool-usage - pushd "$download_directory" >/dev/null + local got_options=() + if test -n "$option_bearer_token"; then + got_options+=( + '--header' + "Authorization: Bearer $option_bearer_token" + ) + fi if test -n "$download_file"; then - got -o "$download_file" "$url" - else - got "$url" + got_options+=(-o "$download_file") fi - popd >/dev/null + got_options+=("$url") + ( + cd "$download_directory" + got "${got_options[@]}" + ) } function do_httpie { # https://httpie.io/docs/cli/download-mode - pushd "$download_directory" >/dev/null + local http_options=('-dc') + if test -n "$option_bearer_token"; then + got_options+=( + '--auth' + "Bearer $option_bearer_token" + ) + fi if test -n "$download_file"; then - http -dco "$download_file" "$url" - else - http -dc "$url" + http_options+=( + -o "$download_file" + ) fi - popd >/dev/null + http_options+=("$url") + ( + cd "$download_directory" + http "${http_options[@]}" + ) } function do_wget() ( # -O, --output-document=FILE write documents to FILE @@ -230,13 +266,26 @@ function down() ( # -nv, --no-verbose turn off verboseness, without being quiet # --show-progress display the progress bar in any verbosity mode # WARNING: timestamping does nothing in combination with -O. See the manual for details. - pushd "$download_directory" >/dev/null + local wget_options=() + if test "$option_bearer_token"; then + options+=( + "--header=Authorization: Bearer $option_bearer_token" + ) + fi if test -n "$download_file"; then - wget --output-document="$download_file" "$url" + wget_options+=( + "--output-document=$download_file" + ) else - wget --timestamping "$url" + wget_options+=( + --timestamping + ) fi - popd >/dev/null + wget_options+=("$url") + ( + cd "$download_directory" + wget "${wget_options[@]}" + ) ) function do_download { if test "$(type -t "do_$tool")" = 'function'; then diff --git a/commands/setup-util b/commands/setup-util index 672b36d8a..f20537847 100755 --- a/commands/setup-util +++ b/commands/setup-util @@ -194,7 +194,7 @@ function setup_util() ( local CHOCO=() CHOCOLATEY=() CINST=() local CONDA=() CONDA_CHANNEL='' local DNF=() DNF_GROUP=() DNF_COPR=() DNF_REPO=() - local DOWNLOAD='' DOWNLOAD_FILENAME='' DOWNLOAD_UNZIP_FORMAT='' DOWNLOAD_UNZIP_FILTER='' DOWNLOAD_BUILD_EVAL='' DOWNLOAD_BUILD_FILTER='' + local DOWNLOAD='' DOWNLOAD_FILENAME='' DOWNLOAD_UNZIP_FORMAT='' DOWNLOAD_UNZIP_FILTER='' DOWNLOAD_BUILD_EVAL='' DOWNLOAD_BUILD_FILTER='' DOWNLOAD_BEARER_TOKEN='' local DPKG=() DEB=() local EMERGE=() EMERGE_REPO=() local EOPKG=() @@ -318,6 +318,7 @@ function setup_util() ( 'DNF_GROUP='*) DNF_GROUP+=("${item#*DNF_GROUP=}") ;; 'DNF_REPO='*) DNF_REPO+=("${item#*DNF_REPO=}") ;; 'DNF='*) DNF+=("${item#*DNF=}") ;; + 'DOWNLOAD_BEARER_TOKEN='*) DOWNLOAD_BEARER_TOKEN="${item#*DOWNLOAD_BEARER_TOKEN=}" ;; 'DOWNLOAD_BUILD_EVAL='*) DOWNLOAD_BUILD_EVAL="${item#*DOWNLOAD_BUILD_EVAL=}" ;; 'DOWNLOAD_BUILD_FILTER='*) DOWNLOAD_BUILD_FILTER="${item#*DOWNLOAD_BUILD_FILTER=}" ;; 'DOWNLOAD_FILENAME='*) DOWNLOAD_FILENAME="${item#*DOWNLOAD_FILENAME=}" ;; @@ -3129,7 +3130,7 @@ function setup_util() ( # download function do_download { - local bin_path download_directory='' download_filepath='' download="$DOWNLOAD" filename="$DOWNLOAD_FILENAME" unzip_format="$DOWNLOAD_UNZIP_FORMAT" unzip_filter="$DOWNLOAD_UNZIP_FILTER" build_eval="$DOWNLOAD_BUILD_EVAL" build_filter="$DOWNLOAD_BUILD_FILTER" + local bin_path download_directory='' download_filepath='' download="$DOWNLOAD" filename="$DOWNLOAD_FILENAME" unzip_format="$DOWNLOAD_UNZIP_FORMAT" unzip_filter="$DOWNLOAD_UNZIP_FILTER" build_eval="$DOWNLOAD_BUILD_EVAL" build_filter="$DOWNLOAD_BUILD_FILTER" bearer_token="$DOWNLOAD_BEARER_TOKEN" # checks if test -z "$download"; then @@ -3173,7 +3174,8 @@ function setup_util() ( --unzip-format="$unzip_format" \ --unzip-filter="$unzip_filter" \ --directory="$download_directory" \ - --filepath="$download_filepath" + --filepath="$download_filepath" \ + --bearer-token="$bearer_token" # build if test -n "$build_eval"; then diff --git a/commands/setup-util-bash b/commands/setup-util-bash index 0cf282415..c638ec30d 100755 --- a/commands/setup-util-bash +++ b/commands/setup-util-bash @@ -7,45 +7,7 @@ # @todo instead of compiling from source with DOWNLOAD, consider using homebrew bottles # can use [get-macos-release-name] to figure it out programatically -# > brew info --json bash | jq -r ".[].bottle.stable.files" -# { -# "arm64_ventura": { -# "cellar": "/opt/homebrew/Cellar", -# "url": "https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:f3a42b9282e6779504034485634a2f3e6e3bddfc70b9990e09e66e3c8c926b7d", -# "sha256": "f3a42b9282e6779504034485634a2f3e6e3bddfc70b9990e09e66e3c8c926b7d" -# }, -# "arm64_monterey": { -# "cellar": "/opt/homebrew/Cellar", -# "url": "https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:5e7e3e3387fc60e907683b437ac6e64879e117a3c5c1421fe6e6257f6aaa3c69", -# "sha256": "5e7e3e3387fc60e907683b437ac6e64879e117a3c5c1421fe6e6257f6aaa3c69" -# }, -# "arm64_big_sur": { -# "cellar": "/opt/homebrew/Cellar", -# "url": "https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:d19858831275271cc8aa9a1a28de6223faa44c6ebbc88e83898fd559de5b627e", -# "sha256": "d19858831275271cc8aa9a1a28de6223faa44c6ebbc88e83898fd559de5b627e" -# }, -# "ventura": { -# "cellar": "/usr/local/Cellar", -# "url": "https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:fd01a9dbdc56f6313a725cb345a3b991cfdaa9e1a91b08fd9791a0e695b55723", -# "sha256": "fd01a9dbdc56f6313a725cb345a3b991cfdaa9e1a91b08fd9791a0e695b55723" -# }, -# "monterey": { -# "cellar": "/usr/local/Cellar", -# "url": "https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:05a5f9435c9e9ffe8377b03e0ca6b27bbb32cc01aff47dd1692cd8d7e735ab3a", -# "sha256": "05a5f9435c9e9ffe8377b03e0ca6b27bbb32cc01aff47dd1692cd8d7e735ab3a" -# }, -# "big_sur": { -# "cellar": "/usr/local/Cellar", -# "url": "https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:680dd3b37e17cc4fa1af6dd8c51c774dd0c9aa3e594e96527020845516b1ea77", -# "sha256": "680dd3b37e17cc4fa1af6dd8c51c774dd0c9aa3e594e96527020845516b1ea77" -# }, -# "x86_64_linux": { -# "cellar": "/home/linuxbrew/.linuxbrew/Cellar", -# "url": "https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:6185e7cdba0e671528c9f38b104c4af58a670240672f83537bfc95983476fbc2", -# "sha256": "6185e7cdba0e671528c9f38b104c4af58a670240672f83537bfc95983476fbc2" -# } -# } -# 17:56:51:/Users/balupton/.local/share/dorothy:dev +# https://github.com/Homebrew/homebrew-core/pkgs/container/core%2Fbash # > brew info --json bash | jq -r ".[].bottle.stable.files" function setup_util_bash() ( @@ -74,14 +36,10 @@ function setup_util_bash() ( ./configure make } - local options=( + local bottle_url='' macos_release arch options=( "${extras[@]}" --cli='bash' "$@" - DOWNLOAD="https://ftp.gnu.org/gnu/bash/bash-${BASH_VERSION_LATEST}.tar.gz" - DOWNLOAD_UNZIP_FORMAT='tar' - DOWNLOAD_UNZIP_FILTER='bash-*/*' - DOWNLOAD_BUILD_EVAL='build_bash' APK='bash' # ALPINE APT='bash' # UBUNTU AUR='bash' # ARCH @@ -93,20 +51,52 @@ function setup_util_bash() ( XBPS='bash' # VOID ZYPPER='bash' # SUSE ) - setup_util "${options[@]}" - - # check if still outdated, if so, install via building instead - if is-bash-version-outdated --quiet; then - options=( - "${extras[@]}" - --cli='bash' - --upgrade - "$@" + arch="$(get-arch)" + if is-mac; then + macos_release="$(get-macos-release-name)" + if test "$arch" = 'a64'; then + if test "$macos_release" = 'ventura'; then + bottle_url='https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:f3a42b9282e6779504034485634a2f3e6e3bddfc70b9990e09e66e3c8c926b7d' + elif test "$macos_release" = 'monterey'; then + bottle_url='https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:5e7e3e3387fc60e907683b437ac6e64879e117a3c5c1421fe6e6257f6aaa3c69' + elif test "$macos_release" = 'big_sur'; then + bottle_url='https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:d19858831275271cc8aa9a1a28de6223faa44c6ebbc88e83898fd559de5b627e' + fi + elif test "$arch" = 'x64'; then + if test "$macos_release" = 'ventura'; then + bottle_url='https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:fd01a9dbdc56f6313a725cb345a3b991cfdaa9e1a91b08fd9791a0e695b55723' + elif test "$macos_release" = 'monterey'; then + bottle_url='https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:05a5f9435c9e9ffe8377b03e0ca6b27bbb32cc01aff47dd1692cd8d7e735ab3a' + elif test "$macos_release" = 'big_sur'; then + bottle_url='https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:680dd3b37e17cc4fa1af6dd8c51c774dd0c9aa3e594e96527020845516b1ea77' + fi + fi + elif is-linux && test "$arch" = 'x64'; then + bottle_url='https://ghcr.io/v2/homebrew/core/bash/blobs/sha256:6185e7cdba0e671528c9f38b104c4af58a670240672f83537bfc95983476fbc2' + fi + if test -n "$bottle_url" && fetch --bearer-token='QQ==' --ok "$bottle_url"; then + options+=( + DOWNLOAD="$bottle_url" + DOWNLOAD_BEARER_TOKEN='QQ==' + DOWNLOAD_UNZIP_FORMAT='tar' + DOWNLOAD_UNZIP_FILTER='bash/*/bin/bash' + ) + else + options+=( DOWNLOAD="https://ftp.gnu.org/gnu/bash/bash-${BASH_VERSION_LATEST}.tar.gz" DOWNLOAD_UNZIP_FORMAT='tar' DOWNLOAD_UNZIP_FILTER='bash-*/*' DOWNLOAD_BUILD_EVAL='build_bash' ) + fi + setup_util "${options[@]}" + + # check if still outdated, if so, install via building instead + if is-bash-version-outdated --quiet; then + options+=( + '--upgrade' + '--order=download' + ) setup_util "${options[@]}" fi