Skip to content

Commit 6d480de

Browse files
committed
intro message on output
1 parent 51d0d03 commit 6d480de

3 files changed

Lines changed: 55 additions & 45 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ The format is (loosely) based on [Keep a Changelog](http://keepachangelog.com/)
1212

1313
- Results summary for options that produce numerous results, ie. --collections, --item-collection, --recursive ([#138](https://github.com/stac-utils/stac-check/pull/138))
1414
- Support for --verbose flag to show verbose results summary ([#138](https://github.com/stac-utils/stac-check/pull/138))
15-
- Added `--output`/`-o` option to save validation results to a file ([#139](https://github.com/stac-utils/stac-check/pull/139))
15+
- Added `--output`/`-o` option to save validation results to a file ([#138](https://github.com/stac-utils/stac-check/pull/138))
16+
- Tests for CLI options ([#138](https://github.com/stac-utils/stac-check/pull/138))
1617

1718
## [v1.10.1] - 2025-06-21
1819

stac_check/cli.py

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
recursive_message,
1414
)
1515
from stac_check.lint import Linter
16+
from stac_check.utilities import handle_output
1617

1718

1819
@click.option(
@@ -152,32 +153,11 @@ def main(
152153
verbose=verbose,
153154
)
154155

155-
# Handle output to file if specified
156-
if output:
157-
original_stdout = sys.stdout
158-
try:
159-
with open(output, "w") as f:
160-
sys.stdout = f
161-
intro_message(display_linter)
162-
if collections:
163-
collections_message(
164-
api_linter,
165-
results=results,
166-
cli_message_func=cli_message,
167-
verbose=verbose,
168-
)
169-
elif item_collection:
170-
item_collection_message(
171-
api_linter,
172-
results=results,
173-
cli_message_func=cli_message,
174-
verbose=verbose,
175-
)
176-
finally:
177-
sys.stdout = original_stdout
178-
click.echo(f"Output written to {output}", err=True)
179-
else:
180-
intro_message(display_linter)
156+
# Show intro message in the terminal
157+
intro_message(display_linter)
158+
159+
# Define output generation function (without intro message since we already showed it)
160+
def generate_output():
181161
if collections:
182162
collections_message(
183163
api_linter,
@@ -192,6 +172,9 @@ def main(
192172
cli_message_func=cli_message,
193173
verbose=verbose,
194174
)
175+
176+
# Handle output (without duplicating the intro message)
177+
handle_output(output, generate_output)
195178
sys.exit(0 if all(msg.get("valid_stac") is True for msg in results) else 1)
196179
else:
197180
# Handle file-based validation (single file or recursive)
@@ -209,23 +192,17 @@ def main(
209192

210193
intro_message(linter)
211194

212-
# Handle output to file if specified and recursive
213-
if output and recursive:
214-
original_stdout = sys.stdout
215-
try:
216-
with open(output, "w") as f:
217-
sys.stdout = f
218-
recursive_message(
219-
linter, cli_message_func=cli_message, verbose=verbose
220-
)
221-
finally:
222-
sys.stdout = original_stdout
223-
click.echo(f"Output written to {output}", err=True)
224-
# Handle recursive validation without output file
225-
elif recursive:
226-
recursive_message(linter, cli_message_func=cli_message, verbose=verbose)
227-
# Handle single file validation
228-
else:
229-
cli_message(linter)
195+
# Show intro message in the terminal
196+
intro_message(linter)
197+
198+
# Define output generation function (without intro message since we already showed it)
199+
def generate_output():
200+
if recursive:
201+
recursive_message(linter, cli_message_func=cli_message, verbose=verbose)
202+
else:
203+
cli_message(linter)
204+
205+
# Handle output (without duplicating the intro message)
206+
handle_output(output if recursive else None, generate_output)
230207

231208
sys.exit(0 if linter.valid_stac else 1)

stac_check/utilities.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import contextlib
2+
from typing import Callable
3+
4+
import click
5+
6+
17
def determine_asset_type(data):
28
"""Determine the STAC asset type from the given data dictionary.
39
@@ -35,6 +41,32 @@ def determine_asset_type(data):
3541
return ""
3642

3743

44+
def handle_output(
45+
output_file: str, callback: Callable[[], None], output_path: str = None
46+
) -> None:
47+
"""Helper function to handle output redirection to a file or stdout.
48+
49+
Args:
50+
output_file: Path to the output file, or None to use stdout
51+
callback: Function that performs the actual output generation
52+
output_path: Optional path to display in the success message
53+
"""
54+
55+
if output_file:
56+
with open(output_file, "w") as f:
57+
with contextlib.redirect_stdout(f):
58+
callback()
59+
click.secho(
60+
f"Output written to {output_path or output_file}",
61+
fg="green",
62+
err=True,
63+
bold=True,
64+
)
65+
click.secho()
66+
else:
67+
callback()
68+
69+
3870
def format_verbose_error(error_data):
3971
"""Format verbose error data into a human-readable string."""
4072
if not error_data or not isinstance(error_data, dict):

0 commit comments

Comments
 (0)