Skip to content

Commit 515d3ed

Browse files
authored
fixed stupid code
1 parent 97e0528 commit 515d3ed

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

db_diff/cli.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ def load(filename: str, key: str, input_format: str, encoding: str = "utf-8") ->
115115
raise click.ClickException(f"Failed to load '{filename}': {e}")
116116

117117
def generate_output_filename(previous: str, current: str, output_file: Optional[str],
118-
output_path: Optional[str], timestamp: Optional[str] = None,
119-
use_timestamp: bool = True) -> str:
118+
output_path: Optional[str], timestamp: bool = False) -> str:
120119
"""
121120
Generate an output filename for the diff result.
122121
@@ -125,8 +124,7 @@ def generate_output_filename(previous: str, current: str, output_file: Optional[
125124
current: Path to the current file
126125
output_file: User-specified output filename (optional)
127126
output_path: User-specified output directory (optional)
128-
timestamp: Timestamp to use (optional)
129-
use_timestamp: Whether to include timestamp in filename (default: False)
127+
timestamp: Whether to include timestamp in filename (default: False)
130128
131129
Returns:
132130
Full path to the output file
@@ -135,10 +133,9 @@ def generate_output_filename(previous: str, current: str, output_file: Optional[
135133
click.ClickException: If the output path doesn't exist
136134
"""
137135
timestamp_prefix = ""
138-
if use_timestamp:
139-
if timestamp is None:
140-
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
141-
timestamp_prefix = f"{timestamp}_"
136+
if timestamp:
137+
timestamp_str = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
138+
timestamp_prefix = f"{timestamp_str}_"
142139

143140
# Extract base filenames without extensions for the comparison files
144141
prev_basename = os.path.splitext(os.path.basename(previous))[0]
@@ -148,11 +145,10 @@ def generate_output_filename(previous: str, current: str, output_file: Optional[
148145
# Default output filename
149146
output_file = f"{timestamp_prefix}diffs_{prev_basename}__vs__{curr_basename}.json"
150147
else:
151-
# Prepend the timestamp to the user-specified filename if needed
152-
output_file = f"{timestamp_prefix}diffs_{output_file}"
153-
# Ensure the output file has a .json extension
154-
if not output_file.endswith('.json'):
155-
output_file += '.json'
148+
# Remove .json if present to avoid double extension
149+
if output_file.lower().endswith('.json'):
150+
output_file = output_file[:-5]
151+
output_file = f"{timestamp_prefix}diffs_{output_file}.json"
156152

157153
# Handle output path
158154
if output_path:
@@ -177,8 +173,8 @@ def generate_output_filename(previous: str, current: str, output_file: Optional[
177173
"\n db-diff old.csv new.csv --key=Id\n"
178174
"\n Output to JSON file:\n"
179175
"\n db-diff old.csv new.csv --output=jsonfile --outfilename=diff.json\n"
180-
"\n Output without timestamp in filename:\n"
181-
"\n db-diff old.csv new.csv --output=jsonfile --timestamp=false\n"
176+
"\n Output with timestamp in filename:\n"
177+
"\n db-diff old.csv new.csv --output=jsonfile --timestamp\n"
182178
"\n Compare only specific fields:\n"
183179
"\n db-diff old.csv new.csv --fields=Id,Name,Email\n"
184180
"\n Ignore specific fields:\n"
@@ -275,9 +271,7 @@ def generate_output_filename(previous: str, current: str, output_file: Optional[
275271
)
276272
@click.option(
277273
"--timestamp",
278-
type=click.Choice(["true", "false"], case_sensitive=False),
279-
default="false",
280-
show_default=True,
274+
is_flag=True,
281275
help="Add timestamp to filename: YYYY-MM-DD_HH-MM-SS_diffs_<filename>.json",
282276
)
283277
@click.version_option()
@@ -339,8 +333,7 @@ def cli(
339333
# Set default output filename if needed
340334
if output == "jsonfile":
341335
try:
342-
use_timestamp = timestamp.lower() == "true"
343-
output_file = generate_output_filename(previous, current, output_file, output_path, use_timestamp=use_timestamp)
336+
output_file = generate_output_filename(previous, current, output_file, output_path, timestamp)
344337
except Exception as e:
345338
raise click.ClickException(f"Failed to generate output filename: {e}")
346339

0 commit comments

Comments
 (0)