comparison mod_rest/example/rest.sh @ 5936:a9c75430cb26

mod_rest: use bash getopts to handle cli flags in example script
author Kim Alvefur <zash@zash.se>
date Sun, 21 Jul 2024 17:58:41 +0200
parents e5ad3f1f48bd
children
comparison
equal deleted inserted replaced
5935:46394b327d17 5936:a9c75430cb26
6 # Dependencies: 6 # Dependencies:
7 # - https://httpie.io/ 7 # - https://httpie.io/
8 # - https://hg.sr.ht/~zash/httpie-oauth2 8 # - https://hg.sr.ht/~zash/httpie-oauth2
9 9
10 # shellcheck disable=SC1091 10 # shellcheck disable=SC1091
11
12 SELF="${0##*/}"
13 function usage() {
14 echo "${SELF} [-h HOST] [-rw] [/path] kind=(message|presence|iq) ...."
15 # Last arguments are handed to HTTPie, so refer to its docs for further details
16 }
11 17
12 # Settings 18 # Settings
13 HOST="" 19 HOST=""
14 DOMAIN="" 20 DOMAIN=""
15 21
23 SCOPE="openid xmpp" 29 SCOPE="openid xmpp"
24 fi 30 fi
25 fi 31 fi
26 32
27 if [[ $# == 0 ]]; then 33 if [[ $# == 0 ]]; then
28 echo "${0##*/} [-h HOST] [-rw] [/path] kind=(message|presence|iq) ...." 34 usage
29 # Last arguments are handed to HTTPie, so refer to its docs for further details 35 exit 1
30 exit 0
31 fi 36 fi
32 37
33 if [[ "$1" == "-h" ]]; then 38 while getopts 'r:h:' flag; do
34 HOST="$2" 39 case "$flag" in
35 shift 2 40 r)
36 elif [ -z "${HOST:-}" ]; then 41 case "$OPTARG" in
42 o)
43 # Default
44 SESSION="session-read-only"
45 ;;
46 w)
47 # To e.g. save Accept headers to the session
48 SESSION="session"
49 ;;
50 *)
51 echo "E: -ro OR -rw" >&2
52 exit 1
53 ;;
54 esac
55 ;;
56 h)
57 HOST="$OPTARG"
58 ;;
59 *)
60 echo "E: Unknown flag '$flag'" >&2
61 usage >&2
62 exit 1
63 esac
64 done
65 shift $((OPTIND-1))
66
67 if [ -z "${HOST:-}" ]; then
37 HOST="$(hostname)" 68 HOST="$(hostname)"
38 fi
39
40 if [[ "$1" == "-rw" ]]; then
41 # To e.g. save Accept headers to the session
42 SESSION="session"
43 shift 1
44 fi 69 fi
45 70
46 if [[ "$HOST" != *.* ]]; then 71 if [[ "$HOST" != *.* ]]; then
47 # Assumes subdomain of your DOMAIN 72 # Assumes subdomain of your DOMAIN
48 if [ -z "${DOMAIN:-}" ]; then 73 if [ -z "${DOMAIN:-}" ]; then