Skip to content

Commit

Permalink
feat(cli): Add validation for new cli options. Move click definitions…
Browse files Browse the repository at this point in the history
… to cli.py

Resolves: dbt-labs#65
  • Loading branch information
nicholasyager committed Jun 25, 2023
1 parent 9c760c7 commit 492d50a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 68 deletions.
67 changes: 66 additions & 1 deletion dbt_meshify/cli.py
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
# TODO add the logic for each meshify command to independent tasks here to make cli thinner
import functools

import click

# define common parameters
project_path = click.option(
"--project-path",
type=click.Path(exists=True),
default=".",
help="The path to the dbt project to operate on. Defaults to the current directory.",
)

exclude = click.option(
"--exclude",
"-e",
default=None,
help="The dbt selection syntax specifying the resources to exclude in the operation",
)

group_yml_path = click.option(
"--group-yml-path",
type=click.Path(exists=False),
help="An optional path to store the new group YAML definition.",
)

select = click.option(
"--select",
"-s",
default=None,
help="The dbt selection syntax specifying the resources to include in the operation",
)

selector = click.option(
"--selector",
default=None,
help="The name of the YML selector specifying the resources to include in the operation",
)

owner_name = click.option(
"--owner-name",
help="The group Owner's name.",
)

owner_email = click.option(
"--owner-email",
help="The group Owner's email address.",
)

owner_properties = click.option(
"--owner-properties",
help="Additional properties to assign to a group Owner.",
)


def owner(func):
"""Add click options and argument validation for creating Owner objects."""

@functools.wraps(func)
def wrapper_decorator(*args, **kwargs):
if kwargs.get('owner_name') is None and kwargs.get('owner_email') is None:
raise click.UsageError(
"Groups require an Owner to be defined using --owner-name and/or --owner-email."
)
return func(*args, **kwargs)

return wrapper_decorator
82 changes: 15 additions & 67 deletions dbt_meshify/main.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,25 @@
import functools
import os
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from typing import Optional

import click
import yaml
from dbt.contracts.graph.unparsed import Owner

from .cli import (
exclude,
group_yml_path,
owner,
owner_email,
owner_name,
owner_properties,
project_path,
select,
selector,
)
from .dbt_projects import DbtProject, DbtProjectHolder, DbtSubProject
from .storage.yaml_editors import DbtMeshModelConstructor

# define common parameters
project_path = click.option(
"--project-path",
type=click.Path(exists=True),
default=".",
help="The path to the dbt project to operate on. Defaults to the current directory.",
)

exclude = click.option(
"--exclude",
"-e",
default=None,
help="The dbt selection syntax specifying the resources to exclude in the operation",
)

group_yml_path = click.option(
"--group-yml-path",
type=click.Path(exists=False),
help="An optional path to store the new group YAML definition.",
)

select = click.option(
"--select",
"-s",
default=None,
help="The dbt selection syntax specifying the resources to include in the operation",
)

owner_name = click.option(
"--owner-name",
help="The group Owner's name.",
)

owner_email = click.option(
"--owner-email",
help="The group Owner's email address.",
)

owner_properties = click.option(
"--owner-properties",
help="Additional properties to assign to a group Owner.",
)

selector = click.option(
"--selector",
default=None,
help="The name of the YML selector specifying the resources to include in the operation",
)


def owner(func):
"""Add click options and argument validation for creating Owner objects."""

@functools.wraps(func)
def wrapper_decorator(*args, **kwargs):
print("Validating owner configs.")
# Do something before
value = func(*args, **kwargs)
# Do something after
return value

return wrapper_decorator


# define cli group
@click.group()
Expand Down Expand Up @@ -244,14 +191,14 @@ def create_group(
"The provided group-yml-path is not contained within the provided dbt project."
)

owner: Owner = Owner(
group_owner: Owner = Owner(
name=owner_name, email=owner_email, _extra=yaml.safe_load(owner_properties)
)

grouper = ResourceGrouper(project)
grouper.add_group(
name=name,
owner=owner,
owner=group_owner,
select=select,
exclude=exclude,
selector=selector,
Expand All @@ -268,6 +215,7 @@ def create_group(
@owner_name
@owner_email
@owner_properties
@owner
@group_yml_path
@click.pass_context
def group(
Expand Down

0 comments on commit 492d50a

Please sign in to comment.