-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoc-jmap
More file actions
executable file
·62 lines (54 loc) · 1.72 KB
/
oc-jmap
File metadata and controls
executable file
·62 lines (54 loc) · 1.72 KB
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
#!/bin/bash
set -euo pipefail
: "${username:=alan}"
: "${password:=demo}"
: "${stalwart:=https://stalwart.opencloud.test}"
h=
for t in xh https curl; do
hash "$t" &>/dev/null && { h="$t"; break; }
done
[[ -n "$h" ]] || { echo "ERROR: failed to find supported HTTP client (httpie, xh, curl)" >&2; exit 1; }
function q {
local verb=$1; shift
local url=$1; shift
case "$h" in
xh|http|https) "$h" --follow --max-redirects=2 --verify=no -a "${username}:${password}" --ignore-stdin "${verb}" "${url}";;
curl) "$h" --silent --fail --location --insecure -u "${username}:${password}" -X "${verb}" "${url}";;
*) echo "ERROR: unsupported HTTP client: ${h}" >&2; exit 1;;
esac
return $?
}
function pretty {
local verb=$1; shift
local url=$1; shift
case "$h" in
xh|http|https) "$h" --follow --max-redirects=2 --verify=no -a "${username}:${password}" --print=HBhb --pretty=all "${verb}" "${url}";;
curl) "$h" --silent --fail --location --insecure -u "${username}:${password}" -X "${verb}" -D- "${url}" --data-binary @- | jq -R -r '. as $line | try fromjson catch $line';;
*) echo "ERROR: unsupported HTTP client: ${h}" >&2; exit 1;;
esac
return $?
}
account_id=$(q GET "${stalwart}/.well-known/jmap" | jq -r '.primaryAccounts."urn:ietf:params:jmap:mail"')
api_url=$(q GET "${stalwart}/.well-known/jmap" | jq -r '.apiUrl')
declare -a args
url=''
for a in "$@"; do
case "$a" in
/*) args+=("${stalwart}$a");;
*) args+=("$a");;
esac
done
function jmap_source() {
local f="$1"
if [[ -n "$f" ]]; then
tail -n +2 < "$f"
else
cat
fi
return $?
}
f=
[[ $# > 0 ]] && f="$1"
body=$(jmap_source "$f" | account_id="${account_id}" accountId="${account_id}" envsubst)
pretty POST "${api_url}" <<<$body
exit $?