Skip to content

Commit

Permalink
Move yaml output to Knack (#12603)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiasli authored Mar 16, 2020
1 parent 0ce8289 commit e7bc3ba
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 27 deletions.
26 changes: 0 additions & 26 deletions src/azure-cli-core/azure/cli/core/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,6 @@
class AzOutputProducer(knack.output.OutputProducer):
def __init__(self, cli_ctx=None):
super(AzOutputProducer, self).__init__(cli_ctx)
additional_formats = {
'yaml': self.format_yaml,
'yamlc': self.format_yaml_color,
'none': self.format_none
}
super(AzOutputProducer, self)._FORMAT_DICT.update(additional_formats)

@staticmethod
def format_yaml(obj):
from yaml import (safe_dump, representer)
import json

try:
return safe_dump(obj.result, default_flow_style=False)
except representer.RepresenterError:
# yaml.safe_dump fails when obj.result is an OrderedDict. knack's --query implementation converts the result to an OrderedDict. https://github.com/microsoft/knack/blob/af674bfea793ff42ae31a381a21478bae4b71d7f/knack/query.py#L46. # pylint: disable=line-too-long
return safe_dump(json.loads(json.dumps(obj.result)), default_flow_style=False)

@staticmethod
def format_yaml_color(obj):
from pygments import highlight, lexers, formatters
return highlight(AzOutputProducer.format_yaml(obj), lexers.YamlLexer(), formatters.TerminalFormatter()) # pylint: disable=no-member

@staticmethod
def format_none(_):
return ""

def check_valid_format_type(self, format_type):
return format_type in self._FORMAT_DICT
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_yaml_output_with_ordered_dict(self):
}

output_producer = AzOutputProducer(DummyCli())
yaml_output = output_producer.format_yaml(CommandResultItem(result=OrderedDict(account_dict)))
yaml_output = output_producer.get_formatter('yaml')(CommandResultItem(result=OrderedDict(account_dict)))
self.assertEqual(account_dict, yaml.safe_load(yaml_output))


Expand Down

0 comments on commit e7bc3ba

Please sign in to comment.