From 11053ebe6abe6a662944329be2cc7ae79f671951 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 18 Mar 2019 19:16:34 +0100 Subject: Add filtering parameters to status command Signed-off-by: David Oberhollenzer --- cmd/service/status.c | 59 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 7 deletions(-) (limited to 'cmd') diff --git a/cmd/service/status.c b/cmd/service/status.c index 3be2f3e..03ff463 100644 --- a/cmd/service/status.c +++ b/cmd/service/status.c @@ -4,8 +4,17 @@ #include "service.h" #include "config.h" +#include +#include #include +static const struct option long_opts[] = { + { "detail", no_argument, NULL, 'd' }, + { NULL, 0, NULL, 0 }, +}; + +static const char *short_opts = "d"; + static void free_resp(init_status_response_t *resp) { free(resp->filename); @@ -14,14 +23,26 @@ static void free_resp(init_status_response_t *resp) static int cmd_status(int argc, char **argv) { + bool is_tty, found, show_details = false; + int i, fd, ret = EXIT_FAILURE; init_status_response_t resp; - int fd, ret = EXIT_FAILURE; char tmppath[256]; const char *state; - bool is_tty; - if (check_arguments(argv[0], argc, 1, 1)) - return EXIT_FAILURE; + for (;;) { + i = getopt_long(argc, argv, short_opts, long_opts, NULL); + if (i == -1) + break; + + switch (i) { + case 'd': + show_details = true; + break; + default: + tell_read_help(argv[0]); + return EXIT_FAILURE; + } + } sprintf(tmppath, "/tmp/svcstatus.%d.sock", (int)getpid()); fd = init_socket_open(tmppath); @@ -50,6 +71,20 @@ static int cmd_status(int argc, char **argv) break; } + if (optind < argc) { + found = false; + + for (i = optind; i < argc; ++i) { + if (fnmatch(argv[i], resp.filename, 0) == 0) { + found = true; + break; + } + } + + if (!found) + continue; + } + switch (resp.state) { case ESS_RUNNING: if (!is_tty) { @@ -85,7 +120,14 @@ static int cmd_status(int argc, char **argv) break; } - printf("[%s] %s\n", state, resp.filename); + if (show_details) { + printf("Service: %s\n", resp.filename); + printf("\tStatus: %s\n", state); + printf("\tTemplate name: %s\n", resp.service_name); + printf("\tExit status: %d\n", resp.exit_status); + } else { + printf("[%s] %s\n", state, resp.filename); + } free_resp(&resp); } @@ -99,11 +141,14 @@ out: static command_t status = { .cmd = "status", - .usage = "", + .usage = "[-d|--detail] [services...]", .s_desc = "report the status of the currently enabled services", .l_desc = "Gathers a list of all currently running services and the " "state that they are in (currently running, done, failed, " - "wating to get scheduled).", + "wating to get scheduled). A list of services with wildcard " + "globbing patterns can be specified. If ommitted, produces " + "a general overview of all services. If the --detail " + "is given, more details are shown about a service.", .run_cmd = cmd_status, }; -- cgit v1.2.3