Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Created April 29, 2021 10:34
Show Gist options
  • Save rawiriblundell/5d8632046cbe6490ac9261f92939c842 to your computer and use it in GitHub Desktop.
Save rawiriblundell/5d8632046cbe6490ac9261f92939c842 to your computer and use it in GitHub Desktop.
Download the latest release file from a sourceforge project
# Usage: get_sourceforge [project] [linux|mac|windows]
get_sourceforge() {
# We require 'curl' and 'jq'
fail_count=0
for binary in curl jq; do
if ! command -v "${binary}" >/dev/null 2>&1; then
printf -- '%s\n' "${binary} is required but was not found in PATH" >&2
(( fail_count++ ))
fi
done
(( fail_count > 0 )) && return 1
unset -v binary fail_count
local sf_proj os_str curl_opts curl_target element remote_target
sf_proj="${1:?No sourceforge project defined}"
os_str="${2:-auto}"
curl_opts=( -s -L -I -o /dev/null -w '%{url_effective}' )
mapfile -t < <(
curl -s "https://sourceforge.net/projects/${sf_proj}/best_release.json" |
jq -r '.platform_releases[].url'
)
# shellcheck disable=SC2068
for element in ${MAPFILE[@]}; do
case "${element}" in
(*[lL]inux*) linux_src="${element}" ;;
(*[mM]ac*|*[dD]arwin*) mac_src="${element}" ;;
(*[wW]in*) win_src="${element}" ;;
esac
done
case "${os_str}" in
([lL]inux) curl_target="${linux_src}" ;;
([mM]ac*) curl_target="${mac_src}" ;;
([wW]in*) curl_target="${win_src}" ;;
(auto)
case "$(uname)" in
(Linux) curl_target="${linux_src}" ;;
(Darwin) curl_target="${mac_src}" ;;
(Win*|*WIN*) curl_target="${win_src}" ;;
esac
esac
remote_target="$(curl "${curl_opts[@]}" "${curl_target}")"
printf -- '%s\n' "Attempting to fetch ${remote_target}..."
curl -O "${remote_target}" || return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment