-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathcli.py
More file actions
195 lines (168 loc) · 5.29 KB
/
cli.py
File metadata and controls
195 lines (168 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import typing as t
import sys
import click
from sqlmesh_dbt.operations import DbtOperations, create
from sqlmesh_dbt.error import cli_global_error_handler, ErrorHandlingGroup
from pathlib import Path
from sqlmesh_dbt.options import YamlParamType
import functools
def _get_dbt_operations(
ctx: click.Context, vars: t.Optional[t.Dict[str, t.Any]], threads: t.Optional[int] = None
) -> DbtOperations:
if not isinstance(ctx.obj, functools.partial):
raise ValueError(f"Unexpected click context object: {type(ctx.obj)}")
dbt_operations = ctx.obj(vars=vars, threads=threads)
if not isinstance(dbt_operations, DbtOperations):
raise ValueError(f"Unexpected dbt operations type: {type(dbt_operations)}")
@ctx.call_on_close
def _cleanup() -> None:
dbt_operations.close()
return dbt_operations
vars_option = click.option(
"--vars",
type=YamlParamType(),
help="Supply variables to the project. This argument overrides variables defined in your dbt_project.yml file. This argument should be a YAML string, eg. '{my_variable: my_value}'",
)
select_option = click.option(
"-s",
"--select",
multiple=True,
help="Specify the nodes to include.",
)
model_option = click.option(
"-m",
"--models",
"--model",
multiple=True,
help="Specify the model nodes to include; other nodes are excluded.",
)
exclude_option = click.option("--exclude", multiple=True, help="Specify the nodes to exclude.")
# TODO: expand this out into --resource-type/--resource-types and --exclude-resource-type/--exclude-resource-types
resource_types = [
"metric",
"semantic_model",
"saved_query",
"source",
"analysis",
"model",
"test",
"unit_test",
"exposure",
"snapshot",
"seed",
"default",
"all",
]
resource_type_option = click.option(
"--resource-type", type=click.Choice(resource_types, case_sensitive=False)
)
@click.group(cls=ErrorHandlingGroup, invoke_without_command=True)
@click.option("--profile", help="Which existing profile to load. Overrides output.profile")
@click.option("-t", "--target", help="Which target to load for the given profile")
@click.option(
"-d",
"--debug/--no-debug",
default=False,
help="Display debug logging during dbt execution. Useful for debugging and making bug reports events to help when debugging.",
)
@click.pass_context
@cli_global_error_handler
def dbt(
ctx: click.Context,
profile: t.Optional[str] = None,
target: t.Optional[str] = None,
debug: bool = False,
) -> None:
"""
An ELT tool for managing your SQL transformations and data models, powered by the SQLMesh engine.
"""
if "--help" in sys.argv:
# we dont need to import sqlmesh/load the project for CLI help
return
# we have a partially applied function here because subcommands might set extra options like --vars
# that need to be known before we attempt to load the project
ctx.obj = functools.partial(
create, project_dir=Path.cwd(), profile=profile, target=target, debug=debug
)
if not ctx.invoked_subcommand:
if profile or target:
# trigger a project load to validate the specified profile / target
ctx.obj()
click.echo(
f"No command specified. Run `{ctx.info_name} --help` to see the available commands."
)
@dbt.command()
@select_option
@model_option
@exclude_option
@resource_type_option
@click.option(
"-f",
"--full-refresh",
is_flag=True,
default=False,
help="If specified, sqlmesh will drop incremental models and fully-recalculate the incremental table from the model definition.",
)
@click.option(
"--env",
"--environment",
help="Run against a specific Virtual Data Environment (VDE) instead of the main environment",
)
@click.option(
"--empty/--no-empty", default=False, help="If specified, limit input refs and sources"
)
@click.option(
"--threads",
type=int,
help="Specify number of threads to use while executing models. Overrides settings in profiles.yml.",
)
@vars_option
@click.pass_context
def run(
ctx: click.Context,
vars: t.Optional[t.Dict[str, t.Any]],
threads: t.Optional[int],
env: t.Optional[str] = None,
**kwargs: t.Any,
) -> None:
"""Compile SQL and execute against the current target database."""
_get_dbt_operations(ctx, vars, threads).run(environment=env, **kwargs)
@dbt.command(name="list")
@select_option
@model_option
@exclude_option
@resource_type_option
@vars_option
@click.pass_context
def list_(ctx: click.Context, vars: t.Optional[t.Dict[str, t.Any]], **kwargs: t.Any) -> None:
"""List the resources in your project"""
_get_dbt_operations(ctx, vars).list_(**kwargs)
@dbt.command(name="ls", hidden=True) # hidden alias for list
@click.pass_context
def ls(ctx: click.Context) -> None:
"""List the resources in your project"""
ctx.forward(list_)
def _not_implemented(name: str) -> None:
@dbt.command(name=name)
def _not_implemented() -> None:
"""Not implemented"""
click.echo(f"dbt {name} not implemented")
for subcommand in (
"build",
"clean",
"clone",
"compile",
"debug",
"deps",
"docs",
"init",
"parse",
"retry",
"run-operation",
"seed",
"show",
"snapshot",
"source",
"test",
):
_not_implemented(subcommand)