Skip to content

Commit

Permalink
Refactoring: run command
Browse files Browse the repository at this point in the history
Signed-off-by: Ching Yi, Chan <[email protected]>
  • Loading branch information
qrtt1 committed Aug 31, 2023
1 parent b36e57b commit b8cf411
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 95 deletions.
101 changes: 6 additions & 95 deletions piperider_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
from piperider_cli.cli_utils import DbtUtil
from piperider_cli.cli_utils.cloud import CloudConnectorHelper
from piperider_cli.configuration import FileSystem, is_piperider_workspace_exist
from piperider_cli.error import DbtProjectNotFoundError, PipeRiderConflictOptionsError
from piperider_cli.error import DbtProjectNotFoundError
from piperider_cli.event import UserProfileConfigurator
from piperider_cli.event.track import TrackCommand
from piperider_cli.exitcode import EC_ERR_TEST_FAILED, EC_WARN_NO_PROFILED_MODULES
from piperider_cli.feedback import Feedback
from piperider_cli.generate_report import GenerateReport
from piperider_cli.guide import Guide
from piperider_cli.initializer import Initializer
from piperider_cli.runner import Runner

release_version = __version__ if sentry_env != 'development' else None

Expand Down Expand Up @@ -245,99 +243,12 @@ def diagnose(**kwargs):
@add_options(dbt_related_options)
@add_options(debug_option)
def run(**kwargs):
'Profile data source, run assertions, and generate report(s). By default, the raw results and reports are saved in ".piperider/outputs".'

datasource = kwargs.get('datasource')
table = kwargs.get('table')
output = kwargs.get('output')
open_report = kwargs.get('open')
enable_share = kwargs.get('share')
skip_report = kwargs.get('skip_report')
dbt_target_path = kwargs.get('dbt_target_path')
dbt_list = kwargs.get('dbt_list')
force_upload = kwargs.get('upload')
project_name = kwargs.get('project')
select = kwargs.get('select')
state = kwargs.get('state')

if project_name is not None:
os.environ.get('PIPERIDER_API_PROJECT')

console = Console()
env_dbt_resources = os.environ.get('PIPERIDER_DBT_RESOURCES')

# True -> 1, False -> 0
if sum([True if table else False, dbt_list, env_dbt_resources is not None]) > 1:
console.print("[bold red]Error:[/bold red] "
"['--table', '--dbt-list'] are mutually exclusive")
sys.exit(1)

# Search dbt project config files
dbt_project_dir = kwargs.get('dbt_project_dir')
no_auto_search = kwargs.get('no_auto_search')
dbt_project_path = DbtUtil.get_dbt_project_path(dbt_project_dir, no_auto_search, recursive=False)
dbt_profiles_dir = kwargs.get('dbt_profiles_dir')
if dbt_project_path:
working_dir = os.path.dirname(dbt_project_path) if dbt_project_path.endswith('.yml') else dbt_project_path
FileSystem.set_working_directory(working_dir)
if dbt_profiles_dir:
FileSystem.set_dbt_profiles_dir(dbt_profiles_dir)
# Only run initializer when dbt project path is provided
Initializer.exec(dbt_project_path=dbt_project_path, dbt_profiles_dir=dbt_profiles_dir, interactive=False)
elif is_piperider_workspace_exist() is False:
raise DbtProjectNotFoundError()

dbt_resources = None
if select and dbt_list is True:
raise PipeRiderConflictOptionsError(
'Cannot use options "--select" with "--dbt-list"',
hint='Remove "--select" option and use "--dbt-list" instead.'
)

if dbt_list:
dbt_resources = DbtUtil.read_dbt_resources(sys.stdin)
if env_dbt_resources is not None:
dbt_resources = DbtUtil.read_dbt_resources(env_dbt_resources)

ret = Runner.exec(datasource=datasource,
table=table,
output=output,
skip_report=skip_report,
dbt_target_path=dbt_target_path,
dbt_resources=dbt_resources,
dbt_select=select,
dbt_state=state,
report_dir=kwargs.get('report_dir'),
skip_datasource_connection=kwargs.get('skip_datasource'))
if ret in (0, EC_ERR_TEST_FAILED, EC_WARN_NO_PROFILED_MODULES):
if enable_share:
force_upload = True

