Skip to content

Commit

Permalink
+shub
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Nov 19, 2023
1 parent 49ed4ea commit 8addf7e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions shub.sh
Original file line number Diff line number Diff line change
@@ -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 <scriptname> [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

0 comments on commit 8addf7e

Please sign in to comment.