Skip to content

Commit

Permalink
[Tag] Allow customizing tag message (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiasli authored Dec 17, 2020
1 parent 6354f3d commit c265fd4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
12 changes: 7 additions & 5 deletions knack/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .preview import PreviewItem
from .experimental import ExperimentalItem
from .log import get_logger
from .util import CLIError
from .util import CLIError, status_tag_messages


logger = get_logger(__name__)
Expand Down Expand Up @@ -250,8 +250,9 @@ def __call__(self, parser, namespace, values, option_string=None):
return PreviewArgumentAction

def _get_preview_arg_message(self):
return "{} '{}' is in preview. It may be changed/removed in a future release.".format(
self.object_type.capitalize(), self.target)
# "Argument xxx"
subject = "{} '{}'".format(self.object_type.capitalize(), self.target)
return status_tag_messages['preview'].format(subject)

options_list = kwargs.get('options_list', None)
object_type = 'argument'
Expand Down Expand Up @@ -300,8 +301,9 @@ def __call__(self, parser, namespace, values, option_string=None):
return ExperimentalArgumentAction

def _get_experimental_arg_message(self):
return "{} '{}' is experimental and not covered by customer support. " \
"Please use with discretion.".format(self.object_type.capitalize(), self.target)
# "Argument xxx"
subject = "{} '{}'".format(self.object_type.capitalize(), self.target)
return status_tag_messages['experimental'].format(subject)

options_list = kwargs.get('options_list', None)
object_type = 'argument'
Expand Down
8 changes: 3 additions & 5 deletions knack/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from .util import StatusTag
from .util import StatusTag, status_tag_messages

_EXPERIMENTAL_TAG = '[Experimental]'
_experimental_kwarg = 'experimental_info'
Expand Down Expand Up @@ -50,8 +50,7 @@ def __init__(self, cli_ctx, object_type='', target=None, tag_func=None, message_
"""

def _default_get_message(self):
return "This {} is experimental and not covered by customer support. " \
"Please use with discretion.".format(self.object_type)
return status_tag_messages['experimental'].format("This " + self.object_type)

super(ExperimentalItem, self).__init__(
cli_ctx=cli_ctx,
Expand All @@ -68,8 +67,7 @@ class ImplicitExperimentalItem(ExperimentalItem):
def __init__(self, **kwargs):

def get_implicit_experimental_message(self):
return "Command group '{}' is experimental and not covered by customer support. " \
"Please use with discretion.".format(self.target)
return status_tag_messages['experimental'].format("Command group '{}'".format(self.target))

kwargs.update({
'tag_func': lambda _: '',
Expand Down
7 changes: 3 additions & 4 deletions knack/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from .util import StatusTag
from .util import StatusTag, status_tag_messages

_PREVIEW_TAG = '[Preview]'
_preview_kwarg = 'preview_info'
Expand Down Expand Up @@ -50,7 +50,7 @@ def __init__(self, cli_ctx, object_type='', target=None, tag_func=None, message_
"""

def _default_get_message(self):
return "This {} is in preview. It may be changed/removed in a future release.".format(self.object_type)
return status_tag_messages['preview'].format("This " + self.object_type)

super(PreviewItem, self).__init__(
cli_ctx=cli_ctx,
Expand All @@ -67,8 +67,7 @@ class ImplicitPreviewItem(PreviewItem):
def __init__(self, **kwargs):

def get_implicit_preview_message(self):
return "Command group '{}' is in preview. It may be changed/removed " \
"in a future release.".format(self.target)
return status_tag_messages['preview'].format("Command group '{}'".format(self.target))

kwargs.update({
'tag_func': lambda _: '',
Expand Down
8 changes: 8 additions & 0 deletions knack/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

NO_COLOR_VARIABLE_NAME = 'KNACK_NO_COLOR'

# Override these values to customize the status message.
# The message should contain a placeholder indicating the subject (like 'This command group', 'Commend group xxx').
# (A dict is used to avoid the "from A import B" pitfall that creates a copy of the imported B.)
status_tag_messages = {
'preview': "{} is in preview. It may be changed/removed in a future release.",
'experimental': "{} is experimental and under development."
}


class CommandResultItem(object): # pylint: disable=too-few-public-methods
def __init__(self, result, table_transformer=None, is_query_active=False,
Expand Down
14 changes: 6 additions & 8 deletions tests/test_experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def test_experimental_command_implicitly_execute(self):
""" Ensure general warning displayed when running command from an experimental parent group. """
self.cli_ctx.invoke('grp1 cmd1 -b b'.split())
actual = self.io.getvalue()
expected = "Command group 'grp1' is experimental and not covered by customer support. " \
"Please use with discretion."
expected = "Command group 'grp1' is experimental and under development."
self.assertIn(remove_space(expected), remove_space(actual))

@redirect_io
Expand All @@ -93,7 +92,7 @@ def test_experimental_command_plain_execute(self):
""" Ensure general warning displayed when running experimental command. """
self.cli_ctx.invoke('cmd1 -b b'.split())
actual = self.io.getvalue()
expected = "This command is experimental and not covered by customer support. Please use with discretion."
expected = "This command is experimental and under development."
self.assertIn(remove_space(expected), remove_space(actual))


Expand Down Expand Up @@ -134,7 +133,7 @@ def test_experimental_command_group_help_plain(self):
expected = """
Group
cli group1 : A group.
This command group is experimental and not covered by customer support. Please use with discretion.
This command group is experimental and under development.
Commands:
cmd1 : Short summary here.
Expand All @@ -151,7 +150,7 @@ def test_experimental_command_implicitly(self):
Command
{} group1 cmd1 : Short summary here.
Long summary here. Still long summary.
Command group 'group1' is experimental and not covered by customer support. Please use with discretion.
Command group 'group1' is experimental and under development.
""".format(self.cli_ctx.name)
self.assertIn(remove_space(expected), remove_space(actual))

Expand Down Expand Up @@ -194,7 +193,7 @@ def test_experimental_arguments_command_help(self):
expected = """
Arguments
--arg1 [Experimental] [Required] : Arg1.
Argument '--arg1' is experimental and not covered by customer support. Please use with discretion.
Argument '--arg1' is experimental and under development.
""".format(self.cli_ctx.name)
self.assertIn(remove_space(expected), remove_space(actual))

Expand All @@ -203,8 +202,7 @@ def test_experimental_arguments_execute(self):
""" Ensure deprecated arguments can be used. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar'.split())
actual = self.io.getvalue()
experimental_expected = "Argument '--arg1' is experimental and not covered by customer support. " \
"Please use with discretion."
experimental_expected = "Argument '--arg1' is experimental and under development."
self.assertIn(experimental_expected, actual)

action_expected = "Side-effect from some original action!"
Expand Down

0 comments on commit c265fd4

Please sign in to comment.