diff --git a/overlays/turnkey.d/github-latest-release/usr/local/bin/gh_releases b/overlays/turnkey.d/github-latest-release/usr/local/bin/gh_releases index 2b3f8ed5..40de3938 100755 --- a/overlays/turnkey.d/github-latest-release/usr/local/bin/gh_releases +++ b/overlays/turnkey.d/github-latest-release/usr/local/bin/gh_releases @@ -2,24 +2,29 @@ [[ -n $DEBUG ]] && set -x -repo_path="$1" # user/repo_name tmp_dir=/tmp/gh_releases rm -rf $tmp_dir mkdir -p $tmp_dir -fatal() { echo -e "\n[FATAL] $@" 1>&2; exit 1; } -warning() { echo -e "[WARNING] $@" 1>&2; } +fatal() { echo -e "\n[FATAL] $*" 1>&2; exit 1; } +warning() { echo -e "[WARNING] $*" 1>&2; } usage() { cat >&2 </ +Usage: $(basename "$0") [OPTIONS] / + +Options: + -h|--help show this help and exit + -a|--all show all (stable) versions, not just the most recent + -p|--pre-release + include alpha, beta and rc versions Env Vars: # used for github api GITHUB_USER GITHUB_USER_TOKEN - # retain temp files and set x if set + # retain temp files and set x DEBUG # omit tags or releases in output respectively if set @@ -31,6 +36,27 @@ Note: Setting GITHUB_USER and GITHUB_USER_TOKEN environment variables are EOF } +unset show_all pre_release repo_path +while [[ "$#" -gt 0 ]]; do + case $1 in + -a|--all) + show_all=true;; + -p|--pre-release) + pre_release=true;; + -h|--help) + usage + exit;; + *) + if [[ -z "$repo_path" ]]; then + repo_path="$1" + else + usage + fatal "Unknown option: $1" + fi;; + esac + shift +done + if [[ -z "$repo_path" ]]; then usage fatal "user/repo not provided!" @@ -44,7 +70,7 @@ fi if [[ -n "$GITHUB_USER" ]] && [[ -n "$GITHUB_USER_TOKEN" ]]; then USER="-u $GITHUB_USER:$GITHUB_USER_TOKEN" else - warning $warn "Authentication won't be used." + warning "$warn Authentication won't be used." USER="" fi @@ -62,12 +88,12 @@ get_page() { key=$2 page=$3 tmp_file=$(mktemp $tmp_dir/XXXX.tmp) - curl $USER -b /tmp/cookies.txt -c /tmp/cookies.txt -s "${url}?page=${page}&per_page=100" > $tmp_file || true - if grep "Bad credentials" $tmp_file >/dev/null ; then + curl "$USER" -b /tmp/cookies.txt -c /tmp/cookies.txt -s "${url}?page=${page}&per_page=100" > "$tmp_file" || true + if grep "Bad credentials" "$tmp_file" >/dev/null ; then fatal "Bad GitHub credentials" else - grep -oP "\"$key\": \"\\K(.*)(?=\")" $tmp_file - rm -f $tmp_file + grep -oP "\"$key\": \"\\K(.*)(?=\")" "$tmp_file" + rm -f "$tmp_file" fi } @@ -89,5 +115,12 @@ get_all_pages() { [[ -z $NO_TAGS ]] && get_all_pages "https://api.github.com/repos/${repo_path}/tags" "name" echo "Done!" 1>&2 -cat $tmp_dir/releases | sort --version-sort --unique +versions=$(cat $tmp_dir/releases | sort --version-sort --unique) +if [[ -z "$pre_release" ]]; then + versions=$(grep -vi 'alpha\|beta\|rc' <<<"$versions") +fi +if [[ -z "$show_all" ]]; then + versions=$(tail -1 <<<"$versions") +fi +echo "$versions" [[ -z $DEBUG ]] && rm -rf $tmp_dir