From: garciadeblas Date: Wed, 16 Oct 2024 08:24:49 +0000 (+0200) Subject: Multiple output formats in list and show commands for advanced cluster mgmt X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=399160135af441b0e13dd775baf7e1261284a11a;p=osm%2Fosmclient.git Multiple output formats in list and show commands for advanced cluster mgmt Change-Id: I2678f24006dc92fbf291d074c4c2a7f9396912ec Signed-off-by: garciadeblas --- diff --git a/osmclient/cli_commands/app_profile.py b/osmclient/cli_commands/app_profile.py index e85a19b..af5647f 100755 --- a/osmclient/cli_commands/app_profile.py +++ b/osmclient/cli_commands/app_profile.py @@ -17,6 +17,7 @@ import click from osmclient.cli_commands import common +from osmclient.common import print_output import logging logger = logging.getLogger("osmclient") @@ -60,11 +61,12 @@ def app_profile_delete(ctx, name, force): "--filter", help="restricts the list to the items matching the filter", ) +@print_output.output_option @click.pass_context -def app_profile_list(ctx, filter): +def app_profile_list(ctx, filter, output): """list all App Profiles""" logger.debug("") - common.generic_list(callback=ctx.obj.app_profile.list, filter=filter) + common.generic_list(callback=ctx.obj.app_profile.list, filter=filter, format=output) @click.command( @@ -72,14 +74,15 @@ def app_profile_list(ctx, filter): short_help="shows the details of an App Profile", ) @click.argument("name") +@print_output.output_option @click.pass_context -def app_profile_show(ctx, name): +def app_profile_show(ctx, name, output): """shows the details of an App Profile NAME: name or ID of the App Profile """ logger.debug("") - common.generic_show(callback=ctx.obj.app_profile.get, name=name) + common.generic_show(callback=ctx.obj.app_profile.get, name=name, format=output) @click.command(name="app-profile-update", short_help="updates an App Profile") diff --git a/osmclient/cli_commands/cluster.py b/osmclient/cli_commands/cluster.py index 46fb258..64dc7c6 100755 --- a/osmclient/cli_commands/cluster.py +++ b/osmclient/cli_commands/cluster.py @@ -15,6 +15,7 @@ import click from osmclient.cli_commands import common +from osmclient.common import print_output import logging import yaml @@ -96,12 +97,15 @@ def cluster_delete(ctx, name, force): "--filter", help="restricts the list to the items matching the filter", ) +@print_output.output_option @click.pass_context -def cluster_list(ctx, filter): +def cluster_list(ctx, filter, output): """list all K8s clusters""" logger.debug("") extras = {"Created": "created"} - common.generic_list(callback=ctx.obj.cluster.list, filter=filter, extras=extras) + common.generic_list( + callback=ctx.obj.cluster.list, filter=filter, format=output, extras=extras + ) @click.command( @@ -109,14 +113,15 @@ def cluster_list(ctx, filter): short_help="shows the details of a K8s cluster", ) @click.argument("name") +@print_output.output_option @click.pass_context -def cluster_show(ctx, name): +def cluster_show(ctx, name, output): """shows the details of a K8s cluster NAME: name or ID of the K8s cluster """ logger.debug("") - common.generic_show(callback=ctx.obj.cluster.get, name=name) + common.generic_show(callback=ctx.obj.cluster.get, name=name, format=output) @click.command(name="cluster-update", short_help="updates a K8s cluster") diff --git a/osmclient/cli_commands/common.py b/osmclient/cli_commands/common.py index 7ba9368..0a3d8c7 100644 --- a/osmclient/cli_commands/common.py +++ b/osmclient/cli_commands/common.py @@ -16,6 +16,7 @@ ####################################################################################### import logging import json +from osmclient.cli_commands import utils from osmclient.common import print_output from osmclient.common.exceptions import ClientException @@ -81,11 +82,18 @@ def generic_show(callback, name, format="table"): if format == "table" or format == "csv": if resp: for k, v in list(resp.items()): - rows.append([k, json.dumps(v, indent=2)]) + rows.append( + [ + utils.wrap_text(k, 30), + utils.wrap_text(text=json.dumps(v, indent=2)), + ] + ) print_output.print_output(format, headers, rows, resp) -def generic_list(callback, filter, format="table", extras={}): +def generic_list(callback, filter, format="table", extras=None): + if not extras: + extras = {} logger.debug("") if filter: filter = "&".join(filter) diff --git a/osmclient/cli_commands/infra_config_profile.py b/osmclient/cli_commands/infra_config_profile.py index 9229a6f..e9f592d 100755 --- a/osmclient/cli_commands/infra_config_profile.py +++ b/osmclient/cli_commands/infra_config_profile.py @@ -17,6 +17,7 @@ import click from osmclient.cli_commands import common +from osmclient.common import print_output import logging logger = logging.getLogger("osmclient") @@ -64,11 +65,14 @@ def infra_config_profile_delete(ctx, name, force): "--filter", help="restricts the list to the items matching the filter", ) +@print_output.output_option @click.pass_context -def infra_config_profile_list(ctx, filter): +def infra_config_profile_list(ctx, filter, output): """list all Infra Config Profiles""" logger.debug("") - common.generic_list(callback=ctx.obj.infra_config_profile.list, filter=filter) + common.generic_list( + callback=ctx.obj.infra_config_profile.list, filter=filter, format=output + ) @click.command( @@ -76,14 +80,17 @@ def infra_config_profile_list(ctx, filter): short_help="shows the details of an Infra Config Profile", ) @click.argument("name") +@print_output.output_option @click.pass_context -def infra_config_profile_show(ctx, name): +def infra_config_profile_show(ctx, name, output): """shows the details of an Infra Config Profile NAME: name or ID of the Infra Config Profile """ logger.debug("") - common.generic_show(callback=ctx.obj.infra_config_profile.get, name=name) + common.generic_show( + callback=ctx.obj.infra_config_profile.get, name=name, format=output + ) @click.command( diff --git a/osmclient/cli_commands/infra_controller_profile.py b/osmclient/cli_commands/infra_controller_profile.py index 6bab0e5..c764bae 100755 --- a/osmclient/cli_commands/infra_controller_profile.py +++ b/osmclient/cli_commands/infra_controller_profile.py @@ -17,6 +17,7 @@ import click from osmclient.cli_commands import common +from osmclient.common import print_output import logging logger = logging.getLogger("osmclient") @@ -66,11 +67,14 @@ def infra_controller_profile_delete(ctx, name, force): "--filter", help="restricts the list to the items matching the filter", ) +@print_output.output_option @click.pass_context -def infra_controller_profile_list(ctx, filter): +def infra_controller_profile_list(ctx, filter, output): """list all Infra Controller Profiles""" logger.debug("") - common.generic_list(callback=ctx.obj.infra_controller_profile.list, filter=filter) + common.generic_list( + callback=ctx.obj.infra_controller_profile.list, filter=filter, format=output + ) @click.command( @@ -79,13 +83,16 @@ def infra_controller_profile_list(ctx, filter): ) @click.argument("name") @click.pass_context -def infra_controller_profile_show(ctx, name): +@print_output.output_option +def infra_controller_profile_show(ctx, name, output): """shows the details of an Infra Controller Profile NAME: name or ID of the Infra Controller Profile """ logger.debug("") - common.generic_show(callback=ctx.obj.infra_controller_profile.get, name=name) + common.generic_show( + callback=ctx.obj.infra_controller_profile.get, name=name, format=output + ) @click.command( diff --git a/osmclient/cli_commands/ksu.py b/osmclient/cli_commands/ksu.py index 27f050d..7060e74 100755 --- a/osmclient/cli_commands/ksu.py +++ b/osmclient/cli_commands/ksu.py @@ -18,6 +18,7 @@ import click from osmclient.cli_commands import common from osmclient.common.exceptions import ClientException +from osmclient.common import print_output import logging import yaml @@ -282,8 +283,9 @@ def ksu_delete(ctx, ksus, force): "--filter", help="restricts the list to the items matching the filter", ) +@print_output.output_option @click.pass_context -def ksu_list(ctx, filter): +def ksu_list(ctx, filter, output): """list all Kubernetes SW Units (KSU)""" logger.debug("") common.generic_list(callback=ctx.obj.ksu.list, filter=filter) @@ -291,8 +293,9 @@ def ksu_list(ctx, filter): @click.command(name="ksu-show", short_help="shows the details of a KSU") @click.argument("name") +@print_output.output_option @click.pass_context -def ksu_show(ctx, name): +def ksu_show(ctx, name, output): """shows the details of a KSU NAME: name or ID of the KSU diff --git a/osmclient/cli_commands/oka.py b/osmclient/cli_commands/oka.py index dce5fa5..b6d4eb5 100755 --- a/osmclient/cli_commands/oka.py +++ b/osmclient/cli_commands/oka.py @@ -17,6 +17,7 @@ import click from osmclient.cli_commands import common +from osmclient.common import print_output import logging logger = logging.getLogger("osmclient") @@ -64,8 +65,9 @@ def oka_delete(ctx, name, force): "--filter", help="restricts the list to the items matching the filter", ) +@print_output.output_option @click.pass_context -def oka_list(ctx, filter): +def oka_list(ctx, filter, output): """list all OSM Kubernetes Application (OKA)""" logger.debug("") common.generic_list(callback=ctx.obj.oka.list, filter=filter) @@ -76,8 +78,9 @@ def oka_list(ctx, filter): short_help="shows the details of an OKA", ) @click.argument("name") +@print_output.output_option @click.pass_context -def oka_show(ctx, name): +def oka_show(ctx, name, output): """shows the details of an OKA NAME: name or ID of the OKA diff --git a/osmclient/cli_commands/resource_profile.py b/osmclient/cli_commands/resource_profile.py index 8efb959..d084afe 100755 --- a/osmclient/cli_commands/resource_profile.py +++ b/osmclient/cli_commands/resource_profile.py @@ -17,6 +17,7 @@ import click from osmclient.cli_commands import common +from osmclient.common import print_output import logging logger = logging.getLogger("osmclient") @@ -62,11 +63,14 @@ def resource_profile_delete(ctx, name, force): "--filter", help="restricts the list to the items matching the filter", ) +@print_output.output_option @click.pass_context -def resource_profile_list(ctx, filter): +def resource_profile_list(ctx, filter, output): """list all Resource Profiles""" logger.debug("") - common.generic_list(callback=ctx.obj.resource_profile.list, filter=filter) + common.generic_list( + callback=ctx.obj.resource_profile.list, filter=filter, format=output + ) @click.command( @@ -74,14 +78,15 @@ def resource_profile_list(ctx, filter): short_help="shows the details of an Resource Profile", ) @click.argument("name") +@print_output.output_option @click.pass_context -def resource_profile_show(ctx, name): +def resource_profile_show(ctx, name, output): """shows the details of an Resource Profile NAME: name or ID of the Resource Profile """ logger.debug("") - common.generic_show(callback=ctx.obj.resource_profile.get, name=name) + common.generic_show(callback=ctx.obj.resource_profile.get, name=name, format=output) @click.command(name="resource-profile-update", short_help="updates an Resource Profile") diff --git a/osmclient/cli_commands/utils.py b/osmclient/cli_commands/utils.py index 25fc21a..e34926a 100755 --- a/osmclient/cli_commands/utils.py +++ b/osmclient/cli_commands/utils.py @@ -15,13 +15,17 @@ import textwrap import logging +import shutil import yaml from osmclient.common.exceptions import ClientException logger = logging.getLogger("osmclient") -def wrap_text(text, width): +def wrap_text(text, width=0): + if not width: + terminal_width = shutil.get_terminal_size().columns + width = max(terminal_width - 34, 40) wrapper = textwrap.TextWrapper(width=width) lines = text.splitlines() return "\n".join(map(wrapper.fill, lines)) diff --git a/osmclient/common/print_output.py b/osmclient/common/print_output.py index 3d72205..9f1e011 100644 --- a/osmclient/common/print_output.py +++ b/osmclient/common/print_output.py @@ -44,7 +44,7 @@ output_option = click.option( default="table", is_eager=True, callback=evaluate_output_format, - help="output format (default: table)", + help="output format (table, json, yaml, csv, jsonpath=...) (default: table)", ) @@ -53,7 +53,7 @@ literal_option = click.option( ) -def print_output(format="table", headers=["field", "value"], rows=[], data=None): +def print_output(format="table", headers=["field", "value"], rows=None, data=None): """ Prints content in specified format. :param format: output format (table, yaml, json, csv, jsonpath=...). Default: @@ -64,6 +64,8 @@ def print_output(format="table", headers=["field", "value"], rows=[], data=None) """ logger.debug("") + if not rows: + rows = [] if format == "table": table = PrettyTable(headers) table.align = "l"