From 80b5a3703672f2f7dc9d9ba4339349f2ca164337 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 | 47 +++++++++++++++++++++++++++++++++- src/composer/cli/help.py | 1 + src/composer/cli/modules.py | 3 ++- src/composer/cli/sources.py | 3 ++- 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/composer/cli/blueprints.py b/src/composer/cli/blueprints.py index 01d05cca..f1dc3e8d 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 dc57b3c2..94da5226 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,7 @@ 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"]])) + 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 f0b66bd1..001e4644 100644 --- a/src/composer/cli/help.py +++ b/src/composer/cli/help.py @@ -18,6 +18,7 @@ compose_help = """ compose start Start a compose using the selected blueprint and output type. types List the supported output types. status List the status of all running and finished composes. + list [WHICH] List (waiting, running, finished, failed) composes. log [kB] Show the last 1kB of the compose log. cancel Cancel a running compose and delete any intermediate results. delete Delete the listed compose results. 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):