Skip to content

Instantly share code, notes, and snippets.

@wallentx
Last active August 1, 2026 15:51
Show Gist options
  • Select an option

  • Save wallentx/b20de4c46355fdba828506312e8b006f to your computer and use it in GitHub Desktop.

Select an option

Save wallentx/b20de4c46355fdba828506312e8b006f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -eo pipefail
download_dir="."
while getopts ":d:v" opt; do
case ${opt} in
d)
download_dir=$OPTARG
;;
v)
set -x
;;
\?)
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
if ! command -v gh >/dev/null 2>&1; then
echo "GitHub CLI (gh) is required" >&2
exit 1
fi
if [[ ! -d $download_dir || ! -w $download_dir ]]; then
echo "Download directory is not writable: $download_dir" >&2
exit 1
fi
cursor_hidden=0
shutdown() {
if ((cursor_hidden)) && command -v tput >/dev/null 2>&1; then
tput cnorm 2>/dev/null || true
fi
}
trap shutdown EXIT
spin()
{
local p=${1:-$!} head=$((1 > 0 ? -1 : 17)) _GI=0 out='' i actual_i char sim_len age denom gval idx esc=$'\e'
local g=() a=() s=(\ \ \ \ 𝙻 𝙾 𝙰 𝙳 𝙸 𝙽 𝙶 . . . \ \ \ )
local greens256=(46 40 34 28 22 16) TRUECOLOR=0
if [[ ! -t 1 ]] || ! command -v tput >/dev/null 2>&1; then
wait "$p"
return
fi
case "${COLORTERM:-}" in *truecolor* | *24bit*) TRUECOLOR=1 ;; esac
for ((i = 0; i < 17; i++)); do g[i]='' a[i]=0; done
if tput civis 2>/dev/null; then
cursor_hidden=1
fi
while kill -0 "$p" 2> /dev/null; do
out=''
sim_len=17
((head += 1))
if ((1 > 0)); then
if ((head >= sim_len)); then
head=0
if ((0)); then for ((i = 0; i < 17; i++)); do g[i]='' a[i]=0; done; fi
fi
else
if ((head < 0)); then
head=$((sim_len - 1))
if ((0)); then for ((i = 0; i < 17; i++)); do g[i]='' a[i]=0; done; fi
fi
fi
for ((i = 0; i < 17; i++)); do
[[ -n "${g[i]:-}" ]] || continue
: "${a[i]:=0}"
((a[i]++))
if ((a[i] >= 11)); then g[i]='' a[i]=0; fi
done
g[head]="${s[_GI]}"
_GI=$(((_GI + 1) % ${#s[@]}))
a[head]=0
for ((i = 0; i < 17; i++)); do
actual_i=$i char=''
if [[ -n "${g[actual_i]:-}" ]]; then
char="${g[actual_i]}"
age=${a[actual_i]:-0}
if ((TRUECOLOR)); then
denom=$((11 - 1))
((denom <= 0)) && denom=1
gval=$((255 - (255 * age) / denom))
((gval < 0)) && gval=0
((gval > 255)) && gval=255
out+="${esc}[38;2;0;${gval};0m${char}${esc}[0m"
else
denom=$((11 - 1))
((denom <= 0)) && denom=1
idx=$((age * (${#greens256[@]} - 1) / denom))
((idx < 0)) && idx=0
((idx >= ${#greens256[@]})) && idx=$((${#greens256[@]} - 1))
out+="${esc}[38;5;${greens256[idx]}m${char}${esc}[0m"
fi
else out+=' '; fi
done
printf "%s\r" "$out"
sleep 0.040
done
printf '%s\r' "${esc}[2K"
if ((cursor_hidden)); then
tput cnorm 2>/dev/null || true
cursor_hidden=0
fi
wait "$p"
}
artifact_url="${1:-}"
if [[ -z "$artifact_url" ]]; then
echo "Artifact URL must be provided"
exit 1
fi
regex_suite="https://github.com/(.+)/(.+)/suites/[0-9]+/artifacts/([0-9]+)"
regex_run="https://github.com/(.+)/(.+)/actions/runs/[0-9]+/artifacts/([0-9]+)"
function to_human_readable() {
local size=$1
if [ "$size" -lt 1024 ]; then
echo "${size}B"
elif [ "$size" -lt 1048576 ]; then
echo $((size / 1024))"K"
elif [ "$size" -lt 1073741824 ]; then
echo $((size / 1048576))"M"
else
echo $((size / 1073741824))"G"
fi
}
function printArtifact() {
local owner="${BASH_REMATCH[1]}"
local repo="${BASH_REMATCH[2]}"
local artifact_id="${BASH_REMATCH[3]}"
local artifact_response
local -a artifact_info
if ! artifact_response=$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "/repos/$owner/$repo/actions/artifacts/$artifact_id" --jq '. | (.name, .archive_download_url, .size_in_bytes)'); then
echo "Failed to fetch artifact information" >&2
return 1
fi
mapfile -t artifact_info <<< "$artifact_response"
if ((${#artifact_info[@]} != 3)) || [[ ! ${artifact_info[2]} =~ ^[0-9]+$ ]]; then
echo "Invalid artifact information returned by GitHub" >&2
return 1
fi
artifact_name="${artifact_info[0]}.zip"
artifact_url="${artifact_info[1]}"
artifact_size="$(to_human_readable "${artifact_info[2]}")"
echo "Artifact Name: $artifact_name"
echo "Artifact Size: $artifact_size"
}
function getArtifact() {
gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "$artifact_url" > "$download_dir/$artifact_name"
}
function execInfo() {
getArtifact &
spin "$!"
if [[ -t 1 ]]; then
printf '\r'
fi
}
if [[ $artifact_url =~ $regex_suite ]] || [[ $artifact_url =~ $regex_run ]]; then
printArtifact
echo -n "Would you like to download this file? [Y/n] "
read -r answer
answer=${answer,,}
if [[ -z $answer || $answer == "y" || $answer == "yes" ]]; then
echo "Downloading $artifact_name..."
execInfo
echo "Download complete!"
else
echo "Aborted."
fi
else
echo "Invalid Artifact URL"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment