Skip to content

Commit 2c52cbc

Browse files
authored
[Config] Rename local-context to config param-persist (Azure#15068)
* [Config] Rename local-context to config param-persist
1 parent cde74c5 commit 2c52cbc

File tree

10 files changed

+168
-9
lines changed

10 files changed

+168
-9
lines changed

linter_exclusions.yml

+10
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,16 @@ config unset:
11881188
key:
11891189
rule_exclusions:
11901190
- no_positional_parameters
1191+
config param-persist show:
1192+
parameters:
1193+
name:
1194+
rule_exclusions:
1195+
- no_positional_parameters
1196+
config param-persist delete:
1197+
parameters:
1198+
name:
1199+
rule_exclusions:
1200+
- no_positional_parameters
11911201
consumption budget create:
11921202
parameters:
11931203
resource_groups:

src/azure-cli-core/azure/cli/core/local_context.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def initialize(self):
5757
self.current_dir = os.getcwd()
5858
except FileNotFoundError:
5959
if self.is_on:
60-
logger.warning('The working directory has been deleted or recreated. Local context is ignored.')
60+
logger.warning('The working directory has been deleted or recreated. Parameter persistence is ignored.')
6161

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

142142
def clear(self, recursive=False):
143143
local_context_files = self._load_local_context_files(recursive=recursive)
144144
for local_context_file in local_context_files:
145145
local_context_file.clear()
146-
logger.warning('Local context information in working directory %s is cleared.',
146+
logger.warning('Parameter persistence information in working directory %s is cleared.',
147147
os.path.dirname(local_context_file.config_dir))
148148

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

158158
def get_value(self, names=None):
159159
result = {}

src/azure-cli/azure/cli/command_modules/config/_help.py

+39
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,42 @@
5353
- name: Unset the configuration of key `core.no_color`.
5454
text: az config unset core.no_color
5555
"""
56+
57+
helps['config param-persist'] = """
58+
type: group
59+
short-summary: Manage parameter persistence.
60+
"""
61+
62+
helps['config param-persist on'] = """
63+
type: command
64+
short-summary: Turn on parameter persistence.
65+
"""
66+
67+
helps['config param-persist off'] = """
68+
type: command
69+
short-summary: Turn off parameter persistence.
70+
"""
71+
72+
helps['config param-persist show'] = """
73+
type: command
74+
short-summary: Show parameter persistence data.
75+
examples:
76+
- name: Show all parameter persistence value
77+
text: az config param-persist show
78+
- name: Show resource_group_name parameter persistence value
79+
text: az config param-persist show resource_group_name
80+
"""
81+
82+
helps['config param-persist delete'] = """
83+
type: command
84+
short-summary: Delete parameter persistence data.
85+
examples:
86+
- name: Delete resource_group_name from parameter persistence
87+
text: az config param-persist delete resource_group_name
88+
- name: Clear all parameter persistence data
89+
text: az config param-persist delete --all
90+
- name: Delete parameter persistence file
91+
text: az config param-persist delete --all --purge
92+
- name: Delete parameter persistence file recursively
93+
text: az config param-persist delete --all --purge --recursive
94+
"""

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

+10
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@ def load_arguments(self, _):
2828
c.argument('local', action='store_true',
2929
help='Include local configuration. Scan from the working directory up to the root drive, then the global configuration '
3030
'and unset the first occurrence.')
31+
32+
with self.argument_context('config param-persist show') as c:
33+
c.positional('name', nargs='*', help='Space-separated list of parameter persistence names.')
34+
35+
with self.argument_context('config param-persist delete') as c:
36+
c.positional('name', nargs='*', help='Space-separated list of parameter persistence names. Either positional name argument or --all can be specified.')
37+
c.argument('all', help='Clear all parameter persistence data. Either positional name argument or --all can be specified.', action='store_true')
38+
c.argument('yes', options_list=['--yes', '-y'], help='Do not prompt for confirmation. Only available when --all is specified.', action='store_true')
39+
c.argument('purge', help='Delete parameter persistence file from working directory. Only available when --all is specified.', action='store_true')
40+
c.argument('recursive', help='Indicate this is recursive delete of parameter persistence. Only available when --all is specified.', action='store_true')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
6+
from knack.util import CLIError
7+
8+
9+
def validate_param_persist(cmd, namespace): # pylint: disable=unused-argument
10+
if not cmd.cli_ctx.local_context.username:
11+
raise CLIError('Can\'t get system user account. Parameter persist is ignored.')
12+
if not cmd.cli_ctx.local_context.current_dir:
13+
raise CLIError('The working directory has been deleted or recreated. You can change to another working '
14+
'directory or reenter current one if it is recreated.')
15+
16+
17+
def validate_param_persist_for_delete(cmd, namespace):
18+
if (namespace.all and namespace.name) or (not namespace.all and not namespace.name):
19+
raise CLIError('Please specify either positional argument name or --all.')
20+
21+
validate_param_persist(cmd, namespace)

src/azure-cli/azure/cli/command_modules/config/commands.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
# --------------------------------------------------------------------------------------------
55

66
from azure.cli.core.commands import CliCommandType
7+
from azure.cli.command_modules.config._validators import validate_param_persist, validate_param_persist_for_delete
78

89

910
def load_command_table(self, _):
10-
1111
config_custom = CliCommandType(operations_tmpl='azure.cli.command_modules.config.custom#{}')
1212

1313
with self.command_group('config', config_custom, is_experimental=True) as g:
1414
g.command('set', 'config_set')
1515
g.command('get', 'config_get')
1616
g.command('unset', 'config_unset')
17+
18+
with self.command_group('config param-persist', config_custom, is_experimental=True) as g:
19+
g.command('on', 'turn_param_persist_on')
20+
g.command('off', 'turn_param_persist_off')
21+
g.show_command('show', 'show_param_persist', validator=validate_param_persist)
22+
g.command('delete', 'delete_param_persist', validator=validate_param_persist_for_delete)

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

+38
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,41 @@ def config_unset(cmd, key=None, local=False):
8686

8787
with ScopedConfig(cmd.cli_ctx.config, local):
8888
cmd.cli_ctx.config.remove_option(section, name)
89+
90+
91+
def turn_param_persist_on(cmd):
92+
if not cmd.cli_ctx.local_context.is_on:
93+
cmd.cli_ctx.local_context.turn_on()
94+
logger.warning('Parameter persistence is turned on, you can run `az config param-persist off` to turn it off.')
95+
else:
96+
raise CLIError('Parameter persistence is on already.')
97+
98+
99+
def turn_param_persist_off(cmd):
100+
if cmd.cli_ctx.local_context.is_on:
101+
cmd.cli_ctx.local_context.turn_off()
102+
logger.warning('Parameter persistence is turned off, you can run `az config param-persist on` to turn it on.')
103+
else:
104+
raise CLIError('Parameter persistence is off already.')
105+
106+
107+
def show_param_persist(cmd, name=None):
108+
if not name:
109+
name = None
110+
return cmd.cli_ctx.local_context.get_value(name)
111+
112+
113+
def delete_param_persist(cmd, name=None, all=False, yes=False, purge=False, recursive=False): # pylint: disable=redefined-builtin
114+
if name:
115+
cmd.cli_ctx.local_context.delete(name)
116+
117+
if all:
118+
from azure.cli.core.util import user_confirmation
119+
if purge:
120+
user_confirmation('You are going to delete parameter persistence file. '
121+
'Are you sure you want to continue this operation ?', yes)
122+
cmd.cli_ctx.local_context.delete_file(recursive)
123+
else:
124+
user_confirmation('You are going to clear all parameter persistence values. '
125+
'Are you sure you want to continue this operation ?', yes)
126+
cmd.cli_ctx.local_context.clear(recursive)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
import unittest
6+
7+
from azure.cli.testsdk import LocalContextScenarioTest
8+
9+
10+
class ParamPersistScenarioTest(LocalContextScenarioTest):
11+
12+
def test_param_persist_commands(self):
13+
self.cmd('config param-persist show')
14+
self.cmd('config param-persist show resource_group_name vnet_name')
15+
self.cmd('config param-persist delete resource_group_name vnet_name')
16+
self.cmd('config param-persist delete --all -y')
17+
self.cmd('config param-persist delete --all --purge -y')
18+
self.cmd('config param-persist delete --all --purge -y --recursive')
19+
20+
from knack.util import CLIError
21+
with self.assertRaises(CLIError):
22+
self.cmd('config param-persist delete resource_group_name --all')
23+
24+
25+
if __name__ == '__main__':
26+
unittest.main()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ def load_arguments(self, _):
3232
c.argument('all', help='Clear all local context data. Either --name or --all can be specified.', action='store_true')
3333
c.argument('yes', options_list=['--yes', '-y'], help='Do not prompt for confirmation. Only available when --all is specified.', action='store_true')
3434
c.argument('purge', help='Delete local context file from working directory. Only available when --all is specified.', action='store_true')
35-
c.argument('recursive', help='Indicates this is recursive delete of local context. Only available when --all is specified.', action='store_true')
35+
c.argument('recursive', help='Indicate this is recursive delete of local context. Only available when --all is specified.', action='store_true')

src/azure-cli/azure/cli/command_modules/configure/commands.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ def load_command_table(self, _):
2020
g.command('delete', 'delete_cache_contents')
2121
g.command('purge', 'purge_cache_contents')
2222

23-
with self.command_group('local-context', configure_custom, is_experimental=True) as g:
23+
def _local_context_deprecate_message(self):
24+
msg = "This {} has been deprecated and will be removed in future release.".format(self.object_type)
25+
msg += " Use '{}' instead.".format(self.redirect)
26+
# msg += " For more information go to"
27+
# msg += " <Add param persist doc link here when it is ready.>"
28+
return msg
29+
30+
with self.command_group('local-context', configure_custom, is_experimental=True,
31+
deprecate_info=self.deprecate(redirect="config param-persist",
32+
message_func=_local_context_deprecate_message)) as g:
2433
g.command('on', 'turn_local_context_on')
2534
g.command('off', 'turn_local_context_off')
2635
g.command('show', 'show_local_context', validator=validate_local_context) # pylint: disable=show-command

0 commit comments

Comments
 (0)