Update projects.py to use new handle_api_result

Use the new function to properly handle error responses for all the
commands.

(cherry picked from commit 77767cd93a)
This commit is contained in:
Brian C. Lane 2018-07-26 10:33:32 -07:00
parent 6614f65198
commit 14542df02c

View File

@ -17,11 +17,11 @@
import logging import logging
log = logging.getLogger("composer-cli") log = logging.getLogger("composer-cli")
import json
import textwrap import textwrap
from composer import http_client as client from composer import http_client as client
from composer.cli.help import projects_help from composer.cli.help import projects_help
from composer.cli.utilities import handle_api_result
def projects_cmd(opts): def projects_cmd(opts):
"""Process projects commands """Process projects commands
@ -60,16 +60,16 @@ def projects_list(socket_path, api_version, args, show_json=False):
""" """
api_route = client.api_url(api_version, "/projects/list") api_route = client.api_url(api_version, "/projects/list")
result = client.get_url_json(socket_path, api_route) result = client.get_url_json(socket_path, api_route)
if show_json: (rc, exit_now) = handle_api_result(result, show_json)
print(json.dumps(result, indent=4)) if exit_now:
return 0 return rc
for proj in result["projects"]: for proj in result["projects"]:
for k in [field for field in ("name", "summary", "homepage", "description") if proj[field]]: for k in [field for field in ("name", "summary", "homepage", "description") if proj[field]]:
print("%s: %s" % (k.title(), textwrap.fill(proj[k], subsequent_indent=" " * (len(k)+2)))) print("%s: %s" % (k.title(), textwrap.fill(proj[k], subsequent_indent=" " * (len(k)+2))))
print("\n\n") print("\n\n")
return 0 return rc
def projects_info(socket_path, api_version, args, show_json=False): def projects_info(socket_path, api_version, args, show_json=False):
"""Output info on a list of projects """Output info on a list of projects
@ -91,9 +91,9 @@ def projects_info(socket_path, api_version, args, show_json=False):
api_route = client.api_url(api_version, "/projects/info/%s" % ",".join(args)) api_route = client.api_url(api_version, "/projects/info/%s" % ",".join(args))
result = client.get_url_json(socket_path, api_route) result = client.get_url_json(socket_path, api_route)
if show_json: (rc, exit_now) = handle_api_result(result, show_json)
print(json.dumps(result, indent=4)) if exit_now:
return 0 return rc
for proj in result["projects"]: for proj in result["projects"]:
for k in [field for field in ("name", "summary", "homepage", "description") if proj[field]]: for k in [field for field in ("name", "summary", "homepage", "description") if proj[field]]:
@ -107,4 +107,4 @@ def projects_info(socket_path, api_version, args, show_json=False):
build["build_time"], build["build_time"],
build["changelog"])) build["changelog"]))
print("") print("")
return 0 return rc