diff options
Diffstat (limited to 'src/piesctl.c')
-rw-r--r-- | src/piesctl.c | 108 |
1 files changed, 88 insertions, 20 deletions
diff --git a/src/piesctl.c b/src/piesctl.c index a9555a1..fcc346a 100644 --- a/src/piesctl.c +++ b/src/piesctl.c @@ -1558,33 +1558,87 @@ com_restart (struct shttp_connection *conn, int argc, char **argv) return 0; } +static void +id_fmt_string (struct json_value *val) +{ + fputs (val->v.s, stdout); +} + +static void +id_fmt_pid (struct json_value *val) +{ + printf ("%.0f", val->v.n); +} + +static void +id_fmt_argv (struct json_value *val) +{ + size_t i, n; + char const *delim = NULL; + + n = json_array_size (val); + for (i = 0; i < n; i++) + { + struct json_value *elt; + if (json_array_get (val, i, &elt) == 0 && elt->type == json_string) + { + if (delim) + fputs (delim, stdout); + fputs (elt->v.s, stdout); + delim = " "; + } + } +} + +struct id_fmt +{ + char *name; + enum json_value_type type; + void (*format) (struct json_value *); +}; + +static struct id_fmt id_fmt[] = { + { "package", json_string, id_fmt_string }, + { "version", json_string, id_fmt_string }, + { "instance", json_string, id_fmt_string }, + { "binary", json_string, id_fmt_string }, + { "argv", json_arr, id_fmt_argv }, + { "PID", json_number, id_fmt_pid }, + { NULL } +}; + +static struct id_fmt * +find_id_fmt (char const *name) +{ + struct id_fmt *p; + + for (p = id_fmt; p->name; p++) + if (strcmp (p->name, name) == 0) + return p; + return NULL; +} + static int com_id (struct shttp_connection *conn, int argc, char **argv) { struct json_value *v; - + struct id_fmt *fmt; + if (argc == 1) { - size_t i; - static char *keywords[] = { - "package", - "version", - "instance", - "binary", - }; - shttp_io_init (&conn->req); shttp_process (conn, METH_GET, "/instance"); - - for (i = 0; i < sizeof (keywords)/ sizeof (keywords[0]); i++) - { - v = shttp_getval (conn, keywords[i], json_string); - if (v) - printf ("%s: %s\n", keywords[i], v->v.s); - } - v = shttp_getval (conn, "PID", json_number); - if (v) - printf ("PID: %.0f\n", v->v.n); + if (!dump) + for (fmt = id_fmt; fmt->name; fmt++) + { + v = shttp_getval (conn, fmt->name, fmt->type); + if (v) + { + printf ("%s: ", fmt->name); + fmt->format (v); + putchar ('\n'); + } + } } else { @@ -1597,10 +1651,24 @@ com_id (struct shttp_connection *conn, int argc, char **argv) grecs_asprintf (&buf, &size, "/instance/%s", argv[i]); shttp_io_init (&conn->req); shttp_process (conn, METH_GET, buf); - if (json_object_get (conn->result, argv[i], &v) == 0) + if (dump) + continue; + fmt = find_id_fmt (argv[i]); + if (fmt) + { + v = shttp_getval (conn, fmt->name, fmt->type); + if (v) + { + printf ("%s: ", fmt->name); + fmt->format (v); + putchar ('\n'); + } + } + else if (json_object_get (conn->result, argv[i], &v) == 0) { printf ("%s: ", argv[i]); print_json (stdout, v); + putchar ('\n'); } } } |