Skip to content

Commit

Permalink
Handle pagination for Github release API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbode committed Apr 1, 2024
1 parent 1445d7e commit 0709603
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 37 deletions.
57 changes: 31 additions & 26 deletions bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,6 @@ source "${MISE_PLUGIN_PATH-${0%/*}/..}/utils.sh"

DOWNLOAD_PATH="https://github.com/getsops/sops/releases/download"

ERRORS=0

if [[ -z ${MISE_INSTALL_TYPE-} ]]; then
echoerr "MISE_INSTALL_TYPE is required"
ERRORS=$((ERRORS + 1))
fi
if [[ -z ${MISE_INSTALL_VERSION-} ]]; then
echoerr "MISE_INSTALL_VERSION is required"
ERRORS=$((ERRORS + 1))
fi
if [[ -z ${MISE_INSTALL_PATH-} ]]; then
echoerr "MISE_INSTALL_PATH is required"
ERRORS=$((ERRORS + 1))
fi

if ((ERRORS)); then
exit 1
fi

install_sops() {
local install_type=$1
if [[ "${install_type}" != "version" ]]; then
Expand All @@ -49,8 +30,8 @@ install_sops() {
mkdir -p "${bin_install_path}"

echo "Downloading sops from ${download_url}"
if ! curl -qSsfL "${download_url}" -o "${bin_path}"; then
echoerr "Error: URL responded exceptionally"
if ! curl -qsSfL -o "${bin_path}" "${download_url}"; then
echoerr "URL responded exceptionally"
return 1
fi
chmod +x "${bin_path}"
Expand All @@ -64,10 +45,10 @@ get_cpu() {
local machine_hardware_name
machine_hardware_name=${MISE_SOPS_OVERWRITE_ARCH:-"$(uname -m)"}

case "${machine_hardware_name}" in
'x86_64') local cpu_type="amd64" ;;
'aarch64') local cpu_type="arm64" ;;
*) local cpu_type="${machine_hardware_name}" ;;
case ${machine_hardware_name} in
x86_64) local cpu_type="amd64" ;;
aarch64) local cpu_type="arm64" ;;
*) local cpu_type=${machine_hardware_name} ;;
esac

echo "${cpu_type}"
Expand All @@ -86,4 +67,28 @@ get_download_url() {
echo "${url}"
}

install_sops "${MISE_INSTALL_TYPE}" "${MISE_INSTALL_VERSION}" "${MISE_INSTALL_PATH}"
main() {
ERRORS=()

if [[ -z ${MISE_INSTALL_TYPE-} ]]; then
ERRORS+=("MISE_INSTALL_TYPE is required")
fi
if [[ -z ${MISE_INSTALL_VERSION-} ]]; then
ERRORS+=("MISE_INSTALL_VERSION is required")
fi
if [[ -z ${MISE_INSTALL_PATH-} ]]; then
ERRORS+=("MISE_INSTALL_PATH is required")
fi

if ((${#ERRORS[@]})); then
echoerr "${ERRORS[@]}"
exit 1
fi

install_sops \
"${MISE_INSTALL_TYPE}" \
"${MISE_INSTALL_VERSION}" \
"${MISE_INSTALL_PATH}"
}

main
29 changes: 19 additions & 10 deletions bin/list-all
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
# shellcheck source-path=..
source "${MISE_PLUGIN_PATH-${0%/*}/..}/utils.sh"

RELEASES_PATH=https://api.github.com/repos/getsops/sops/releases
EXCLUDE_VERSIONS="2\.\+"
RELEASES_PATH="https://api.github.com/repos/getsops/sops/releases?per_page=100"
NEXT_LINK='<([^\>]+)>; rel="next"'

if [[ -n ${GITHUB_API_TOKEN-} ]]; then
set -- "$@" -H "Authorization: token ${GITHUB_API_TOKEN}"
VERSION_RE='[0-9][^"]\+'
LINE_RE=$(printf '"tag_name": "v\\?%s"' "${VERSION_RE}")

if [[ -n ${GITHUB_TOKEN-} ]]; then
set -- "$@" -H "Authorization: Bearer ${GITHUB_TOKEN}"
fi

# stripping off the 'v' prefix as it causes issues with 'mise install foo latest'
curl -qsSL "$@" "${RELEASES_PATH}" |
grep -o '"tag_name": "v\?[0-9][^"]\+"' |
grep -o '[0-9][^"]\+' |
grep -v "^${EXCLUDE_VERSIONS}" |
sort_versions
URL=${RELEASES_PATH}

# strip off the 'v' prefix as it causes issues with 'mise install foo latest'
while [[ -n ${URL} ]]; do
RESULT=$(curl --include -qsSfL "$@" "${URL}")
grep -o "${LINE_RE}" <<< "${RESULT}"
if LINK=$(grep -m1 '^link: ' <<< "${RESULT}") && [[ ${LINK} =~ ${NEXT_LINK} ]]; then
URL=${BASH_REMATCH[1]}
else
break
fi
done | grep -o "${VERSION_RE}" | sort_versions
2 changes: 1 addition & 1 deletion utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set -euo pipefail
export RELEASES_PATH=https://api.github.com/repos/getsops/sops/releases

echoerr() {
printf 'mise-sops: %s\n' "$1" >&2
printf 'mise-sops: %s\n' "$@" >&2
}

sort_versions() {
Expand Down

0 comments on commit 0709603

Please sign in to comment.