Skip to content

Commit bec30c8

Browse files
authored
refactoring
1 parent 515d3ed commit bec30c8

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

db_diff/cli.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ def load(filename: str, key: str, input_format: str, encoding: str = "utf-8") ->
114114
logger.error(f"Error loading file {filename}: {str(e)}")
115115
raise click.ClickException(f"Failed to load '{filename}': {e}")
116116

117-
def generate_output_filename(previous: str, current: str, output_file: Optional[str],
118-
output_path: Optional[str], timestamp: bool = False) -> str:
117+
def generate_output_filename(previous: str, current: str, output_file: Optional[str], output_path: Optional[str], timestamp: bool = False) -> str:
119118
"""
120119
Generate an output filename for the diff result.
121120
@@ -132,33 +131,36 @@ def generate_output_filename(previous: str, current: str, output_file: Optional[
132131
Raises:
133132
click.ClickException: If the output path doesn't exist
134133
"""
135-
timestamp_prefix = ""
136-
if timestamp:
137-
timestamp_str = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
138-
timestamp_prefix = f"{timestamp_str}_"
139-
140134
# Extract base filenames without extensions for the comparison files
141135
prev_basename = os.path.splitext(os.path.basename(previous))[0]
142136
curr_basename = os.path.splitext(os.path.basename(current))[0]
143137

144-
if not output_file:
145-
# Default output filename
146-
output_file = f"{timestamp_prefix}diffs_{prev_basename}__vs__{curr_basename}.json"
147-
else:
148-
# Remove .json if present to avoid double extension
138+
# Build the base filename
139+
if output_file:
140+
# Remove .json extension if present to avoid double extension
149141
if output_file.lower().endswith('.json'):
150142
output_file = output_file[:-5]
151-
output_file = f"{timestamp_prefix}diffs_{output_file}.json"
143+
base_filename = f"diffs_{output_file}.json"
144+
else:
145+
# Default output filename
146+
base_filename = f"diffs_{prev_basename}__vs__{curr_basename}.json"
152147

153-
# Handle output path
148+
# Add timestamp prefix if enabled
149+
if timestamp:
150+
timestamp_str = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
151+
base_filename = f"{timestamp_str}_{base_filename}"
152+
153+
# Resolve the final path
154154
if output_path:
155155
if not os.path.isdir(output_path):
156156
logger.error(f"Output path '{output_path}' does not exist")
157157
raise click.ClickException(f"Output path '{output_path}' does not exist or is not a directory.")
158-
output_file = os.path.join(output_path, os.path.basename(output_file))
158+
full_path = os.path.join(output_path, base_filename)
159+
else:
160+
full_path = base_filename
159161

160-
logger.debug(f"Generated output filename: {output_file}")
161-
return output_file
162+
logger.debug(f"Generated output filename: {full_path}")
163+
return full_path
162164

163165
@click.command(
164166
context_settings={

0 commit comments

Comments
 (0)