Skip to content

Commit

Permalink
[Core] aaz: Support yaml file as value for compound arguments (#2…
Browse files Browse the repository at this point in the history
…3817)

* support yaml file as value for compound type argument

* optimize help message

* fix style issue
  • Loading branch information
kairu-ms authored Sep 13, 2022
1 parent 6b230f9 commit 8357f17
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/azure-cli-core/azure/cli/core/aaz/_arg_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/aaz/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
}
Expand Down
13 changes: 13 additions & 0 deletions src/azure-cli-core/azure/cli/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import binascii
import getpass
import json
import yaml
import logging
import os
import platform
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 8357f17

Please sign in to comment.