Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

click working with list #6641

Merged
merged 14 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20230117-213729.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: dbt list working with click
time: 2023-01-17T21:37:29.91632-05:00
custom:
Author: michelleark
Issue: "5549"
29 changes: 22 additions & 7 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
from dbt.task.run import RunTask
from dbt.task.test import TestTask
from dbt.task.snapshot import SnapshotTask
from dbt.task.list import ListTask


# CLI invocation
def cli_runner():
# Alias "list" to "ls"
ls = copy(cli.commands["list"])
ls.hidden = True
cli.add_command(ls, "ls")

# Run the cli
cli()

Expand Down Expand Up @@ -155,6 +151,7 @@ def docs(ctx, **kwargs):
@p.compile_docs
@p.defer
@p.exclude
@p.models
@p.profile
@p.profiles_dir
@p.project_dir
Expand Down Expand Up @@ -196,6 +193,7 @@ def docs_serve(ctx, **kwargs):
@p.defer
@p.exclude
@p.full_refresh
@p.models
@p.parse_only
@p.profile
@p.profiles_dir
Expand Down Expand Up @@ -278,6 +276,7 @@ def init(ctx, **kwargs):
@click.pass_context
@p.exclude
@p.indirect_selection
@p.models
@p.output
@p.output_keys
@p.profile
Expand All @@ -290,10 +289,21 @@ def init(ctx, **kwargs):
@p.target
@p.vars
@requires.preflight
@requires.profile
@requires.project
def list(ctx, **kwargs):
"""List the resources in your project"""
click.echo(f"`{inspect.stack()[0][3]}` called\n flags: {ctx.obj['flags']}")
return None, True
config = RuntimeConfig.from_parts(ctx.obj["project"], ctx.obj["profile"], ctx.obj["flags"])
task = ListTask(ctx.obj["flags"], config)

results = task.run()
success = task.interpret_results(results)
return results, success


ls = copy(cli.commands["list"])
ls.hidden = True
cli.add_command(ls, "ls")


# dbt parse
Expand Down Expand Up @@ -323,6 +333,7 @@ def parse(ctx, **kwargs):
@p.exclude
@p.fail_fast
@p.full_refresh
@p.models
@p.profile
@p.profiles_dir
@p.project_dir
Expand Down Expand Up @@ -368,6 +379,7 @@ def run_operation(ctx, **kwargs):
@click.pass_context
@p.exclude
@p.full_refresh
@p.models
@p.profile
@p.profiles_dir
@p.project_dir
Expand All @@ -392,6 +404,7 @@ def seed(ctx, **kwargs):
@click.pass_context
@p.defer
@p.exclude
@p.models
@p.profile
@p.profiles_dir
@p.project_dir
Expand Down Expand Up @@ -425,6 +438,7 @@ def source(ctx, **kwargs):
@source.command("freshness")
@click.pass_context
@p.exclude
@p.models
@p.output_path # TODO: Is this ok to re-use? We have three different output params, how much can we consolidate?
@p.profile
@p.profiles_dir
Expand All @@ -449,6 +463,7 @@ def freshness(ctx, **kwargs):
@p.exclude
@p.fail_fast
@p.indirect_selection
@p.models
@p.profile
@p.profiles_dir
@p.project_dir
Expand Down
12 changes: 11 additions & 1 deletion core/dbt/cli/option_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from click import ParamType
from click import ParamType, Choice

from dbt.config.utils import parse_cli_vars
from dbt.exceptions import ValidationException
Expand Down Expand Up @@ -33,3 +33,13 @@ def convert(self, value, param, ctx):
return None
else:
return value


class ChoiceTuple(Choice):
name = "CHOICE_TUPLE"

def convert(self, value, param, ctx):
for value_item in value:
super().convert(value_item, param, ctx)

return value
44 changes: 44 additions & 0 deletions core/dbt/cli/options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import click


# Implementation from: https://stackoverflow.com/a/48394004
# Note MultiOption options must be specified with type=tuple or type=ChoiceTuple (https://github.com/pallets/click/issues/2012)
class MultiOption(click.Option):
def __init__(self, *args, **kwargs):
self.save_other_options = kwargs.pop("save_other_options", True)
nargs = kwargs.pop("nargs", -1)
assert nargs == -1, "nargs, if set, must be -1 not {}".format(nargs)
super(MultiOption, self).__init__(*args, **kwargs)
self._previous_parser_process = None
self._eat_all_parser = None

def add_to_parser(self, parser, ctx):
def parser_process(value, state):
# method to hook to the parser.process
done = False
value = [value]
if self.save_other_options:
# grab everything up to the next option
while state.rargs and not done:
for prefix in self._eat_all_parser.prefixes:
if state.rargs[0].startswith(prefix):
done = True
if not done:
value.append(state.rargs.pop(0))
else:
# grab everything remaining
value += state.rargs
state.rargs[:] = []
value = tuple(value)
# call the actual process
self._previous_parser_process(value, state)

retval = super(MultiOption, self).add_to_parser(parser, ctx)
for name in self.opts:
our_parser = parser._long_opt.get(name) or parser._short_opt.get(name)
if our_parser:
self._eat_all_parser = our_parser
self._previous_parser_process = our_parser.process
our_parser.process = parser_process
break
return retval
30 changes: 22 additions & 8 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pathlib import Path, PurePath

import click
from dbt.cli.option_types import YAML
from dbt.cli.options import MultiOption
from dbt.cli.option_types import YAML, ChoiceTuple
from dbt.cli.resolvers import default_project_dir, default_profiles_dir


Expand Down Expand Up @@ -80,7 +81,9 @@
hidden=True,
)

exclude = click.option("--exclude", envvar=None, help="Specify the nodes to exclude.")
exclude = click.option(
"--exclude", envvar=None, type=tuple, cls=MultiOption, help="Specify the nodes to exclude."
)

fail_fast = click.option(
"--fail-fast/--no-fail-fast",
Expand Down Expand Up @@ -133,13 +136,22 @@
hidden=True,
)

models = click.option(
"--models",
"--model",
"-m",
envvar=None,
help="Specify the nodes to include.",
cls=MultiOption,
type=tuple,
)

output = click.option(
"--output",
envvar=None,
help="TODO: No current help text",
type=click.Choice(["json", "name", "path", "selector"], case_sensitive=False),
default="name",
default="selector",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching default value of "selector" with what's in main.py on the main branch

)

output_keys = click.option(
Expand Down Expand Up @@ -233,10 +245,11 @@
)

resource_type = click.option(
"--resource-types",
"--resource-type",
envvar=None,
help="TODO: No current help text",
type=click.Choice(
type=ChoiceTuple(
[
"metric",
"source",
Expand All @@ -251,16 +264,17 @@
],
case_sensitive=False,
),
default="default",
cls=MultiOption,
default=(),
)

select = click.option(
"-m",
"-s",
"select",
"--select",
envvar=None,
help="Specify the nodes to include.",
multiple=True,
cls=MultiOption,
type=tuple,
)

selector = click.option(
Expand Down
Binary file modified core/dbt/docs/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified core/dbt/docs/build/doctrees/index.doctree
Binary file not shown.
Loading