Skip to content

Commit 2fb12fb

Browse files
authored
[Keyvault] az keyvault secret download: Add --overwrite flag (#31659)
1 parent 3abde36 commit 2fb12fb

File tree

4 files changed

+525
-229
lines changed

4 files changed

+525
-229
lines changed

src/azure-cli/azure/cli/command_modules/keyvault/_params.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ class CLISecurityDomainOperation(str, Enum):
558558
c.argument('encoding', arg_type=get_enum_type(secret_encoding_values), options_list=['--encoding', '-e'],
559559
help="Encoding of the secret. By default, will look for the 'file-encoding' tag on the secret. "
560560
"Otherwise will assume 'utf-8'.", default=None)
561+
c.argument('overwrite', action='store_true', options_list=['--overwrite'], help="Overwrite the file if it exists.")
561562

562563
for scope in ['download', 'backup', 'restore']:
563564
with self.argument_context('keyvault secret {}'.format(scope)) as c:

src/azure-cli/azure/cli/command_modules/keyvault/custom.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,10 +1460,13 @@ def update_key_rotation_policy(cmd, client, value, key_name=None):
14601460

14611461

14621462
# region KeyVault Secret
1463-
def download_secret(client, file_path, name=None, encoding=None, version=''): # pylint: disable=unused-argument
1463+
def download_secret(client, file_path, name=None, encoding=None, version='', overwrite=False): # pylint: disable=unused-argument
14641464
""" Download a secret from a KeyVault. """
1465-
if os.path.isfile(file_path) or os.path.isdir(file_path):
1466-
raise CLIError("File or directory named '{}' already exists.".format(file_path))
1465+
if os.path.isdir(file_path):
1466+
raise CLIError("Cannot write to '{}': it is a directory.".format(file_path))
1467+
1468+
if not overwrite and os.path.isfile(file_path):
1469+
raise CLIError("File '{}' already exists. Use --overwrite to overwrite the existing file.".format(file_path))
14671470

14681471
secret = client.get_secret(name, version)
14691472

0 commit comments

Comments
 (0)