-
Notifications
You must be signed in to change notification settings - Fork 1
/
set-echo
executable file
·69 lines (60 loc) · 1020 Bytes
/
set-echo
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
#!/bin/bash
set -eu
set -o pipefail
function debug()
{
if [ "$DEBUG" -ne 0 ]
then
echo "$@" >&2
fi
}
if [ "$#" -eq 1 ]
then
H="$1"
T=""
elif [ "$#" -eq 2 ]
then
H="$1"
T="$2"
else
echo "$0 <IP address> [token]" >&2
echo
exit 1
fi
if [[ ! -v XLED_DEBUG ]]
then
if [ -t 1 ]
then
XLED_DEBUG_INTERNAL=1
else
XLED_DEBUG_INTERNAL=0
fi
else
XLED_DEBUG_INTERNAL="$XLED_DEBUG"
unset XLED_DEBUG
fi
IFS= read -r -d '' content || true
escaped_content=$(echo "$content" | awk '{printf "%s\\n", $0}')
if [ -z "$T" ]
then
T=$(./auth "$H")
fi
E="echo"
if [ "$XLED_DEBUG_INTERNAL" -ne 0 ]
then
echo "POST $E" >&2
fi
read -r -d '' data <<EOF || true
{
"message": "$escaped_content"
}
EOF
if [ "$XLED_DEBUG_INTERNAL" -ne 0 ]
then
echo "payload:" >&2
echo "$data" | jq
echo "response:" >&2
echo "$data" | XLED_DEBUG=0 ./post-json "$H" "$T" "$E" | jq
else
echo "$data" | XLED_DEBUG=0 ./post-json "$H" "$T" "$E"
fi