From f037e9d71e0c2b2fcc4b0fe5194228acddd05f5b Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Fri, 13 Apr 2012 00:08:20 +0200
Subject: [PATCH] gnt-job list: Add options for commonly used filters
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

While β€œgnt-job list” would also accept filters on the command line (e.g.
β€œ'status == "error"'”, having shortcuts in the form of options comes in
handy.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/cli.py            | 11 +++++++++--
 lib/client/gnt_job.py | 40 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/lib/cli.py b/lib/cli.py
index 256562ef3..f18e65bf4 100644
--- a/lib/cli.py
+++ b/lib/cli.py
@@ -2800,7 +2800,7 @@ def _WarnUnknownFields(fdefs):
 
 def GenericList(resource, fields, names, unit, separator, header, cl=None,
                 format_override=None, verbose=False, force_filter=False,
-                namefield=None):
+                namefield=None, qfilter=None):
   """Generic implementation for listing all items of a resource.
 
   @param resource: One of L{constants.QR_VIA_LUXI}
@@ -2826,12 +2826,19 @@ def GenericList(resource, fields, names, unit, separator, header, cl=None,
   @type namefield: string
   @param namefield: Name of field to use for simple filters (see
     L{qlang.MakeFilter} for details)
+  @type qfilter: list or None
+  @param qfilter: Query filter (in addition to names)
 
   """
   if not names:
     names = None
 
-  qfilter = qlang.MakeFilter(names, force_filter, namefield=namefield)
+  namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield)
+
+  if qfilter is None:
+    qfilter = namefilter
+  elif namefilter is not None:
+    qfilter = [qlang.OP_AND, namefilter, qfilter]
 
   if cl is None:
     cl = GetClient()
diff --git a/lib/client/gnt_job.py b/lib/client/gnt_job.py
index 5641a0546..7d12ab369 100644
--- a/lib/client/gnt_job.py
+++ b/lib/client/gnt_job.py
@@ -79,10 +79,13 @@ def ListJobs(opts, args):
   fmtoverride.update(dict.fromkeys(["opstart", "opexec", "opend"],
     (lambda value: map(FormatTimestamp, value), None)))
 
+  qfilter = qlang.MakeSimpleFilter("status", opts.status_filter)
+
   return GenericList(constants.QR_JOB, selected_fields, args, None,
                      opts.separator, not opts.no_headers,
                      format_override=fmtoverride, verbose=opts.verbose,
-                     force_filter=opts.force_filter, namefield="id")
+                     force_filter=opts.force_filter, namefield="id",
+                     qfilter=qfilter)
 
 
 def ListJobFields(opts, args):
@@ -337,10 +340,43 @@ def WatchJob(opts, args):
   return retcode
 
 
+_PENDING_OPT = \
+  cli_option("--pending", default=None,
+             action="store_const", dest="status_filter",
+             const=frozenset([
+               constants.JOB_STATUS_QUEUED,
+               constants.JOB_STATUS_WAITING,
+               ]),
+             help="Show only jobs pending execution")
+
+_RUNNING_OPT = \
+  cli_option("--running", default=None,
+             action="store_const", dest="status_filter",
+             const=frozenset([
+               constants.JOB_STATUS_RUNNING,
+               ]),
+             help="Show jobs currently running only")
+
+_ERROR_OPT = \
+  cli_option("--error", default=None,
+             action="store_const", dest="status_filter",
+             const=frozenset([
+               constants.JOB_STATUS_ERROR,
+               ]),
+             help="Show failed jobs only")
+
+_FINISHED_OPT = \
+  cli_option("--finished", default=None,
+             action="store_const", dest="status_filter",
+             const=constants.JOBS_FINALIZED,
+             help="Show finished jobs only")
+
+
 commands = {
   "list": (
     ListJobs, [ArgJobId()],
-    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT],
+    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT,
+     _PENDING_OPT, _RUNNING_OPT, _ERROR_OPT, _FINISHED_OPT],
     "[job_id ...]",
     "Lists the jobs and their status. The available fields can be shown"
     " using the \"list-fields\" command (see the man page for details)."
-- 
GitLab