|
1 | 1 | import click
|
2 | 2 | import json as std_json
|
3 | 3 | import time as time_module
|
| 4 | +import os |
| 5 | +import datetime |
4 | 6 | from . import load_csv, load_json, compare, human_text
|
5 | 7 |
|
6 | 8 | @click.command(
|
7 | 9 | context_settings={"help_option_names": ["-h", "--help"]},
|
8 | 10 | help="Compare the differences between two CSV or JSON files to find differences.",
|
9 |
| - epilog="Example: db-diff old.csv new.csv --key=Id --output=jsonfile --outputfile=diff.json", |
| 11 | + epilog="Example: db-diff old.csv new.csv --key=Id --output=jsonfile --outfilename=diff.json", |
10 | 12 | )
|
11 | 13 | @click.argument(
|
12 | 14 | "previous",
|
|
29 | 31 | help="Output format. Available: (readable|json|jsonfile).",
|
30 | 32 | )
|
31 | 33 | @click.option(
|
32 |
| - "--outputfile", |
| 34 | + "--outfilename", |
33 | 35 | "output_file",
|
34 | 36 | type=click.Path(file_okay=True, dir_okay=False, writable=True, resolve_path=True),
|
35 | 37 | default=None,
|
36 | 38 | help="File to write JSON output to (only used with --output=jsonfile).",
|
37 | 39 | )
|
| 40 | +@click.option( |
| 41 | + "--outfilepath", |
| 42 | + "output_path", |
| 43 | + type=click.Path(file_okay=False, dir_okay=True, writable=True, resolve_path=True), |
| 44 | + default=None, |
| 45 | + help="Directory to save the output file (only used with --output=jsonfile).", |
| 46 | +) |
38 | 47 | @click.option(
|
39 | 48 | "--showunchanged",
|
40 | 49 | "show_unchanged",
|
|
63 | 72 | @click.version_option()
|
64 | 73 | def cli(
|
65 | 74 | previous, current, key, input_format,
|
66 |
| - show_unchanged, encoding, show_time, output, output_file |
| 75 | + show_unchanged, encoding, show_time, output, output_file, output_path |
67 | 76 | ):
|
68 | 77 | """Compare the differences between two CSV or JSON files."""
|
69 | 78 | dialect = {
|
70 | 79 | "csv": "excel",
|
71 | 80 | "tsv": "excel-tab",
|
72 | 81 | }
|
73 | 82 |
|
74 |
| - if output == "jsonfile" and not output_file: |
75 |
| - raise click.UsageError( |
76 |
| - "--outputfile must be specified when --output=jsonfile", |
77 |
| - ctx=click.get_current_context(), |
78 |
| - ) |
| 83 | + # Set default output filename if needed |
| 84 | + if output == "jsonfile": |
| 85 | + if not output_file: |
| 86 | + timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") |
| 87 | + output_file = f"{timestamp}_diffs.json" |
| 88 | + if output_path: |
| 89 | + if not os.path.isdir(output_path): |
| 90 | + raise click.ClickException(f"Output path '{output_path}' does not exist or is not a directory.") |
| 91 | + output_file = os.path.join(output_path, os.path.basename(output_file)) |
79 | 92 |
|
80 | 93 | def sniff_key_column(filename, input_format):
|
81 | 94 | # Only for CSV/TSV/JSON
|
|
0 commit comments