15
15
from __future__ import annotations
16
16
17
17
import logging
18
+ from typing import Annotated
18
19
19
20
import typer
21
+ from snowflake .cli ._plugins .dbt .constants import DBT_COMMANDS
20
22
from snowflake .cli ._plugins .dbt .manager import DBTManager
23
+ from snowflake .cli .api .commands .decorators import global_options_with_connection
21
24
from snowflake .cli .api .commands .snow_typer import SnowTyperFactory
22
25
from snowflake .cli .api .output .types import CommandResult , QueryResult
23
26
@@ -37,31 +40,47 @@ def list_dbts(
37
40
** options ,
38
41
) -> CommandResult :
39
42
"""
40
- List all dbt projects on Snowflake.
43
+ List all dbt on Snowflake projects .
41
44
"""
42
45
return QueryResult (DBTManager ().list ())
43
46
44
47
45
- @app .command (
46
- "execute" ,
47
- requires_connection = True ,
48
- context_settings = {"allow_extra_args" : True , "ignore_unknown_options" : True },
48
+ # `execute` is a pass through command group, meaning that all params after command should be passed over as they are,
49
+ # suppressing usual CLI behaviour for displaying help or formatting options.
50
+ dbt_execute_app = SnowTyperFactory (
51
+ name = "execute" ,
52
+ help = "Execute a dbt command" ,
49
53
)
50
- def execute (
51
- ctx : typer . Context ,
52
- dbt_command : str = typer . Argument (
53
- help = "dbt command to execute, i. e. run, compile, seed..." ,
54
- ),
55
- name : str = typer . Option (
56
- default = ...,
57
- help = "Name of the dbt object to execute command on." ,
58
- ) ,
54
+ app . add_typer ( dbt_execute_app )
55
+
56
+
57
+ @ dbt_execute_app . callback ()
58
+ @ global_options_with_connection
59
+ def before_callback (
60
+ name : Annotated [
61
+ str , typer . Argument ( help = "Name of the dbt object to execute command on." )
62
+ ] ,
59
63
** options ,
60
- ) -> CommandResult :
61
- """
62
- Execute command on dbt in Snowflake project.
63
- """
64
- # ctx.args are parameters that were not captured as known cli params (those are in **options).
65
- # as a consequence, we don't support passing params known to snowflake cli further to dbt
66
- dbt_cli_args = ctx .args
67
- return QueryResult (DBTManager ().execute (dbt_command , name , * dbt_cli_args ))
64
+ ):
65
+ """Handles global options passed before the command and takes pipeline name to be accessed through child context later"""
66
+ pass
67
+
68
+
69
+ for cmd in DBT_COMMANDS :
70
+
71
+ @dbt_execute_app .command (
72
+ name = cmd ,
73
+ requires_connection = False ,
74
+ requires_global_options = False ,
75
+ context_settings = {"allow_extra_args" : True , "ignore_unknown_options" : True },
76
+ help = f"Execute { cmd } command on dbt on Snowflake project." ,
77
+ add_help_option = False ,
78
+ )
79
+ def _dbt_execute (
80
+ ctx : typer .Context ,
81
+ ) -> CommandResult :
82
+ # TODO: figure out how to present logs to users
83
+ dbt_cli_args = ctx .args
84
+ dbt_command = ctx .command .name
85
+ name = ctx .parent .params ["name" ]
86
+ return QueryResult (DBTManager ().execute (dbt_command , name , * dbt_cli_args ))
0 commit comments