Skip to content

Commit 0bcf98d

Browse files
committed
Add an errors-only output format
1 parent 4c5ff35 commit 0bcf98d

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

organize/cli.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
Options:
3131
<config> A config name or path to a config file
3232
-W --working-dir <dir> The working directory
33-
-F --format (DEFAULT|JSONL) The output format [Default: DEFAULT]
33+
-F --format (default|errorsonly|JSONL)
34+
The output format [Default: default]
3435
-T --tags <tags> Tags to run (eg. "initial,release")
3536
-S --skip-tags <tags> Tags to skip
3637
-h --help Show this help page.
@@ -72,7 +73,7 @@
7273

7374
Tags = Set[str]
7475
OutputFormat = Annotated[
75-
Literal["default", "jsonl"], BeforeValidator(lambda v: v.lower())
76+
Literal["default", "jsonl", "errorsonly"], BeforeValidator(lambda v: v.lower())
7677
]
7778

7879
console = Console()
@@ -84,6 +85,16 @@ def _open_uri(uri: str):
8485
webbrowser.open(uri)
8586

8687

88+
def _output_for_format(format: OutputFormat) -> Output:
89+
if format == "default":
90+
return Default()
91+
elif format == "errorsonly":
92+
return Default(errors_only=True)
93+
elif format == "jsonl":
94+
return JSONL()
95+
raise ValueError(f"{format} is not a valid output format.")
96+
97+
8798
def execute(
8899
config: Optional[str],
89100
working_dir: Optional[Path],
@@ -92,12 +103,10 @@ def execute(
92103
skip_tags: Tags,
93104
simulate: bool,
94105
) -> None:
95-
output = JSONL() if format == "jsonl" else Default()
96-
assert isinstance(output, Output)
97106
config_path = find_config(name_or_path=config)
98107
Config.from_path(config_path).execute(
99108
simulate=simulate,
100-
output=output,
109+
output=_output_for_format(format),
101110
tags=tags,
102111
skip_tags=skip_tags,
103112
working_dir=working_dir or Path("."),

organize/output/default.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from .output import Level
1616

1717
if TYPE_CHECKING:
18+
from typing import List
19+
1820
from organize.resource import Resource
1921

2022
from ._sender import SenderType
@@ -51,7 +53,7 @@ def set_error_msg(cls, msg: str) -> None:
5153

5254

5355
class Default:
54-
def __init__(self, theme: Optional[Theme] = None):
56+
def __init__(self, theme: Optional[Theme] = None, errors_only: bool = False):
5557
if theme is None:
5658
theme = Theme(
5759
{
@@ -74,6 +76,10 @@ def __init__(self, theme: Optional[Theme] = None):
7476
"summary.fail": "red",
7577
}
7678
)
79+
self.errors_only = errors_only
80+
self.msg_queue: List[str] = []
81+
self.det_resource = ChangeDetector()
82+
7783
self.console = Console(theme=theme, highlight=False)
7884

7985
self.status = Status("", console=self.console)
@@ -141,14 +147,25 @@ def msg(
141147
sender: SenderType,
142148
level: Level = "info",
143149
) -> None:
144-
self.show_resource(res)
145150
msg_pre = format_sender(sender=sender, standalone=res.path is None)
146151
if level == "info":
147-
self.console.print(f"{msg_pre}{format_info(msg=msg)}")
148-
elif level == "error":
149-
self.console.print(f"{msg_pre}{format_error(msg=msg)}")
152+
msg = f"{msg_pre}{format_info(msg=msg)}"
150153
elif level == "warn":
151-
self.console.print(f"{msg_pre}{format_warn(msg=msg)}")
154+
msg = f"{msg_pre}{format_warn(msg=msg)}"
155+
elif level == "error":
156+
msg = f"{msg_pre}{format_error(msg=msg)}"
157+
158+
if self.errors_only:
159+
if self.det_resource.changed(res):
160+
self.msg_queue.clear()
161+
self.msg_queue.append(msg)
162+
if level == "error":
163+
self.show_resource(res)
164+
for msg in self.msg_queue:
165+
self.console.print(msg)
166+
else:
167+
self.show_resource(res)
168+
self.console.print(msg)
152169

153170
def confirm(
154171
self,

0 commit comments

Comments
 (0)