-
Notifications
You must be signed in to change notification settings - Fork 4
/
curl-exit-code-to-string
executable file
·105 lines (84 loc) · 2.92 KB
/
curl-exit-code-to-string
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
#!/bin/bash
# Convert curl return code to human friendly text.
# See 'man curl' for more info.
DIE() {
echo "Error: $1" >&2
exit 1
}
Generate() {
local tmp=$(mktemp)
local data=""
# Get exit codes and strings from the curl man page.
data="$(MANWIDTH=$width man curl | sed -n '/^EXIT CODES$/,/^AUTHORS \/ CONTRIBUTORS$/'p | grep "^ [ ]*[0-9][0-9]* [ ]*")"
data="$(echo "$data" | sed "s|\"|'|g")"
data="$(echo "$data" | sed 's|^\([ ]*[0-9]*\)[ ]*\(.*\)|\1) echo "\2" ;;|')"
echo "==> Creating $converter" >&2
cat <<EOF > "$converter"
#!/bin/bash
_generated_func_() {
local curlRetCode="\$1"
local msg
case "\$curlRetCode" in
$(echo "$data")
0) echo "OK" ;;
*) echo "[Error] The manual page of 'curl' does not recognize error code '\$curlRetCode'."
return 1
;;
esac
}
_generated_func_ "\$1"
EOF
[ -r "$converter" ] || DIE "Creating '$converter' failed."
chmod +x "$converter"
}
usage_f() {
local msg="$1"
cat <<EOF
Convert the curl exit code to a descriptive string.
Usage: $progname { curl-exit-code | option }
Options:
-c, --create Generate up to date curl string list.
-r, --remove Remove the current curl string list.
-h, --help This help.
Example:
$progname 6
EOF
case "$msg" in
"") exit 0 ;; # exit normally
"stay") ;; # do not exit
*) # exit with a message
printf "\nError: %s\n" "$msg" >&2
exit 1
;;
esac
}
CurlReturnCodeToString() {
local progname=curl-exit-code-to-string
local width=300 # man page error strings can be long, currently code 9 line needs over 200 characters ...
[ -n "$1" ] || usage_f "parameter missing"
local curlRetCode=""
local cdir="$HOME/.config/$progname"
local converter="$cdir/curl-code-to-string-converter"
local timenow_sec=$(date +%s)
local timefile_sec=0
local generate_interval_seconds=$((3600*24*7)) # weekly automatic generation of the curl error code list
mkdir -p "$cdir"
[ -r "$converter" ] && timefile_sec=$(stat -c %Y "$converter")
while [ -n "$1" ] ; do
case "$1" in
--create | -c) Generate ; return ;;
--remove | -r) echo "==> Removing $converter" >&2 ; rm -f "$converter" ; return ;;
--converter) echo "$converter" ; return 0 ;;
--width) echo "$width" ; return 0 ;;
--help | -h) usage_f ;;
-*) DIE "unsupported option '$1'" ;;
*) curlRetCode="$1" ;;
esac
shift
done
# ((timenow_sec += generate_interval_seconds)) # testing generation!
[ $timenow_sec -gt $((timefile_sec + generate_interval_seconds)) ] && Generate
[ -x "$converter" ] || DIE "curl code to text converter '$converter' not found or not executable."
"$converter" "$curlRetCode"
}
CurlReturnCodeToString "$@"