Skip to content

Instantly share code, notes, and snippets.

@MateuszKubuszok
Last active April 15, 2024 10:49
Show Gist options
  • Select an option

  • Save MateuszKubuszok/3bb02c2be0388fd1f63635fc438f851c to your computer and use it in GitHub Desktop.

Select an option

Save MateuszKubuszok/3bb02c2be0388fd1f63635fc438f851c to your computer and use it in GitHub Desktop.

Revisions

  1. MateuszKubuszok revised this gist Apr 15, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion github-badge-cache-buster.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    !/bin/bash
    #!/bin/bash

    defaultURL='https://github.com/ledgersmb/LedgerSMB'
    defaultFILE=''
  2. MateuszKubuszok revised this gist Apr 15, 2024. No changes.
  3. MateuszKubuszok created this gist Apr 15, 2024.
    87 changes: 87 additions & 0 deletions github-badge-cache-buster.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    !/bin/bash

    defaultURL='https://github.com/ledgersmb/LedgerSMB'
    defaultFILE=''

    HELP() {
    cat <<-EOF
    usage: github-badge-cache-buster.sh [ -h | --help | [ repoURL | "auto" [ badgeFile ] ] ]
    repoURL defaults to '$defaultURL'
    badgeFile defaults to '$defaultFILE'
    EOF
    exit 1;
    }
    [[ "$@" =~ --help|-h ]] && HELP;
    paramURL="${1:-auto}"
    paramFile="$2"
    init() {
    [[ -x `which curl` ]] || {
    cat <<-EOF
    ===============================
    ===============================
    == FATAL ERROR ==
    ===============================
    ===============================
    == we need to be able to run ==
    == curl ==
    ===============================
    == please install it ==
    == then rerun this script ==
    ===============================
    EOF
    exit 9
    }
    # try reading a repo url from the current repo. otherwise default to ledgersm/LedgerSMB
    read -t30 autoURL < <( git config --get remote.origin.url ) || autoURL="$defaultURL"
    # if a URL was passed on the command line use it, otherwise use $autoURL
    [[ "${paramURL:-auto}" == "auto" ]] && unset paramURL;
    repoURL="${paramURL:-$autoURL}";
    # if a URL was passed on the command line use it, otherwise use $autoURL
    badge_mdFILE="${paramFile:-$defaultFILE}"
    COLUMNS=`tput cols`
    UserAgentString='KLUDGE (Linux;) (please fix github tickets 224 116 111 218 414 etc)'
    }
    GetBadgeURLs() {
    curl --silent --user-agent "$UserAgentString" --output /dev/stdout "${repoURL}${badge_mdFILE}" | grep -o 'https://camo.githubusercontent.com.*alt="[^"]*'
    }
    PurgeBadges() {
    rm -f results.txt
    while read -t5 URL Badge; do
    URL="${URL%\"*}";
    Badge="'${Badge#*\"}' "
    echo -n "Purging Badge ${Badge:0:25}: ";
    read -t60 result < <( curl --silent --request 'PURGE' -H 'Content-Type:application/json' --user-agent "$UserAgentString" --output '/dev/stdout' $URL | jq .status )
    result="${result//\"/}"
    echo "${result:-???}"
    done
    }
    main() {
    init;
    cat <<-EOF
    ============================================
    == about to purge cached badge images for ==
    ============================================
    ${repoURL}${badge_mdFILE}
    ============================================
    EOF
    PurgeBadges < <( GetBadgeURLs )
    echo
    }
    main;