From 8357f17bdc9abb50e3f97a465943ea8301c925f7 Mon Sep 17 00:00:00 2001 From: kai ru <69238381+kairu-ms@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:06:52 +0800 Subject: [PATCH] [Core] `aaz`: Support `yaml` file as value for compound arguments (#23817) * support yaml file as value for compound type argument * optimize help message * fix style issue --- .../azure/cli/core/aaz/_arg_action.py | 9 +++++++-- src/azure-cli-core/azure/cli/core/aaz/_help.py | 2 +- src/azure-cli-core/azure/cli/core/util.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/aaz/_arg_action.py b/src/azure-cli-core/azure/cli/core/aaz/_arg_action.py index d7e01fe992b..8ef2f017567 100644 --- a/src/azure-cli-core/azure/cli/core/aaz/_arg_action.py +++ b/src/azure-cli-core/azure/cli/core/aaz/_arg_action.py @@ -167,7 +167,7 @@ def decode_values(cls, values): @classmethod def _decode_value(cls, key, key_items, value): # pylint: disable=unused-argument from ._arg import AAZSimpleTypeArg - from azure.cli.core.util import get_file_json, shell_safe_json_parse + from azure.cli.core.util import get_file_json, shell_safe_json_parse, get_file_yaml schema = cls._schema for item in key_items: @@ -186,7 +186,12 @@ def _decode_value(cls, key, key_items, value): # pylint: disable=unused-argumen # read from file path = os.path.expanduser(value) if os.path.exists(path): - v = get_file_json(path, preserve_order=True) + if path.endswith('.yml') or path.endswith('.yaml'): + # read from yaml file + v = get_file_yaml(path) + else: + # read from json file + v = get_file_json(path, preserve_order=True) else: try: v = cls._str_parser(value) diff --git a/src/azure-cli-core/azure/cli/core/aaz/_help.py b/src/azure-cli-core/azure/cli/core/aaz/_help.py index 3509fbaa019..1d6758a3bdd 100644 --- a/src/azure-cli-core/azure/cli/core/aaz/_help.py +++ b/src/azure-cli-core/azure/cli/core/aaz/_help.py @@ -21,7 +21,7 @@ shorthand_help_messages = { "show-help": 'Try `??` to show more.', - "short-summary": 'Shorthand syntax supported.', + "short-summary": 'Support shorthand-syntax, json-file and yaml-file.', "long-summary": 'See https://github.com/Azure/azure-cli/tree/dev/doc/shorthand_syntax.md ' 'for more about shorthand syntax.' } diff --git a/src/azure-cli-core/azure/cli/core/util.py b/src/azure-cli-core/azure/cli/core/util.py index 50560bb258b..01cf1e768d8 100644 --- a/src/azure-cli-core/azure/cli/core/util.py +++ b/src/azure-cli-core/azure/cli/core/util.py @@ -8,6 +8,7 @@ import binascii import getpass import json +import yaml import logging import os import platform @@ -535,6 +536,18 @@ def get_file_json(file_path, throw_on_empty=True, preserve_order=False): raise CLIError("Failed to parse file '{}' with exception:\n{}".format(file_path, ex)) +def get_file_yaml(file_path, throw_on_empty=True): + content = read_file_content(file_path) + if not content: + if throw_on_empty: + raise CLIError("Failed to parse file '{}' with exception:\nNo content in the file.".format(file_path)) + return None + try: + return yaml.safe_load(content) + except yaml.parser.ParserError as ex: + raise CLIError("Failed to parse file '{}' with exception:\n{}".format(file_path, ex)) from ex + + def read_file_content(file_path, allow_binary=False): from codecs import open as codecs_open # Note, always put 'utf-8-sig' first, so that BOM in WinOS won't cause trouble.