Skip to content

Commit

Permalink
Implement --version in click (#6802)
Browse files Browse the repository at this point in the history
  • Loading branch information
stu-k authored Jan 31, 2023
1 parent 84bf5b4 commit 59d773e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20230126-143102.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Implement --version for click cli
time: 2023-01-26T14:31:02.740282-06:00
custom:
Author: stu-k
Issue: "6757"
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ jobs:
mypy --version
python -m pip install -r requirements.txt
python -m pip install -r dev-requirements.txt
# Running version disabled temporarily because version isn't implemented on this branch
# Please un-comment it when GH #6757 / CT-1926 is complete
#dbt --version
dbt --version
- name: Run pre-commit hooks
run: pre-commit run --all-files --show-diff-on-failure
Expand Down
17 changes: 11 additions & 6 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from copy import copy
from pprint import pformat as pf # This is temporary for RAT-ing
from typing import List, Tuple, Optional

import click
Expand All @@ -22,6 +21,7 @@
from dbt.task.build import BuildTask
from dbt.task.generate import GenerateTask
from dbt.task.init import InitTask
from dbt.version import __version__, get_version_information


# CLI invocation
Expand All @@ -34,6 +34,10 @@ class dbtUsageException(Exception):
pass


class dbtInternalException(Exception):
pass


# Programmatic invocation
class dbtRunner:
def __init__(
Expand All @@ -52,6 +56,11 @@ def invoke(self, args: List[str]) -> Tuple[Optional[List], bool]:
"manifest": self.manifest,
}
return cli.invoke(dbt_ctx)
except click.exceptions.Exit as e:
# 0 exit code, expected for --version early exit
if str(e) == "0":
return [], True
raise dbtInternalException(f"unhandled exit code {str(e)}")
except (click.NoSuchOption, click.UsageError) as e:
raise dbtUsageException(e.message)

Expand All @@ -64,6 +73,7 @@ def invoke(self, args: List[str]) -> Tuple[Optional[List], bool]:
epilog="Specify one of these sub-commands and you can find more help from there.",
)
@click.pass_context
@click.version_option(version=__version__, message=get_version_information())
@p.anonymous_usage_stats
@p.cache_selected_only
@p.debug
Expand All @@ -82,7 +92,6 @@ def invoke(self, args: List[str]) -> Tuple[Optional[List], bool]:
@p.static_parser
@p.use_colors
@p.use_experimental_parser
@p.version
@p.version_check
@p.warn_error
@p.warn_error_options
Expand All @@ -91,10 +100,6 @@ def cli(ctx, **kwargs):
"""An ELT tool for managing your SQL transformations and data models.
For more documentation on these commands, visit: docs.getdbt.com
"""
# Version info
if ctx.params["version"]:
click.echo(f"`version` called\n ctx.params: {pf(ctx.params)}")
return


# dbt build
Expand Down
6 changes: 0 additions & 6 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,6 @@
default="{}",
)

version = click.option(
"--version",
envvar=None,
help="Show version information",
is_flag=True,
)

version_check = click.option(
"--version-check/--no-version-check",
Expand Down
Binary file modified core/dbt/docs/build/doctrees/environment.pickle
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/unit/test_cli_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def test_mp_context(self, run_context):
@pytest.mark.parametrize("param", cli.params)
def test_cli_group_flags_from_params(self, run_context, param):
flags = Flags(run_context)
if param.name.upper() == "VERSION":
return
assert hasattr(flags, param.name.upper())
assert getattr(flags, param.name.upper()) == run_context.params[param.name.lower()]

Expand Down

0 comments on commit 59d773e

Please sign in to comment.