From 93805e6266cd1a6c3096d43d5f232c8312d7ca3f Mon Sep 17 00:00:00 2001 From: Patryk Czajka Date: Fri, 19 Jul 2024 16:33:54 +0200 Subject: [PATCH] Refactor VariableOption flag --- src/snowflake/cli/api/commands/flags.py | 19 +++++++++++++------ src/snowflake/cli/plugins/git/commands.py | 4 ++-- src/snowflake/cli/plugins/init/commands.py | 5 ++++- src/snowflake/cli/plugins/sql/commands.py | 8 +++----- src/snowflake/cli/plugins/stage/commands.py | 4 ++-- tests/__snapshots__/test_help_messages.ambr | 13 ++----------- 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/snowflake/cli/api/commands/flags.py b/src/snowflake/cli/api/commands/flags.py index 1e6d2c993e..1ba564e755 100644 --- a/src/snowflake/cli/api/commands/flags.py +++ b/src/snowflake/cli/api/commands/flags.py @@ -449,15 +449,22 @@ def _password_callback(value: str): NoInteractiveOption = typer.Option(False, "--no-interactive", help="Disable prompting.") -VariablesOption = typer.Option( - None, - "--variable", - "-D", - help='Variables for the execution context. For example: `-D "="`. ' + +def variables_option(description: str): + return typer.Option( + None, + "--variable", + "-D", + help=description, + show_default=False, + ) + + +ExecuteVariablesOption = variables_option( + 'Variables for the execution context. For example: `-D "="`. ' "For SQL files variables are use to expand the template and any unknown variable will cause an error. " "For Python files variables are used to update os.environ dictionary. Provided keys are capitalized to adhere to best practices." "In case of SQL files string values must be quoted in `''` (consider embedding quoting in the file).", - show_default=False, ) diff --git a/src/snowflake/cli/plugins/git/commands.py b/src/snowflake/cli/plugins/git/commands.py index 63c652b35b..478d666047 100644 --- a/src/snowflake/cli/plugins/git/commands.py +++ b/src/snowflake/cli/plugins/git/commands.py @@ -23,9 +23,9 @@ import typer from click import ClickException from snowflake.cli.api.commands.flags import ( + ExecuteVariablesOption, OnErrorOption, PatternOption, - VariablesOption, identifier_argument, like_option, ) @@ -276,7 +276,7 @@ def copy( def execute( repository_path: str = RepoPathArgument, on_error: OnErrorType = OnErrorOption, - variables: Optional[List[str]] = VariablesOption, + variables: Optional[List[str]] = ExecuteVariablesOption, **options, ): """ diff --git a/src/snowflake/cli/plugins/init/commands.py b/src/snowflake/cli/plugins/init/commands.py index 8fa1c5453d..b1eca7a8ca 100644 --- a/src/snowflake/cli/plugins/init/commands.py +++ b/src/snowflake/cli/plugins/init/commands.py @@ -22,8 +22,8 @@ from click import ClickException from snowflake.cli.api.commands.flags import ( NoInteractiveOption, - VariablesOption, parse_key_value_variables, + variables_option, ) from snowflake.cli.api.commands.snow_typer import SnowTyperFactory from snowflake.cli.api.constants import DEFAULT_SIZE_LIMIT_MB @@ -70,6 +70,9 @@ def _path_argument_callback(path: str) -> str: default=DEFAULT_SOURCE, help=f"local path to template directory or URL to git repository with templates.", ) +VariablesOption = variables_option( + "String in `key=value` format. Provided variables will not be prompted for." +) TEMPLATE_METADATA_FILE_NAME = "template.yml" diff --git a/src/snowflake/cli/plugins/sql/commands.py b/src/snowflake/cli/plugins/sql/commands.py index ae3e9c4f36..3f888d4c4b 100644 --- a/src/snowflake/cli/plugins/sql/commands.py +++ b/src/snowflake/cli/plugins/sql/commands.py @@ -21,6 +21,7 @@ from snowflake.cli.api.commands.decorators import with_project_definition from snowflake.cli.api.commands.flags import ( parse_key_value_variables, + variables_option, ) from snowflake.cli.api.commands.snow_typer import SnowTyperFactory from snowflake.cli.api.output.types import CommandResult, MultipleResults, QueryResult @@ -55,11 +56,8 @@ def execute_sql( "-i", help="Read the query from standard input. Use it when piping input to this command.", ), - data_override: List[str] = typer.Option( - None, - "--variable", - "-D", - help="String in format of key=value. If provided the SQL content will " + data_override: List[str] = variables_option( + "String in format of key=value. If provided the SQL content will " "be treated as template and rendered using provided data.", ), **options, diff --git a/src/snowflake/cli/plugins/stage/commands.py b/src/snowflake/cli/plugins/stage/commands.py index 0805440aea..781695b5b8 100644 --- a/src/snowflake/cli/plugins/stage/commands.py +++ b/src/snowflake/cli/plugins/stage/commands.py @@ -23,9 +23,9 @@ import typer from snowflake.cli.api.cli_global_context import cli_context from snowflake.cli.api.commands.flags import ( + ExecuteVariablesOption, OnErrorOption, PatternOption, - VariablesOption, like_option, ) from snowflake.cli.api.commands.snow_typer import SnowTyperFactory @@ -201,7 +201,7 @@ def execute( show_default=False, ), on_error: OnErrorType = OnErrorOption, - variables: Optional[List[str]] = VariablesOption, + variables: Optional[List[str]] = ExecuteVariablesOption, **options, ): """ diff --git a/tests/__snapshots__/test_help_messages.ambr b/tests/__snapshots__/test_help_messages.ambr index ab73ffa92e..6f2dd86e93 100644 --- a/tests/__snapshots__/test_help_messages.ambr +++ b/tests/__snapshots__/test_help_messages.ambr @@ -2032,16 +2032,8 @@ | git repository with templates. | | [default: | | https://github.com/snowflakedb/snowflake-c… | - | --variable -D TEXT Variables for the execution context. For | - | example: `-D "="`. For SQL | - | files variables are use to expand the | - | template and any unknown variable will | - | cause an error. For Python files variables | - | are used to update os.environ dictionary. | - | Provided keys are capitalized to adhere to | - | best practices.In case of SQL files string | - | values must be quoted in `''` (consider | - | embedding quoting in the file). | + | --variable -D TEXT String in `key=value` format. Provided | + | variables will not be prompted for. | | --no-interactive Disable prompting. | | --help -h Show this message and exit. | +------------------------------------------------------------------------------+ @@ -6756,7 +6748,6 @@ | --variable -D TEXT String in format of key=value. If provided the SQL | | content will be treated as template and rendered | | using provided data. | - | [default: None] | | --project -p TEXT Path where Snowflake project resides. Defaults to | | current working directory. | | --env TEXT String in format of key=value. Overrides variables |