forked from cowboy/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curlsubl
executable file
·48 lines (36 loc) · 1.4 KB
/
curlsubl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env bash
if [[ ! "$1" || "$1" == "-h" || "$1" == "--help" ]]; then cat <<HELP
Curl a remote file into an editor.
http://benalman.com/
echo "Usage: $(basename "$0") [curloptions...] url"
Curl a remote file into the editor of your choice. The filename will be based
on the URL's filename. If a file extension can't be determined from the URL,
index.html will be used.
The editor will be auto-magically determined by stripping any leading "curl"
off this script's filename. For example, call this script "curlmate" and "mate"
will be run.
Copyright (c) 2012 "Cowboy" Ben Alman
Licensed under the MIT license.
http://benalman.com/about/license/
HELP
exit; fi
script="$(basename "$0")"
bin="${script#curl}"
cache_dir=$DOTFILES/caches/"$script"
# Create tempfile.
tmp="$(mktemp "/tmp/$script.XXXXX")"
# Curl (using all specified options) to tempfile, storing url for later.
echo -e 'Fetching file...\n'
url="$(curl "$@" -o "$tmp" -w '%{url_effective}')"
# Remove leading http:// or https:// from url, and add index.html at the end if
# filename can't be determined.
path="$(echo "$url" | perl -pe's#^https?://##i;s#/$|(^[^/\s]+|/[^/.\s]+)$#$1/index.html#')"
dir="$cache_dir/$(dirname "$path")"
file="$dir/$(basename "$path")"
# Create directory if it doesn't exist.
[ -e "$dir" ] || mkdir -p "$dir"
# Copy tempfile to final file.
mv "$tmp" "$file"
# Open file in editor!
echo -e "\nOpening ~${file#$HOME}"
"$bin" "$file"