Skip to content

Commit 4348fda

Browse files
committed
laitos-cli: a wip cli shell script
1 parent 1fbbbd3 commit 4348fda

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

laitos-cli.sh

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env bash
2+
3+
# A WIP laitos cli shell script.
4+
5+
set -Eeuo pipefail
6+
export LANG=C LC_ALL=C
7+
8+
handle_exit() {
9+
local -r -i exit_status="$?"
10+
echo "exiting with status code $exit_status"
11+
exit $exit_status
12+
}
13+
14+
trap handle_exit INT HUP TERM QUIT EXIT
15+
16+
print_usage() {
17+
local -r -i exit_status="$1"
18+
echo "$0 - laitos client CLI"
19+
echo "-u, --url"
20+
printf "\tInfo endpoint URLs (e.g. https://example.com/laitos-info), separated by comma, may be repeated.\n"
21+
exit $exit_status
22+
}
23+
24+
main() {
25+
local -a url_argv=() urls=()
26+
while true; do
27+
local arg_val=''
28+
if [ $# -eq 0 ]; then
29+
break
30+
fi
31+
case "$1" in
32+
-h | --help)
33+
print_usage 0
34+
;;
35+
--url=*)
36+
arg_val="${1#*=}"
37+
;&
38+
-u | --url)
39+
if [ ! "$arg_val" ]; then
40+
shift
41+
[ $# -ge 1 ] && arg_val="$1"
42+
fi
43+
[ ! "$arg_val" ] && print_usage 1
44+
url_argv+=("$arg_val")
45+
;;
46+
esac
47+
shift
48+
done
49+
# Split possibly comma separated server arg values.
50+
for argv in "${url_argv[@]}"; do
51+
while IFS=',' read -r -a fields; do
52+
urls+=("${fields[@]}")
53+
done <<<"$argv"
54+
done
55+
56+
printf "Fetching server info from %s URLs...\n" "${#urls[@]}"
57+
58+
local -A resp=()
59+
for url in "${urls[@]}"; do
60+
mapfile -d $'\0' resp_body < <(curl -H 'Accept: application/json' "$url" 2>/dev/null)
61+
resp["$url"]="$resp_body"
62+
wait
63+
done
64+
65+
for key in "${!resp[@]}"; do
66+
local val="${resp["$key"]}"
67+
printf "%s response length %s\n" "$key" "${#val}"
68+
done
69+
}
70+
71+
main "${@}"

0 commit comments

Comments
 (0)