Commit f037e9d7 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

gnt-job list: Add options for commonly used filters



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: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 5ad68a23
......@@ -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()
......
......@@ -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)."
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment