-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.py
121 lines (104 loc) · 3.1 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import dataclasses
import sys
from typing import List, Type
import click
from git_limiter import constants
from git_limiter.backend.base import GitBackend
from git_limiter.backend.git_subprocess_backend import GitSubprocessBackend
from git_limiter.checks.base import GitCheck
from git_limiter.checks.max_diff import MaxChangedFilesCheck, MaxDeletionsCheck, MaxInsertionsCheck
from git_limiter.config.settings import Settings
from git_limiter.stats import collect_git_stats
from git_limiter.terminal.rich_terminal import RichTerminal
@dataclasses.dataclass
class CLIArgs:
compared_branch: str
max_insertions: int
max_deletions: int
max_changed_files: int
config: str
def _make_decision(check_results: List[bool]) -> int:
"""Complete program with either error or success code."""
if all(check_results):
return constants.SUCCESS_CODE
else:
return constants.ERROR_CODE
def _run_app(cli_args: CLIArgs):
settings = Settings(
compared_branch=cli_args.compared_branch,
max_insertions=cli_args.max_insertions,
max_deletions=cli_args.max_deletions,
max_changed_files=cli_args.max_changed_files,
)
settings.override_from_config_file(config_path=cli_args.config)
git_backend: GitBackend = GitSubprocessBackend(
settings=settings,
)
check_classes: List[Type[GitCheck]] = [
MaxChangedFilesCheck,
MaxInsertionsCheck,
MaxDeletionsCheck,
]
collected_stats = collect_git_stats(
git_backend=git_backend,
)
checks: List[GitCheck] = [
check_class(
collected_stats=collected_stats,
settings=settings,
terminal=RichTerminal(),
)
for check_class in check_classes
]
check_results: List[bool] = []
for check in checks:
check_result = check.run()
check_results.append(check_result)
return_code = _make_decision(check_results=check_results)
sys.exit(return_code)
@click.command(name="git-limiter")
@click.option(
"--compared-branch",
default=constants.DEFAULT_COMPARED_BRANCH,
type=str,
help="Target branch to compare your current changes with.",
)
@click.option(
"--max-insertions",
default=constants.DEFAULT_MAX_INSERTIONS,
type=int,
help="Maximum number of insertions",
)
@click.option(
"--max-deletions",
default=constants.DEFAULT_MAX_DELETIONS,
type=int,
help="Maximum number of deletions",
)
@click.option(
"--max-changed-files",
default=constants.DEFAULT_MAX_CHANGED_FILES,
type=int,
help="Maximum number of changed files",
)
@click.option(
"--config",
default=constants.DEFAULT_CONFIG,
type=str,
help="Path to config(pyproject.toml only supported)",
)
def run(
compared_branch: str,
max_insertions: int,
max_deletions: int,
max_changed_files: int,
config: str,
) -> None:
cli_args = CLIArgs(
compared_branch=compared_branch,
max_insertions=max_insertions,
max_deletions=max_deletions,
max_changed_files=max_changed_files,
config=config,
)
_run_app(cli_args=cli_args)