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

feat(dbt): Adding flag to raise an error when a model sync fails #266

Merged
merged 11 commits into from
Mar 11, 2024
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ repos:
# additional_dependencies: [flake8-bugbear]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.910' # Use the sha / tag you want to point at
rev: 'v0.981' # Use the sha / tag you want to point at
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to bump it to fix pre-commit for newer Python versions. python/mypy#13627

hooks:
- id: mypy
additional_dependencies:
Expand Down
90 changes: 31 additions & 59 deletions src/preset_cli/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from preset_cli.auth.lib import get_credentials_path, store_credentials
from preset_cli.auth.preset import JWTTokenError, PresetAuth
from preset_cli.cli.superset.main import superset
from preset_cli.lib import setup_logging, split_comma
from preset_cli.exceptions import CLIError
from preset_cli.lib import raise_cli_errors, setup_logging, split_comma

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -103,6 +104,7 @@ def is_help() -> bool:
@click.option("--loglevel", default="INFO")
@click.version_option()
@click.pass_context
@raise_cli_errors
def preset_cli( # pylint: disable=too-many-branches, too-many-locals, too-many-arguments, too-many-statements
ctx: click.core.Context,
baseurl: str,
Expand Down Expand Up @@ -138,14 +140,8 @@ def preset_cli( # pylint: disable=too-many-branches, too-many-locals, too-many-
credentials = yaml.load(input_, Loader=yaml.SafeLoader)
api_token = credentials["api_token"]
api_secret = credentials["api_secret"]
except Exception: # pylint: disable=broad-except
click.echo(
click.style(
"Couldn't read credentials",
fg="bright_red",
),
)
sys.exit(1)
except Exception as excinfo: # pylint: disable=broad-except
raise CLIError("Couldn't read credentials", 1) from excinfo
else:
manager_url = URL(baseurl.replace("api.", "manage."))
click.echo(
Expand All @@ -165,15 +161,12 @@ def preset_cli( # pylint: disable=too-many-branches, too-many-locals, too-many-
api_secret = cast(str, api_secret)
try:
ctx.obj["AUTH"] = PresetAuth(manager_api_url, api_token, api_secret)
except JWTTokenError:
click.echo(
click.style(
"Failed to auth using the provided credentials."
" Please run ``preset-cli auth``",
fg="bright_red",
),
except JWTTokenError as excinfo:
error_message = (
"Failed to auth using the provided credentials."
" Please run ``preset-cli auth``"
)
sys.exit(1)
raise CLIError(error_message, 1) from excinfo

if not workspaces and ctx.invoked_subcommand == "superset" and not is_help():
client = PresetClient(ctx.obj["MANAGER_URL"], ctx.obj["AUTH"])
Expand All @@ -189,13 +182,7 @@ def preset_cli( # pylint: disable=too-many-branches, too-many-locals, too-many-
i += 1

if i == 0:
click.echo(
click.style(
"No workspaces available",
fg="bright_red",
),
)
sys.exit(1)
raise CLIError("No workspaces available", 1)
if i == 1:
workspaces = hostnames

Expand Down Expand Up @@ -224,6 +211,7 @@ def preset_cli( # pylint: disable=too-many-branches, too-many-locals, too-many-
default=False,
help="Show existing credentials",
)
@raise_cli_errors
def auth(baseurl: str, overwrite: bool = False, show: bool = False) -> None:
"""
Store credentials for auth.
Expand All @@ -232,16 +220,11 @@ def auth(baseurl: str, overwrite: bool = False, show: bool = False) -> None:

if show:
if not credentials_path.exists():
click.echo(
click.style(
(
f"The file {credentials_path} doesn't exist. "
"Run ``preset-cli auth`` to create it."
),
fg="bright_red",
),
error_message = (
f"The file {credentials_path} doesn't exist. "
"Run ``preset-cli auth`` to create it."
)
sys.exit(1)
raise CLIError(error_message, 1)

ruler = "=" * len(str(credentials_path))
with open(credentials_path, encoding="utf-8") as input_:
Expand All @@ -251,16 +234,11 @@ def auth(baseurl: str, overwrite: bool = False, show: bool = False) -> None:
sys.exit(0)

if credentials_path.exists() and not overwrite:
click.echo(
click.style(
(
f"The file {credentials_path} already exists. "
"Pass ``--overwrite`` to replace it."
),
fg="bright_red",
),
error_message = (
f"The file {credentials_path} already exists. "
"Pass ``--overwrite`` to replace it."
)
sys.exit(1)
raise CLIError(error_message, 1)

manager_url = URL(baseurl.replace("api.", "manage."))
manager_api_url = URL(baseurl)
Expand Down Expand Up @@ -288,13 +266,7 @@ def get_teams(client: PresetClient) -> List[str]:
i += 1

if i == 0:
click.echo(
click.style(
"No teams available",
fg="bright_red",
),
)
sys.exit(1)
raise CLIError("No teams available", 1)
if i == 1:
return all_teams

Expand All @@ -314,6 +286,7 @@ def get_teams(client: PresetClient) -> List[str]:
default="users.yaml",
)
@click.pass_context
@raise_cli_errors
def invite_users(ctx: click.core.Context, teams: List[str], path: str) -> None:
"""
Invite users to join Preset teams.
Expand All @@ -336,6 +309,7 @@ def invite_users(ctx: click.core.Context, teams: List[str], path: str) -> None:
help="Save results to a YAML or CSV file instead of priting on the terminal",
)
@click.pass_context
@raise_cli_errors
def list_group_membership(
ctx: click.core.Context,
teams: List[str],
Expand All @@ -351,13 +325,10 @@ def list_group_membership(

# in case --save-report was used, confirm if a valid option was used before sending requests
if save_report and save_report.casefold() not in {"yaml", "csv"}:
click.echo(
click.style(
"Invalid option. Please use ``--save-report=csv`` or ``--save-report=yaml``",
fg="bright_red",
),
raise CLIError(
"Invalid option. Please use ``--save-report=csv`` or ``--save-report=yaml``",
1,
)
sys.exit(1)

for team in teams:
# print the team name in case multiple teams were provided and it's not an export
Expand Down Expand Up @@ -464,6 +435,7 @@ def export_group_membership_csv(groups: Dict[str, Any], team: str) -> None:
default="users.yaml",
)
@click.pass_context
@raise_cli_errors
def import_users(ctx: click.core.Context, teams: List[str], path: str) -> None:
"""
Import users by adding them via SCIM.
Expand Down Expand Up @@ -629,9 +601,9 @@ def sync_user_role_to_workspace(
)


preset_cli.add_command(auth)
preset_cli.add_command(invite_users)
preset_cli.add_command(import_users)
preset_cli.add_command(auth, name="auth")
preset_cli.add_command(invite_users, name="invite-users")
preset_cli.add_command(import_users, name="import-users")
Comment on lines +604 to +606
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For some reason, for the commands that involved the raise_cli_errors decorator, the tests were failing with command not found until I explicitly added the command names. 🤷

preset_cli.add_command(sync_roles)
preset_cli.add_command(superset)
preset_cli.add_command(list_group_membership)
preset_cli.add_command(list_group_membership, name="list-group-membership")
Loading
Loading