-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfetch
executable file
Β·197 lines (178 loc) Β· 5.34 KB
/
fetch
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
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env bash
function fetch_() (
source "$DOROTHY/sources/bash.bash"
# =====================================
# Arguments
function help {
cat <<-EOF >/dev/stderr
ABOUT:
Fetch the URL, using whichever tool is available on your system.
USAGE:
fetch [...options] <url>
OPTIONS:
--bearer-token=<token>
If provided, include this in a bearer token header.
--[no-]status[=<yes|no>]
If truthy, output only the status code, rather than the entire contents of the fetch URL.
--[no-]ok[=<yes|no>]
If truthy, skip fetching contents and only test if fetching the URL returned a success status code.
If falsey, skip fetching contents and only test if fetching the URL returned a non-successful status code.
QUIRKS:
If [curl] was required, but not found, it will be installed automatically.
EOF
if test "$#" -ne 0; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}
# process
local item option_auth_token='' option_content_type='' option_body='' option_url='' option_status='no' option_ok=''
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--auth-token='*) option_auth_token="${item#*=}" ;;
'--bot-token='*)
option_auth_token="${item#*=}"
if test -n "$option_auth_token"; then
option_auth_token="Bot $option_auth_token"
fi
;;
'--bearer-token='*)
option_auth_token="${item#*=}"
if test -n "$option_auth_token"; then
option_auth_token="Bearer $option_auth_token"
fi
;;
'--content-type='*) option_content_type="${item#*=}" ;;
'--json') option_content_type='application/json' ;;
'--body='*) option_body="${item#*=}" ;;
'--no-status'* | '--status'*)
option_status="$(get-flag-value --affirmative --fallback="$option_status" -- "$item")"
;;
'--no-ok'* | '--ok'*)
option_ok="$(get-flag-value --affirmative --fallback="$option_ok" -- "$item")"
;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*)
if test -z "$option_url"; then
option_url="$item"
else
help "An unrecognised argument was provided: $item"
fi
;;
esac
done
# check
if test -z "$option_url"; then
help "No URL was provided"
fi
# =====================================
# Helpers
function __log_failure {
local status="$?"
echo-style --error1='Failed to fetch the URL: ' --code-error1="$option_url" >/dev/stderr || return
return "$status"
}
# -I, --head Show document info only
# -m, --max-time <seconds> Maximum time allowed for the transfer
# -o, --output <file> Write to file instead of stdout
# -s, --silent No curl output, only content output.
# -w, --write-out <format> Use output FORMAT after completion
# -L, --location Follow redirects
# -f, --fail Fail on server errors.
# -A, --user-agent <name> Send User-Agent <name> to server
# -S, --show-error When used with -s, --silent, it makes curl show an error message if it fails.
function do_curl {
local options=("$@")
if test -n "$option_auth_token"; then
options+=(
'--header'
"Authorization: $option_auth_token"
)
fi
if test -n "$option_content_type"; then
options+=(
'--header'
"Content-Type: $option_content_type"
)
fi
if test -n "$option_body"; then
options+=(
'--data'
"$option_body"
)
fi
# use echo-wait to fix [curl: (23) Failure writing output to destination] when piping to ripgrep
{
curl "${options[@]}" "$option_url" || __log_failure
} | echo-wait
}
function do_wget {
local options=("$@")
if test -n "$option_auth_token"; then
options+=(
"--header=Authorization: $option_auth_token"
)
fi
wget "${options[@]}" "$option_url" || __log_failure
}
function fetch_status {
local options=(
--head # only fetch head
--max-time 3 # wait three seconds
--output /dev/null # no content output
--silent # no curl output
--write-out '%{http_code}' # output the http code
--location # follow redirects
)
setup-util-curl --quiet
do_curl "${options[@]}"
}
function fetch_ok {
# no wget equivalent
local status
status="$(fetch_status "$option_url")"
test "$status" -ge 200 -a "$status" -le 300
}
function fetch_contents {
local options
if __command_exists -- curl; then
options=(
--fail # -f, --fail fail fast with no output on HTTP errors
--silent # -s, --silent: only output content
--show-error # -S, --show-error: show error even when -s is used
--location # -L, --location: follow redirects
)
do_curl "${options[@]}"
elif __command_exists -- wget; then
options=(
-quiet # only output content
-O- # output to stdout
)
do_wget "${options[@]}"
else
get-installer --first-success --invoke --quiet -- curl wget
fetch_contents
fi
}
# =====================================
# Action
# perform appropriate action
if test "$option_status" = 'yes'; then
fetch_status
elif test "$option_ok" = 'yes'; then
fetch_ok
elif test "$option_ok" = 'no'; then
local ok_status
eval_capture --statusvar=ok_status -- fetch_ok
test "$ok_status" -ne 0
else
fetch_contents
fi
)
# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
fetch_ "$@"
fi