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