Skip to content

Commit

Permalink
[Config] Rename local-context to config param-persist (#15068)
Browse files Browse the repository at this point in the history
* [Config] Rename local-context to config param-persist
  • Loading branch information
arrownj authored Sep 14, 2020
1 parent cde74c5 commit 2c52cbc
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 9 deletions.
10 changes: 10 additions & 0 deletions linter_exclusions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,16 @@ config unset:
key:
rule_exclusions:
- no_positional_parameters
config param-persist show:
parameters:
name:
rule_exclusions:
- no_positional_parameters
config param-persist delete:
parameters:
name:
rule_exclusions:
- no_positional_parameters
consumption budget create:
parameters:
resource_groups:
Expand Down
12 changes: 6 additions & 6 deletions src/azure-cli-core/azure/cli/core/local_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def initialize(self):
self.current_dir = os.getcwd()
except FileNotFoundError:
if self.is_on:
logger.warning('The working directory has been deleted or recreated. Local context is ignored.')
logger.warning('The working directory has been deleted or recreated. Parameter persistence is ignored.')

if self.is_on:
self._local_context_file = self._get_local_context_file()
Expand Down Expand Up @@ -133,17 +133,17 @@ def delete_file(self, recursive=False):
parent_dir = os.path.dirname(local_context_file.config_path)
if not os.listdir(parent_dir):
shutil.rmtree(parent_dir)
logger.warning('Local context persistence file in working directory %s is deleted.',
logger.warning('Parameter persistence file in working directory %s is deleted.',
os.path.dirname(local_context_file.config_dir))
except Exception: # pylint: disable=broad-except
logger.warning('Fail to delete local context persistence file in working directory %s',
logger.warning('Fail to delete parameter persistence file in working directory %s',
os.path.dirname(local_context_file.config_dir))

def clear(self, recursive=False):
local_context_files = self._load_local_context_files(recursive=recursive)
for local_context_file in local_context_files:
local_context_file.clear()
logger.warning('Local context information in working directory %s is cleared.',
logger.warning('Parameter persistence information in working directory %s is cleared.',
os.path.dirname(local_context_file.config_dir))

def delete(self, names=None):
Expand All @@ -152,8 +152,8 @@ def delete(self, names=None):
for scope in local_context_file.sections():
for name in names:
local_context_file.remove_option(scope, name)
logger.warning('Local context value is deleted. You can run `az local-context show` to show all available '
'values.')
logger.warning('Parameter persistence value is deleted. You can run `az config param-persist show` to show all '
'available values.')

def get_value(self, names=None):
result = {}
Expand Down
39 changes: 39 additions & 0 deletions src/azure-cli/azure/cli/command_modules/config/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,42 @@
- name: Unset the configuration of key `core.no_color`.
text: az config unset core.no_color
"""

helps['config param-persist'] = """
type: group
short-summary: Manage parameter persistence.
"""

helps['config param-persist on'] = """
type: command
short-summary: Turn on parameter persistence.
"""

helps['config param-persist off'] = """
type: command
short-summary: Turn off parameter persistence.
"""

helps['config param-persist show'] = """
type: command
short-summary: Show parameter persistence data.
examples:
- name: Show all parameter persistence value
text: az config param-persist show
- name: Show resource_group_name parameter persistence value
text: az config param-persist show resource_group_name
"""

helps['config param-persist delete'] = """
type: command
short-summary: Delete parameter persistence data.
examples:
- name: Delete resource_group_name from parameter persistence
text: az config param-persist delete resource_group_name
- name: Clear all parameter persistence data
text: az config param-persist delete --all
- name: Delete parameter persistence file
text: az config param-persist delete --all --purge
- name: Delete parameter persistence file recursively
text: az config param-persist delete --all --purge --recursive
"""
10 changes: 10 additions & 0 deletions src/azure-cli/azure/cli/command_modules/config/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ def load_arguments(self, _):
c.argument('local', action='store_true',
help='Include local configuration. Scan from the working directory up to the root drive, then the global configuration '
'and unset the first occurrence.')

with self.argument_context('config param-persist show') as c:
c.positional('name', nargs='*', help='Space-separated list of parameter persistence names.')

with self.argument_context('config param-persist delete') as c:
c.positional('name', nargs='*', help='Space-separated list of parameter persistence names. Either positional name argument or --all can be specified.')
c.argument('all', help='Clear all parameter persistence data. Either positional name argument or --all can be specified.', action='store_true')
c.argument('yes', options_list=['--yes', '-y'], help='Do not prompt for confirmation. Only available when --all is specified.', action='store_true')
c.argument('purge', help='Delete parameter persistence file from working directory. Only available when --all is specified.', action='store_true')
c.argument('recursive', help='Indicate this is recursive delete of parameter persistence. Only available when --all is specified.', action='store_true')
21 changes: 21 additions & 0 deletions src/azure-cli/azure/cli/command_modules/config/_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from knack.util import CLIError


def validate_param_persist(cmd, namespace): # pylint: disable=unused-argument
if not cmd.cli_ctx.local_context.username:
raise CLIError('Can\'t get system user account. Parameter persist is ignored.')
if not cmd.cli_ctx.local_context.current_dir:
raise CLIError('The working directory has been deleted or recreated. You can change to another working '
'directory or reenter current one if it is recreated.')


def validate_param_persist_for_delete(cmd, namespace):
if (namespace.all and namespace.name) or (not namespace.all and not namespace.name):
raise CLIError('Please specify either positional argument name or --all.')

validate_param_persist(cmd, namespace)
8 changes: 7 additions & 1 deletion src/azure-cli/azure/cli/command_modules/config/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import CliCommandType
from azure.cli.command_modules.config._validators import validate_param_persist, validate_param_persist_for_delete


def load_command_table(self, _):

config_custom = CliCommandType(operations_tmpl='azure.cli.command_modules.config.custom#{}')

with self.command_group('config', config_custom, is_experimental=True) as g:
g.command('set', 'config_set')
g.command('get', 'config_get')
g.command('unset', 'config_unset')

with self.command_group('config param-persist', config_custom, is_experimental=True) as g:
g.command('on', 'turn_param_persist_on')
g.command('off', 'turn_param_persist_off')
g.show_command('show', 'show_param_persist', validator=validate_param_persist)
g.command('delete', 'delete_param_persist', validator=validate_param_persist_for_delete)
38 changes: 38 additions & 0 deletions src/azure-cli/azure/cli/command_modules/config/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,41 @@ def config_unset(cmd, key=None, local=False):

with ScopedConfig(cmd.cli_ctx.config, local):
cmd.cli_ctx.config.remove_option(section, name)


def turn_param_persist_on(cmd):
if not cmd.cli_ctx.local_context.is_on:
cmd.cli_ctx.local_context.turn_on()
logger.warning('Parameter persistence is turned on, you can run `az config param-persist off` to turn it off.')
else:
raise CLIError('Parameter persistence is on already.')


def turn_param_persist_off(cmd):
if cmd.cli_ctx.local_context.is_on:
cmd.cli_ctx.local_context.turn_off()
logger.warning('Parameter persistence is turned off, you can run `az config param-persist on` to turn it on.')
else:
raise CLIError('Parameter persistence is off already.')


def show_param_persist(cmd, name=None):
if not name:
name = None
return cmd.cli_ctx.local_context.get_value(name)


def delete_param_persist(cmd, name=None, all=False, yes=False, purge=False, recursive=False): # pylint: disable=redefined-builtin
if name:
cmd.cli_ctx.local_context.delete(name)

if all:
from azure.cli.core.util import user_confirmation
if purge:
user_confirmation('You are going to delete parameter persistence file. '
'Are you sure you want to continue this operation ?', yes)
cmd.cli_ctx.local_context.delete_file(recursive)
else:
user_confirmation('You are going to clear all parameter persistence values. '
'Are you sure you want to continue this operation ?', yes)
cmd.cli_ctx.local_context.clear(recursive)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import unittest

from azure.cli.testsdk import LocalContextScenarioTest


class ParamPersistScenarioTest(LocalContextScenarioTest):

def test_param_persist_commands(self):
self.cmd('config param-persist show')
self.cmd('config param-persist show resource_group_name vnet_name')
self.cmd('config param-persist delete resource_group_name vnet_name')
self.cmd('config param-persist delete --all -y')
self.cmd('config param-persist delete --all --purge -y')
self.cmd('config param-persist delete --all --purge -y --recursive')

from knack.util import CLIError
with self.assertRaises(CLIError):
self.cmd('config param-persist delete resource_group_name --all')


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def load_arguments(self, _):
c.argument('all', help='Clear all local context data. Either --name or --all can be specified.', action='store_true')
c.argument('yes', options_list=['--yes', '-y'], help='Do not prompt for confirmation. Only available when --all is specified.', action='store_true')
c.argument('purge', help='Delete local context file from working directory. Only available when --all is specified.', action='store_true')
c.argument('recursive', help='Indicates this is recursive delete of local context. Only available when --all is specified.', action='store_true')
c.argument('recursive', help='Indicate this is recursive delete of local context. Only available when --all is specified.', action='store_true')
11 changes: 10 additions & 1 deletion src/azure-cli/azure/cli/command_modules/configure/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ def load_command_table(self, _):
g.command('delete', 'delete_cache_contents')
g.command('purge', 'purge_cache_contents')

with self.command_group('local-context', configure_custom, is_experimental=True) as g:
def _local_context_deprecate_message(self):
msg = "This {} has been deprecated and will be removed in future release.".format(self.object_type)
msg += " Use '{}' instead.".format(self.redirect)
# msg += " For more information go to"
# msg += " <Add param persist doc link here when it is ready.>"
return msg

with self.command_group('local-context', configure_custom, is_experimental=True,
deprecate_info=self.deprecate(redirect="config param-persist",
message_func=_local_context_deprecate_message)) as g:
g.command('on', 'turn_local_context_on')
g.command('off', 'turn_local_context_off')
g.command('show', 'show_local_context', validator=validate_local_context) # pylint: disable=show-command
Expand Down

0 comments on commit 2c52cbc

Please sign in to comment.