From e673fd5927d27bde6e931345bd95b1069d91b28c Mon Sep 17 00:00:00 2001 From: Will Woods Date: Mon, 30 Jul 2018 12:03:06 -0400 Subject: [PATCH] composer-cli: clean up "list" commands This patch does two things: 1) Add "compose list", which lists compose UUIDs and other basic info, 2) Fix up "blueprints list", "modules list", "sources list", and "compose types" so their output is just a plain list of identifiers --- src/composer/cli/blueprints.py | 3 ++- src/composer/cli/compose.py | 48 +++++++++++++++++++++++++++++++++- src/composer/cli/help.py | 3 +++ src/composer/cli/modules.py | 3 ++- src/composer/cli/sources.py | 3 ++- 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/composer/cli/blueprints.py b/src/composer/cli/blueprints.py index 4f2bbb06..d4fc33b7 100644 --- a/src/composer/cli/blueprints.py +++ b/src/composer/cli/blueprints.py @@ -77,7 +77,8 @@ def blueprints_list(socket_path, api_version, args, show_json=False): if exit_now: return rc - print("blueprints: " + ", ".join([r for r in result["blueprints"]])) + # "list" should output a plain list of identifiers, one per line. + print("\n".join(result["blueprints"])) return rc diff --git a/src/composer/cli/compose.py b/src/composer/cli/compose.py index b0c60541..327c07b1 100644 --- a/src/composer/cli/compose.py +++ b/src/composer/cli/compose.py @@ -36,6 +36,7 @@ def compose_cmd(opts): This dispatches the compose commands to a function """ cmd_map = { + "list": compose_list, "status": compose_status, "types": compose_types, "start": compose_start, @@ -57,6 +58,50 @@ def compose_cmd(opts): return cmd_map[opts.args[1]](opts.socket, opts.api_version, opts.args[2:], opts.json, opts.testmode) +def compose_list(socket_path, api_version, args, show_json=False, testmode=0): + """Return a simple list of compose identifiers""" + + states = ("running", "waiting", "finished", "failed") + + which = set() + + if any(a not in states for a in args): + # TODO: error about unknown state + return 1 + elif not args: + which.update(states) + else: + which.update(args) + + results = [] + + if "running" in which or "waiting" in which: + api_route = client.api_url(api_version, "/compose/queue") + r = client.get_url_json(socket_path, api_route) + if "running" in which: + results += r["run"] + if "waiting" in which: + results += r["new"] + + if "finished" in which: + api_route = client.api_url(api_version, "/compose/finished") + r = client.get_url_json(socket_path, api_route) + results += r["finished"] + + if "failed" in which: + api_route = client.api_url(api_version, "/compose/failed") + r = client.get_url_json(socket_path, api_route) + results += r["failed"] + + if results: + if show_json: + print(json.dumps(results, indent=4)) + else: + list_fmt = "{id} {queue_status} {blueprint} {version} {compose_type}" + print("\n".join(list_fmt.format(**c) for c in results)) + + return 0 + def compose_status(socket_path, api_version, args, show_json=False, testmode=0): """Return the status of all known composes @@ -148,7 +193,8 @@ def compose_types(socket_path, api_version, args, show_json=False, testmode=0): print(json.dumps(result, indent=4)) return 0 - print("Compose Types: " + ", ".join([t["name"] for t in result["types"]])) + # output a plain list of identifiers, one per line + print("\n".join(t["name"] for t in result["types"])) def compose_start(socket_path, api_version, args, show_json=False, testmode=0): """Start a new compose using the selected blueprint and type diff --git a/src/composer/cli/help.py b/src/composer/cli/help.py index 1f34dcdf..29362857 100644 --- a/src/composer/cli/help.py +++ b/src/composer/cli/help.py @@ -25,6 +25,9 @@ compose types compose status List the status of all running and finished composes. +compose list [waiting|running|finished|failed] + List basic information about composes. + compose log [] Show the last SIZE kB of the compose log. diff --git a/src/composer/cli/modules.py b/src/composer/cli/modules.py index 20c119cf..c69e9a98 100644 --- a/src/composer/cli/modules.py +++ b/src/composer/cli/modules.py @@ -42,6 +42,7 @@ def modules_cmd(opts): if exit_now: return rc - print("Modules:\n" + "\n".join([" "+r["name"] for r in result["modules"]])) + # "list" should output a plain list of identifiers, one per line. + print("\n".join(r["name"] for r in result["modules"])) return rc diff --git a/src/composer/cli/sources.py b/src/composer/cli/sources.py index c9dea42f..79e02e62 100644 --- a/src/composer/cli/sources.py +++ b/src/composer/cli/sources.py @@ -67,7 +67,8 @@ def sources_list(socket_path, api_version, args, show_json=False): if exit_now: return rc - print("Sources: %s" % ", ".join(result["sources"])) + # "list" should output a plain list of identifiers, one per line. + print("\n".join(result["sources"])) return rc def sources_info(socket_path, api_version, args, show_json=False):