-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_dev_app.py
More file actions
97 lines (88 loc) · 3.35 KB
/
_dev_app.py
File metadata and controls
97 lines (88 loc) · 3.35 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
import contextlib
from pathlib import Path
from typing import Annotated, Any
import typer
from rich import print
from cognite_toolkit._cdf_tk.cdf_toml import CDFToml
from cognite_toolkit._cdf_tk.client import ToolkitClient
from cognite_toolkit._cdf_tk.commands import ResourcesCommand
from cognite_toolkit._cdf_tk.feature_flags import FeatureFlag, Flags
from cognite_toolkit._cdf_tk.utils.auth import EnvironmentVariables
from ._dev_function_app import DevFunctionApp
from ._run import RunApp
CDF_TOML = CDFToml.load(Path.cwd())
class DevApp(typer.Typer):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.callback(invoke_without_command=True)(self.main)
self.add_typer(RunApp(*args, **kwargs), name="run")
self.add_typer(DevFunctionApp(*args, **kwargs), name="function")
if FeatureFlag.is_enabled(Flags.CREATE):
self.command("create")(self.create)
@staticmethod
def main(ctx: typer.Context) -> None:
"""Commands to work with development."""
if ctx.invoked_subcommand is None:
print("Use [bold yellow]cdf dev --help[/] for more information.")
return None
def create(
self,
kind: Annotated[
list[str] | None,
typer.Argument(
help="The kind of resource to create. eg. container, space, view, datamodel, etc.",
callback=lambda ctx, param, value: [
s.strip() for item in value or [] for s in item.split(",") if s.strip()
],
),
] = None,
module: Annotated[
str | None,
typer.Option(
"--module",
"-m",
help="Name of an existing module or a new module to create the resource in.",
),
] = None,
prefix: Annotated[
str | None,
typer.Option(
"--prefix",
"-p",
help="The prefix of the resource file to create without suffixes and extensions. "
"eg. --prefix=my_space. If not provided, a default prefix like 'my_<kind>' will be used.",
),
] = None,
verbose: Annotated[
bool,
typer.Option(
"--verbose",
"-v",
help="Turn on to get more verbose output when running the command",
),
] = False,
organization_dir: Annotated[
Path,
typer.Option(
"--organization-dir",
"-o",
help="Path to the organization directory",
),
] = CDF_TOML.cdf.default_organization_dir,
) -> None:
"""create resource YAMLs."""
client: ToolkitClient | None = None
with contextlib.redirect_stdout(None), contextlib.suppress(Exception):
# Try to load client if possible, but ignore errors.
# This is only used for logging purposes in the command.
client = EnvironmentVariables.create_from_environment().get_client()
cmd = ResourcesCommand(client=client)
cmd.run(
lambda: cmd.create(
organization_dir=organization_dir,
module_name=module,
kind=kind,
prefix=prefix,
verbose=verbose,
)
)