William-Owen

William-Owen / gist-pull

Last active 1 month ago

Like 0

A small Fish script can take an OpenGist URL, download the current HEAD ZIP, and extract all files without creating a Git repository.

Revision ecf0b01d030bccce93ca686a5df855819339b7f5

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