aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/afm-util.in
blob: 45946852d7177f1e145fcb6a445acc0c61c2dd9c (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash

send() {
  local verb="$1"
  afb-client-demo -H -d unix:@afm_platform_rundir@/apis/ws/afm-main "$verb" "$2" |
  awk '$1=="ON-REPLY" && $3!="success"{$1="ERROR:";$2="";print > "/dev/stderr";exit 1;}NR>1'
}

all=false
force=false
uid="$UID"
help=false

set -- $(getopt -l all,force,help,uid: -s afhu: -n afm-util -- "$@")
while :
do
  case "$1" in
  -a|--all) all=true; shift;;
  -f|--force) force=true; shift;;
  -h|--help) help=true; shift;;
  -u|--uid) uid="$2"; shift 2;;
  --) shift; break;;
  *) help=true; break;;
  esac
done

getall() {
  case "$1" in
    -a|--all) echo -n '{"all":true}';;
    *) echo -n true;;
  esac
}

case "$1" in

  list|runnables)
    send runnables "{\"all\":$all,\"uid\":$uid}"
    ;;

  add|install)
    f=$(realpath $2)
    r=true
    if [[ "$(basename $0)" = "afm-install" ]]; then r=false; fi
    send install "{\"wgt\":\"$f\",\"force\":$force,\"reload\":$r}"
    ;;

  remove|uninstall)
    i=$2
    send uninstall "\"$i\""
    ;;

  info|detail)
    i=$2
    send detail "{\"id\":$i,\"uid\":$uid}"
    ;;

  ps|runners)
    send runners  "{\"all\":$all,\"uid\":$uid}"
    ;;

  run|start)
    i=$2
    send start "{\"id\":$i,\"uid\":$uid}"
    ;;

  once)
    i=$2
    send once "{\"id\":$i,\"uid\":$uid}"
    ;;

  terminate|kill)
    i=$2
    send terminate  "{\"runid\":$i,\"uid\":$uid}"
    ;;

  state|status)
    i=$2
    send state  "{\"runid\":$i,\"uid\":$uid}"
    ;;

  -h|--help|help)
    cat << EOC
usage: $(basename $0) command [arg]

The commands are:

  list
  runnables      list the runnable widgets installed
                 option -a or --all for all instances

  add wgt
  install wgt    install the wgt file

  remove id
  uninstall id   remove the installed widget of id

  info id
  detail id      print detail about the installed widget of id

  ps
  runners        list the running instance
                 option -a or --all for all instances

  run id
  start id       start an instance of the widget of id

  once id        run once an instance of the widget of id

  kill rid
  terminate rid  terminate the running instance rid

  status rid
  state rid      get status of the running instance rid

EOC
    ;;

  *)
    echo "unknown command $1" >&2
    exit 1
    ;;
esac