Skip to content

Commit

Permalink
Refactor VariableOption flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pczajka committed Jul 19, 2024
1 parent 59905dc commit 93805e6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
19 changes: 13 additions & 6 deletions src/snowflake/cli/api/commands/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<key>=<value>"`. '

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 "<key>=<value>"`. '
"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,
)


Expand Down
4 changes: 2 additions & 2 deletions src/snowflake/cli/plugins/git/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
):
"""
Expand Down
5 changes: 4 additions & 1 deletion src/snowflake/cli/plugins/init/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand Down
8 changes: 3 additions & 5 deletions src/snowflake/cli/plugins/sql/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/snowflake/cli/plugins/stage/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -201,7 +201,7 @@ def execute(
show_default=False,
),
on_error: OnErrorType = OnErrorOption,
variables: Optional[List[str]] = VariablesOption,
variables: Optional[List[str]] = ExecuteVariablesOption,
**options,
):
"""
Expand Down
13 changes: 2 additions & 11 deletions tests/__snapshots__/test_help_messages.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<key>=<value>"`. 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. |
+------------------------------------------------------------------------------+
Expand Down Expand Up @@ -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 |
Expand Down

0 comments on commit 93805e6

Please sign in to comment.