c98e748b11b19ea8b625fd02ac2967862b121e21
[staging/windowmanager.git] / scripts / wm-request
1 #!/bin/sh
2
3 nopygments=0
4 if [ "$1" = "-p" ]
5 then
6    nopygments=1
7    shift
8 fi
9
10 if ! [ "$1" ]
11 then
12    echo "Usage: $0 VERB [ARGS]" >&2
13    exit 1
14 fi
15
16 for i in uuidgen curl
17 do
18    which $i 2>/dev/null 1>&2 || { echo "Program $i is missing" >&2; exit 1; }
19 done
20
21 set -eu
22
23 if which python 2>/dev/null 1>&2 && echo '{ "test": "1" }' | python -m json.tool 2>/dev/null 1>&2
24 then
25    if [ $nopygments = 0 ] && which pygmentize 2>/dev/null 1>&2
26    then
27       json_pretty() {
28          python -m json.tool | pygmentize -l json
29       }
30    else
31       json_pretty() {
32          python -m json.tool
33       }
34    fi
35 else
36    json_pretty() {
37       cat
38    }
39 fi
40
41 verb=$1
42 shift
43
44 args=""
45 for i in "$@"
46 do
47    args="$i"
48 done
49
50 if [ "$args" ]
51 then
52    args="?$args"
53 fi
54
55 UUIDFILE=/tmp/wm-request-uuid
56 if ! [ -f $UUIDFILE ]
57 then
58    uuidgen > $UUIDFILE
59 fi
60 UUID="`cat $UUIDFILE`"
61
62 curl -H "x-afb-uuid: $UUID" -s http://localhost:1234/api/winman/${verb}${args} | json_pretty
63
64 # vim:set ft=sh: