-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-from-url
executable file
·187 lines (158 loc) · 4.06 KB
/
run-from-url
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bash
export PS4='+${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): } '
set -e
readonly version=0
function print-help() {
local -r width=$(($(tput cols) - 2))
cat <<EOH | fold -s -w $width
Usage: ${0##*/} [-houv] URL FILE [...]
Usage: ${0##*/} -i [-houv] [--] [...]
Runs a local copy of a BASH script fetched from the Internet, caching based on ETag.
WARNING: Do not run scripts from the Internet without close inspection.
With URL and FILE, wrap the rest of the command line, otherwise run in place.
-h, --help print help and exit normally
-i, --inplace run inplace
-o, --offline run local copy, do not update
-u, --update update local copy, do not run
-v, --verbose print verbose output
If NAME is the script name, caches the ETag for URL in .NAME.etag.
If run inplace and NAME is the script name, the local copy is .NAME and the URL is read from .NAME.url. Use -- to distinguish flags for this script from flags for the local copy.
Examples:
${0##*/} https://github.com/some_one/some_script .some_script -flag arg
some_script -i -- -flag arg
EOH
}
function print-version() {
cat <<EOV
${0##*/} $version
This is free and unencumbered software released into the public domain.
For more information, please refer to <http://unlicense.org/>.
Written by B. K. Oxley (binkley).
EOV
}
inplace=false
offline=false
update=false
verbose=false
while getopts :hiouv-: opt; do
[[ - == "$opt" ]] && opt="${OPTARG%%=*}" OPTARG="${OPTARG#*=}"
case $opt in
h | help)
print-help
exit 0
;;
i | inplace) inplace=true ;;
o | offline) offline=true ;;
u | update) update=true ;;
v | verbose) verbose=true ;;
version)
print-version
exit 0
;;
:)
cat <<EOE >&2
${0##*/}: option requires an argument -- '$OPTARG'
Try '${0##*/} --help' for more information.
EOE
exit 2
;;
?)
cat <<EOE >&2
${0##*/}: invalid option -- '$OPTARG'
Try '${0##*/} --help' for more information.
EOE
exit 2
;;
esac
done
shift $((OPTIND - 1))
if $offline && $update; then
cat <<EOE >&2
${0##*/}: both -o and -u
Try '${0##*/} --help' for more information.
EOE
exit 2
fi
name=${0##*/}
if $inplace; then
pushd "${0%/*}"
url_file=$PWD/.$name.url
url="$(<"$url_file")"
content_file="$PWD/.$name"
popd
set -- "$url" "$content_file" "$@"
fi >/dev/null
case $# in
0 | 1)
cat <<EOE >&2
${0##*/}: missing arguments
Try '${0##*/} --help' for more information.
EOE
exit 2
;;
\
*)
url=$1
case $2 in
/*) content_file=$2 ;;
*) content_file=$PWD/$2 ;;
esac
readonly url content_file
;;
esac
shift 2
if $offline; then
exec $content_file "$@"
fi
case $name in
.*) etag_file=$(dirname "$content_file")/$name.etag ;;
*) etag_file=$(dirname "$content_file")/.$name.etag ;;
esac
readonly etag_file
[[ -s $etag_file ]] || echo 0 >"$etag_file"
old_etag="$(<"$etag_file")"
readonly old_etag
trap 'rm -rf $tmp_dir' EXIT
tmp_dir="$(mktemp -d 2>/dev/null || mktemp -d -t "${0##*/}")"
tmp_headers_file=$tmp_dir/headers
tmp_content_file=$tmp_dir/content
read -r http_code < <(curl -s -D "$tmp_headers_file" -o "$tmp_content_file" \
-w '%{http_code}\n' \
-H "If-None-Match: \"$old_etag\"" \
"$url")
readonly http_code
readonly no_update=304
readonly updated=200
case $http_code in
"$no_update")
if $verbose; then
echo "${0##*/}: no update" >&2
fi
rm -rf "$tmp_dir"
exec $content_file "$@"
;;
"$updated") if $verbose; then
echo "${0##*/}: updated" >&2
fi ;;
*)
echo "${0##*/}: response $http_code for $url" >&2
exit 1
;;
esac
while read -r header value; do
[[ etag: == "${header,,}" ]] || continue
new_etag=${value%$'\r'}
new_etag=${new_etag#\"}
new_etag=${new_etag%\"}
break
done <"$tmp_headers_file"
readonly new_etag
if [[ -z "$new_etag" ]]; then
echo "$0: no ETag for $url" >&2
exit 1
fi
mv "$tmp_content_file" "$content_file"
chmod +x "$content_file"
echo "$new_etag" >"$etag_file"
rm -rf "$tmp_dir"
exec $content_file "$@"