diff --git a/awxkit/awxkit/cli/client.py b/awxkit/awxkit/cli/client.py index de68801af5..3a87a7113a 100755 --- a/awxkit/awxkit/cli/client.py +++ b/awxkit/awxkit/cli/client.py @@ -12,7 +12,7 @@ from .custom import handle_custom_actions from .format import (add_authentication_arguments, add_output_formatting_arguments, FORMATTERS, format_response) -from .options import ResourceOptionsParser +from .options import ResourceOptionsParser, UNIQUENESS_RULES from .resource import parse_resource, is_control_resource from awxkit import api, config, utils, exceptions, WSClient # noqa from awxkit.cli.utils import HelpfulArgumentParser, cprint, disable_color @@ -159,10 +159,34 @@ class CLI(object): response = getattr(resource, self.method)() else: response = self.parse_action(resource) + + _filter = self.get_config('filter') + + # human format for metrics, settings is special + if ( + self.resource in ('metrics', 'settings') and + self.get_config('format') == 'human' + ): + response.json = { + 'count': len(response.json), + 'results': [ + {'key': k, 'value': v} + for k, v in response.json.items() + ] + } + _filter = 'key, value' + + if ( + self.get_config('format') == 'human' and + _filter == '.' and + self.resource in UNIQUENESS_RULES + ): + _filter = UNIQUENESS_RULES[self.resource] + formatted = format_response( response, fmt=self.get_config('format'), - filter=self.get_config('filter'), + filter=_filter, changed=self.original_action in ( 'modify', 'create', 'associate', 'disassociate' ) diff --git a/awxkit/awxkit/cli/options.py b/awxkit/awxkit/cli/options.py index 1620921c74..aa52a2582c 100644 --- a/awxkit/awxkit/cli/options.py +++ b/awxkit/awxkit/cli/options.py @@ -12,6 +12,13 @@ from .format import add_output_formatting_arguments from .resource import DEPRECATED_RESOURCES_REVERSE +UNIQUENESS_RULES = { + 'me': 'id, username', + 'users': 'id, username', + 'instances': 'id, hostname', +} + + def pk_or_name(v2, model_name, value, page=None): if isinstance(value, int): return value @@ -26,10 +33,8 @@ def pk_or_name(v2, model_name, value, page=None): if model_name in DEPRECATED_RESOURCES_REVERSE: model_name = DEPRECATED_RESOURCES_REVERSE[model_name] - if model_name == 'users': - identity = 'username' - elif model_name == 'instances': - model_name = 'hostname' + if model_name in UNIQUENESS_RULES: + identity = UNIQUENESS_RULES[model_name][-1] if hasattr(v2, model_name): page = getattr(v2, model_name) diff --git a/awxkit/awxkit/cli/resource.py b/awxkit/awxkit/cli/resource.py index be4f7409fb..956774c6a8 100644 --- a/awxkit/awxkit/cli/resource.py +++ b/awxkit/awxkit/cli/resource.py @@ -167,10 +167,23 @@ def parse_resource(client, skip_deprecated=False): command = CustomCommand.registry[resource]() response = command.handle(client, parser) if response: + _filter = client.get_config('filter') + if ( + resource == 'config' and + client.get_config('format') == 'human' + ): + response = { + 'count': len(response), + 'results': [ + {'key': k, 'value': v} + for k, v in response.items() + ] + } + _filter = 'key, value' formatted = format_response( Page.from_json(response), fmt=client.get_config('format'), - filter=client.get_config('filter'), + filter=_filter ) print(formatted) raise SystemExit()