Skip to content

Commit 1639a38

Browse files
committed
add file as content
1 parent 6b05a20 commit 1639a38

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/azure-cli-core/azure/cli/core/aaz/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
AAZFreeFormDictArg, AAZFloatArg, AAZBaseArg, AAZBoolArg, AAZListArg, AAZResourceGroupNameArg, \
1414
AAZResourceLocationArg, AAZResourceIdArg, AAZSubscriptionIdArg, AAZUuidArg, AAZDateArg, AAZTimeArg, \
1515
AAZDateTimeArg, AAZDurationArg, AAZFileArg, AAZPasswordArg, AAZPaginationTokenArg, AAZPaginationLimitArg, \
16-
AAZAnyTypeArg
16+
AAZAnyTypeArg, AAZFileBytesArg
1717
from ._arg_fmt import AAZStrArgFormat, AAZIntArgFormat, AAZFloatArgFormat, AAZBoolArgFormat, AAZObjectArgFormat, \
1818
AAZDictArgFormat, AAZFreeFormDictArgFormat, AAZListArgFormat, AAZResourceLocationArgFormat, \
1919
AAZResourceIdArgFormat, AAZSubscriptionIdArgFormat, AAZUuidFormat, AAZDateFormat, AAZTimeFormat, \

src/azure-cli-core/azure/cli/core/aaz/_arg.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from ._arg_fmt import AAZObjectArgFormat, AAZListArgFormat, AAZDictArgFormat, AAZFreeFormDictArgFormat, \
2323
AAZSubscriptionIdArgFormat, AAZResourceLocationArgFormat, AAZResourceIdArgFormat, AAZUuidFormat, AAZDateFormat, \
2424
AAZTimeFormat, AAZDateTimeFormat, AAZDurationFormat, AAZFileArgTextFormat, AAZPaginationTokenArgFormat, \
25-
AAZIntArgFormat
25+
AAZIntArgFormat, AAZFileBytesArgFormat
2626
from .exceptions import AAZUnregisteredArg
2727
from ._prompt import AAZPromptInput
2828

@@ -599,6 +599,18 @@ def __init__(self, fmt=None, **kwargs):
599599
super().__init__(fmt=fmt, **kwargs)
600600

601601

602+
class AAZFileBytesArg(AAZStrArg):
603+
"""Argument that accepts a file path and returns both file content in bytes"""
604+
605+
def __init__(self, fmt=None, **kwargs):
606+
fmt = fmt or AAZFileBytesArgFormat()
607+
super().__init__(fmt=fmt, **kwargs)
608+
609+
@property
610+
def _type_in_help(self):
611+
return "File Bytes"
612+
613+
602614
# Generic Update arguments
603615
class AAZGenericUpdateForceStringArg(AAZBoolArg):
604616

src/azure-cli-core/azure/cli/core/aaz/_arg_fmt.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,28 @@ def read_file(self, file_path):
748748
return data
749749

750750

751+
class AAZFileBytesArgFormat(AAZBaseArgFormat):
752+
"""Format for file info argument that returns both filename and content"""
753+
754+
def __call__(self, ctx, value):
755+
assert isinstance(value, AAZSimpleValue)
756+
data = value._data
757+
if data == AAZUndefined or data is None or value._is_patch:
758+
return value
759+
760+
assert isinstance(data, str)
761+
762+
if not os.path.isfile(data):
763+
raise AAZInvalidArgValueError(f"File '{data}' doesn't exist")
764+
765+
try:
766+
with open(data, 'rb') as f:
767+
value._data = f.read()
768+
return value
769+
except Exception as e:
770+
raise AAZInvalidArgValueError(f"Failed to read file '{data}': {str(e)}")
771+
772+
751773
class AAZPaginationTokenArgFormat(AAZBaseArgFormat):
752774
def __call__(self, ctx, value):
753775
def validate_json(s):

0 commit comments

Comments
 (0)