auto_upload = CloudConnectorHelper.is_auto_upload()
is_cloud_view = (force_upload or auto_upload)

if not skip_report:
GenerateReport.exec(None, kwargs.get('report_dir'), output, open_report, is_cloud_view)

if ret == EC_WARN_NO_PROFILED_MODULES:
# No module was profiled
if dbt_list or dbt_resources or select:
Guide().show('No resources was profiled. Please check given "--select", "--dbt-list" option or '
'environment variable "PIPERIDER_DBT_RESOURCES" to choose the resources to profile.')
ret = 0

if CloudConnectorHelper.is_login() and is_cloud_view:
ret = CloudConnectorHelper.upload_latest_report(report_dir=kwargs.get('report_dir'), debug=kwargs.get('debug'),
open_report=open_report, enable_share=enable_share,
project_name=project_name)
elif not CloudConnectorHelper.is_login() and is_cloud_view:
console = Console()
console.print('[bold yellow]Warning: [/bold yellow]The report is not uploaded due to not logged in.')
"""
Profile data source, run assertions, and generate report(s). By default, the raw results and reports are saved in ".piperider/outputs".
"""

if ret != 0:
if ret != EC_WARN_NO_PROFILED_MODULES:
sys.exit(ret)
return ret
from piperider_cli.cli_utils.run_cmd import run as cmd
return cmd(**kwargs)


@cli.command(short_help='Generate recommended assertions. - Deprecated', cls=TrackCommand)
Expand Down
111 changes: 111 additions & 0 deletions piperider_cli/cli_utils/run_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import os
import sys

from rich.console import Console


def run(**kwargs):
'Profile data source, run assertions, and generate report(s). By default, the raw results and reports are saved in ".piperider/outputs".'

from piperider_cli.cli_utils import DbtUtil
from piperider_cli.cli_utils.cloud import CloudConnectorHelper
from piperider_cli.configuration import FileSystem, is_piperider_workspace_exist
from piperider_cli.error import DbtProjectNotFoundError, PipeRiderConflictOptionsError
from piperider_cli.exitcode import EC_ERR_TEST_FAILED, EC_WARN_NO_PROFILED_MODULES
from piperider_cli.generate_report import GenerateReport
from piperider_cli.guide import Guide
from piperider_cli.initializer import Initializer
from piperider_cli.runner import Runner

datasource = kwargs.get('datasource')
table = kwargs.get('table')
output = kwargs.get('output')
open_report = kwargs.get('open')
enable_share = kwargs.get('share')
skip_report = kwargs.get('skip_report')
dbt_target_path = kwargs.get('dbt_target_path')
dbt_list = kwargs.get('dbt_list')
force_upload = kwargs.get('upload')
project_name = kwargs.get('project')
select = kwargs.get('select')
state = kwargs.get('state')

if project_name is not None:
os.environ.get('PIPERIDER_API_PROJECT')

Check warning on line 34 in piperider_cli/cli_utils/run_cmd.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/cli_utils/run_cmd.py#L34

Added line #L34 was not covered by tests

console = Console()
env_dbt_resources = os.environ.get('PIPERIDER_DBT_RESOURCES')

# True -> 1, False -> 0
if sum([True if table else False, dbt_list, env_dbt_resources is not None]) > 1:
console.print("[bold red]Error:[/bold red] "
"['--table', '--dbt-list'] are mutually exclusive")
sys.exit(1)

# Search dbt project config files
dbt_project_dir = kwargs.get('dbt_project_dir')
no_auto_search = kwargs.get('no_auto_search')
dbt_project_path = DbtUtil.get_dbt_project_path(dbt_project_dir, no_auto_search, recursive=False)
dbt_profiles_dir = kwargs.get('dbt_profiles_dir')
if dbt_project_path:
working_dir = os.path.dirname(dbt_project_path) if dbt_project_path.endswith('.yml') else dbt_project_path
FileSystem.set_working_directory(working_dir)
if dbt_profiles_dir:
FileSystem.set_dbt_profiles_dir(dbt_profiles_dir)
# Only run initializer when dbt project path is provided
Initializer.exec(dbt_project_path=dbt_project_path, dbt_profiles_dir=dbt_profiles_dir, interactive=False)
elif is_piperider_workspace_exist() is False:
raise DbtProjectNotFoundError()

