-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
remote
executable file
·65 lines (55 loc) · 1.5 KB
/
remote
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
#!/usr/bin/env zsh
# simple remote cli for sawroom
# requires zsh, sed, curl and jq
# default sawroom testnet
host=195.201.41.35
chain_port=8008
api_port=9009
user=demo
pass=demo
cmd="$1"
[[ $1 ]] || {
print "usage: zenroom script.zen | ./remote command/path"
print " config host, user, pass and ports inside the script"
return 1
}
function json_extract {
tmpjq=`mktemp`
tmpin=`mktemp`
cat > $tmpin
cat <<EOF > $tmpjq
# break out early
def filter(\$key):
label \$out
| foreach inputs as \$in ( null;
if . == null
then if \$in[0][0] == \$key then \$in
else empty
end
elif \$in[0][0] != \$key then break \$out
else \$in
end;
select(length==2) );
reduce filter(\$key) as \$in ({};
setpath(\$in[0]; \$in[1]) )
EOF
jq -n -c --arg key "$1" --stream -f $tmpjq $tmpin | sed 's/.*"\(.*\)"[^"]*$/\1/'
rm -f $tmpjq
rm -f $tmpin
}
function json_escape() {
sed -E 's/([^\]|^)"/\1\\"/g'
}
# get api auth token
token=`mktemp`
curl -X POST "http://${host}:${api_port}/token" -H "accept: application/json" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=&username=${user}&password=${pass}" | json_extract access_token > $token
body=`mktemp`
cat | json_escape > $body
curl -X POST "http://${host}:${api_port}/${cmd}/?address=http%3A%2F%2Flocalhost%3A${chain_port}%2Fbatches" \
-H "accept: application/json" -H "Authorization: Bearer `cat $token`" -H "Content-Type: application/json" \
-d `cat $body`
# cleanup
rm -f $body
rm -f $token