Welcome to little lamb

Code » anopa » commit 2c032e1

status: Add --listdir

author Olivier Brunel
2015-07-15 17:17:23 UTC
committer Olivier Brunel
2015-07-15 17:17:23 UTC
parent 2b89c3bd887fa7ac5abf3ce747f7fe7691f39cb8

status: Add --listdir

doc/aa-status.pod +10 -1
src/anopa/aa-status.c +35 -2

diff --git a/doc/aa-status.pod b/doc/aa-status.pod
index 0edb910..ef1b3df 100644
--- a/doc/aa-status.pod
+++ b/doc/aa-status.pod
@@ -4,7 +4,8 @@ aa-status - Show status of services
 
 =head1 SYNOPSIS
 
-B<aa-status> [B<-D>] [B<-r> I<repodir>] [B<-a>] [B<-L>] [I<service...>]
+B<aa-status> [B<-D>] [B<-r> I<repodir>] [B<-a>] [B<-L>] [B<-l> I<listdir>]
+[I<service...>]
 
 =head1 OPTIONS
 
@@ -26,6 +27,14 @@ shown on console and logged.
 
 Show help screen and exit.
 
+=item B<-l, --listdir> I<dir>
+
+Use I<dir> to list services to show status of. Only one can be set, if specified
+more than once the last one will be used.
+
+If I<dir> doesn't start with a slash or dot, it will be prefixed with
+I</etc/anopa/listdirs/>
+
 =item B<-L, --list>
 
 Show statuses as a list, with one service per line, elipsizing service name
diff --git a/src/anopa/aa-status.c b/src/anopa/aa-status.c
index 0dfda1b..23ef15c 100644
--- a/src/anopa/aa-status.c
+++ b/src/anopa/aa-status.c
@@ -49,6 +49,7 @@
 #include <anopa/service_status.h>
 #include <anopa/err.h>
 #include "util.h"
+#include "common.h"
 
 struct config
 {
@@ -562,12 +563,22 @@ it_all (direntry *d, void *data)
     return 0;
 }
 
+static int
+it_listdir (direntry *d, void *data)
+{
+    if (*d->d_name == '.')
+        return 0;
+    load_service (d->d_name, data);
+    return 0;
+}
+
 static void
 dieusage (int rc)
 {
     aa_die_usage (rc, "[OPTION...] [service...]",
             " -D, --double-output           Enable double-output mode\n"
             " -r, --repodir DIR             Use DIR as repository directory\n"
+            " -l, --listdir DIR             Use DIR to list services to get status of\n"
             " -a, --all                     Show status of all services\n"
             " -L, --list                    Show statuses as one-liners list\n"
             " -h, --help                    Show this help screen and exit\n"
@@ -580,6 +591,7 @@ main (int argc, char * const argv[])
 {
     PROG = "aa-status";
     const char *path_repo = "/run/services";
+    const char *path_list = NULL;
     int mode_both = 0;
     struct config cfg = { 0, };
     int all = 0;
@@ -592,6 +604,7 @@ main (int argc, char * const argv[])
             { "all",                no_argument,        NULL,   'a' },
             { "double-output",      no_argument,        NULL,   'D' },
             { "help",               no_argument,        NULL,   'h' },
+            { "listdir",            required_argument,  NULL,   'l' },
             { "list",               no_argument,        NULL,   'L' },
             { "repodir",            required_argument,  NULL,   'r' },
             { "version",            no_argument,        NULL,   'V' },
@@ -599,7 +612,7 @@ main (int argc, char * const argv[])
         };
         int c;
 
-        c = getopt_long (argc, argv, "aDhLr:V", longopts, NULL);
+        c = getopt_long (argc, argv, "aDhl:Lr:V", longopts, NULL);
         if (c == -1)
             break;
         switch (c)
@@ -615,6 +628,11 @@ main (int argc, char * const argv[])
             case 'h':
                 dieusage (0);
 
+            case 'l':
+                unslash (optarg);
+                path_list = optarg;
+                break;
+
             case 'L':
                 cfg.mode_list = 1;
                 break;
@@ -636,7 +654,7 @@ main (int argc, char * const argv[])
 
     aa_init_output (mode_both);
 
-    if (!all && argc < 1)
+    if (!all && !path_list && argc < 1)
         dieusage (1);
 
     r = aa_init_repo (path_repo, AA_REPO_READ);
@@ -667,6 +685,21 @@ main (int argc, char * const argv[])
         if (r < 0)
             strerr_diefu2sys (-r, "scan repo directory ", path_repo);
     }
+    else if (path_list)
+    {
+        stralloc sa = STRALLOC_ZERO;
+        int r;
+
+        if (*path_list != '/' && *path_list != '.')
+            stralloc_cats (&sa, LISTDIR_PREFIX);
+        stralloc_catb (&sa, path_list, strlen (path_list) + 1);
+        r = aa_scan_dir (&sa, 1, it_listdir, &cfg);
+        stralloc_free (&sa);
+        if (r < 0)
+            strerr_diefu3sys (-r, "read list directory ",
+                    (*path_list != '/' && *path_list != '.') ? LISTDIR_PREFIX : path_list,
+                    (*path_list != '/' && *path_list != '.') ? path_list : "");
+    }
     else
         for (i = 0; i < argc; ++i)
             load_service (argv[i], &cfg);