Files
Madison Grubb 7df63f21aa fixes to imagen
2026-03-13 11:17:38 -04:00

75 lines
2.3 KiB
Bash

#!/bin/bash -e
################################################################################
## File: yq.sh
## Desc: Installs YQ with checksum validation
################################################################################
# source: https://github.com/actions/runner-images/blob/5d6938f680075d63fa71f8aa70990866cd12884b/images/linux/scripts/installers/yq.sh
download_with_retries() {
local URL="$1"
local DEST="${2:-.}"
local NAME="${3:-${URL##*/}}"
echo "Downloading '$URL' to '${DEST}/${NAME}'..."
local retries=20
local interval=30
while [ "$retries" -gt 0 ]; do
((retries--)) || true
if curl -fsSL -o "${DEST}/${NAME}" "$URL"; then
echo "Download completed"
return 0
fi
echo "Error downloading. Waiting ${interval}s before retry, ${retries} attempts left"
sleep "$interval"
done
echo "Could not download $URL"
return 1
}
get_hash_from_remote_file() {
local url=$1
local keyword=$2
local delimiter=${3:-' '}
local word_number=${4:-1}
local line
line=$(curl -fsSL "$url" | sed 's/ */ /g' | tr -d '`' | grep "$keyword" | head -n1 || true)
if [ -z "$line" ]; then
echo "Keyword ($keyword) not found in the file with hashes." >&2
exit 1
fi
echo "$line" | cut -d "$delimiter" -f "$word_number" | tr -d -c '[:alnum:]'
}
use_checksum_comparison() {
local file_path=$1
local checksum=$2
local sha_type=${3:-256}
local local_file_hash
echo "Performing checksum verification"
if [ ! -f "$file_path" ]; then
echo "File not found: $file_path" >&2
exit 1
fi
local_file_hash=$(shasum -a "$sha_type" "$file_path" | awk '{print $1}')
if [ "$local_file_hash" != "$checksum" ]; then
echo "Checksum verification failed. Expected: $checksum; Actual: $local_file_hash." >&2
exit 1
fi
echo "Checksum verification passed"
}
yq_arch() {
case "$(uname -m)" in
aarch64) echo arm64 ;;
x86_64) echo amd64 ;;
armv7l) echo arm ;;
*) exit 1 ;;
esac
}
base_url="https://github.com/mikefarah/yq/releases/latest/download"
filename="yq_linux_$(yq_arch)"
download_with_retries "${base_url}/${filename}" "/tmp" "yq"
external_hash=$(get_hash_from_remote_file "${base_url}/checksums" "${filename} " " " "19")
use_checksum_comparison "/tmp/yq" "${external_hash}"
sudo install /tmp/yq /usr/bin/yq