Check warning on line 58 in piperider_cli/cli_utils/run_cmd.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/cli_utils/run_cmd.py#L57-L58

Added lines #L57 - L58 were not covered by tests

dbt_resources = None
if select and dbt_list is True:
raise PipeRiderConflictOptionsError(
'Cannot use options "--select" with "--dbt-list"',
hint='Remove "--select" option and use "--dbt-list" instead.'
)

if dbt_list:
dbt_resources = DbtUtil.read_dbt_resources(sys.stdin)

Check warning on line 68 in piperider_cli/cli_utils/run_cmd.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/cli_utils/run_cmd.py#L68

Added line #L68 was not covered by tests
if env_dbt_resources is not None:
dbt_resources = DbtUtil.read_dbt_resources(env_dbt_resources)

Check warning on line 70 in piperider_cli/cli_utils/run_cmd.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/cli_utils/run_cmd.py#L70

Added line #L70 was not covered by tests

ret = Runner.exec(datasource=datasource,
table=table,
output=output,
skip_report=skip_report,
dbt_target_path=dbt_target_path,
dbt_resources=dbt_resources,
dbt_select=select,
dbt_state=state,
report_dir=kwargs.get('report_dir'),
skip_datasource_connection=kwargs.get('skip_datasource'))
if ret in (0, EC_ERR_TEST_FAILED, EC_WARN_NO_PROFILED_MODULES):
if enable_share:
force_upload = True

Check warning on line 84 in piperider_cli/cli_utils/run_cmd.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/cli_utils/run_cmd.py#L84

Added line #L84 was not covered by tests

auto_upload = CloudConnectorHelper.is_auto_upload()
is_cloud_view = (force_upload or auto_upload)

if not skip_report:
GenerateReport.exec(None, kwargs.get('report_dir'), output, open_report, is_cloud_view)

if ret == EC_WARN_NO_PROFILED_MODULES:
# No module was profiled
if dbt_list or dbt_resources or select:
Guide().show('No resources was profiled. Please check given "--select", "--dbt-list" option or '

Check warning on line 95 in piperider_cli/cli_utils/run_cmd.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/cli_utils/run_cmd.py#L94-L95

Added lines #L94 - L95 were not covered by tests
'environment variable "PIPERIDER_DBT_RESOURCES" to choose the resources to profile.')
ret = 0

Check warning on line 97 in piperider_cli/cli_utils/run_cmd.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/cli_utils/run_cmd.py#L97

Added line #L97 was not covered by tests

if CloudConnectorHelper.is_login() and is_cloud_view:
ret = CloudConnectorHelper.upload_latest_report(report_dir=kwargs.get('report_dir'),

Check warning on line 100 in piperider_cli/cli_utils/run_cmd.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/cli_utils/run_cmd.py#L100

Added line #L100 was not covered by tests
debug=kwargs.get('debug'),
open_report=open_report, enable_share=enable_share,
project_name=project_name)
elif not CloudConnectorHelper.is_login() and is_cloud_view:
console = Console()
console.print('[bold yellow]Warning: [/bold yellow]The report is not uploaded due to not logged in.')

Check warning on line 106 in piperider_cli/cli_utils/run_cmd.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/cli_utils/run_cmd.py#L105-L106

Added lines #L105 - L106 were not covered by tests

if ret != 0:
if ret != EC_WARN_NO_PROFILED_MODULES:
sys.exit(ret)

Check warning on line 110 in piperider_cli/cli_utils/run_cmd.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/cli_utils/run_cmd.py#L109-L110

Added lines #L109 - L110 were not covered by tests
return ret

0 comments on commit b8cf411

Please sign in to comment.