From 8addf7ee9150705b862ef1547b83242b405d347b Mon Sep 17 00:00:00 2001 From: Max Howell Date: Sun, 19 Nov 2023 08:19:32 -0500 Subject: [PATCH] +shub --- .github/workflows/ci.yml | 12 ++++++++++ shub.sh | 51 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100755 shub.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..0e4cea5 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,12 @@ +on: + pull_request: + paths: shub.sh + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: pkgxdev/setup@v2 + - run: ./shub.sh pkgxdev/demo + - run: ./shub.sh pkgxdev/demo # check cache route works too diff --git a/shub.sh b/shub.sh new file mode 100755 index 0000000..5408ff2 --- /dev/null +++ b/shub.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +set -eo pipefail + +if test -n "$RUNNER_DEBUG" -a -n "$GITHUB_ACTIONS" -o -n "$VERBOSE"; then + set -x +fi + +SCRIPTNAME=$1 +shift + +if [ -z "$SCRIPTNAME" ]; then + echo "usage: shub [args…]" 1>&2 + exit 64 +fi + +if [ "$(uname)" = Darwin ]; then + CACHE="${XDG_CACHE_HOME:-$HOME/Library/Caches}/scripthub/$SCRIPTNAME" +else + CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/scripthub/$SCRIPTNAME" +fi + +get_etag() { + grep -i ETag "$CACHE/headers.txt" | sed -e 's/ETag: "\(.*\)"/\1/' | tr -d '\r' +} + +if [ -f "$CACHE/headers.txt" ] && ETAG=$(get_etag); then + ETAG=(--header If-None-Match:\ $ETAG) +else + mkdir -p "$CACHE" +fi + +URL="https://pkgxdev.github.io/scripthub/$SCRIPTNAME" + +if curl \ + $ETAG \ + --silent \ + --fail \ + --show-error \ + --dump-header "$CACHE/headers.txt" \ + --output "$CACHE/script" \ + "$URL" +then + chmod +x "$CACHE/script" + exec "$CACHE/script" "$@" +elif [ -f "$CACHE/$SCRIPTNAME" ]; then + echo "warn: couldn’t update check" 1>&2 + exec "$CACHE/$SCRIPTNAME" "$@" +else + echo "error: $URL" 1>&2 +fi