| 1 | #!/usr/bin/env fish |
| 2 | |
| 3 | function usage |
| 4 | echo "Usage: gist-pull <gist-url> [destination]" |
| 5 | echo |
| 6 | echo "Example:" |
| 7 | echo " gist-pull https://gist.example.com/william/my-gist ./config" |
| 8 | end |
| 9 | |
| 10 | if test (count $argv) -lt 1 |
| 11 | usage |
| 12 | exit 1 |
| 13 | end |
| 14 | |
| 15 | set -l gist_url $argv[1] |
| 16 | set -l destination "." |
| 17 | |
| 18 | if test (count $argv) -ge 2 |
| 19 | set destination $argv[2] |
| 20 | end |
| 21 | |
| 22 | # Remove query strings, fragments, trailing slash and optional .git suffix |
| 23 | set gist_url (string replace -r '[?#].*$' '' -- $gist_url) |
| 24 | set gist_url (string replace -r '/+$' '' -- $gist_url) |
| 25 | set gist_url (string replace -r '\.git$' '' -- $gist_url) |
| 26 | |
| 27 | if not string match -rq '^https?://' -- $gist_url |
| 28 | echo "Error: expected an HTTP or HTTPS OpenGist URL" >&2 |
| 29 | exit 1 |
| 30 | end |
| 31 | |
| 32 | set -l archive_url "$gist_url/archive/HEAD" |
| 33 | set -l archive (mktemp --suffix=.zip) |
| 34 | |
| 35 | if test $status -ne 0 |
| 36 | echo "Error: could not create temporary file" >&2 |
| 37 | exit 1 |
| 38 | end |
| 39 | |
| 40 | function cleanup --on-event fish_exit |
| 41 | rm -f $archive |
| 42 | end |
| 43 | |
| 44 | echo "Fetching $gist_url" |
| 45 | |
| 46 | if not curl --fail --location --silent --show-error \ |
| 47 | "$archive_url" \ |
| 48 | --output "$archive" |
| 49 | echo "Error: failed to download gist" >&2 |
| 50 | exit 1 |
| 51 | end |
| 52 | |
| 53 | mkdir -p "$destination" |
| 54 | |
| 55 | if command -q bsdtar |
| 56 | if not bsdtar -xf "$archive" -C "$destination" |
| 57 | echo "Error: failed to extract gist" >&2 |
| 58 | exit 1 |
| 59 | end |
| 60 | else if command -q unzip |
| 61 | if not unzip -oq "$archive" -d "$destination" |
| 62 | echo "Error: failed to extract gist" >&2 |
| 63 | exit 1 |
| 64 | end |
| 65 | else |
| 66 | echo "Error: bsdtar or unzip is required" >&2 |
| 67 | exit 1 |
| 68 | end |
| 69 | |
| 70 | echo "Updated: $destination" |
William-Owen / gist-pull
Last active 1 month ago
A small Fish script can take an OpenGist URL, download the current HEAD ZIP, and extract all files without creating a Git repository.