Skip to content

Commit c2bcfe0

Browse files
🛡️ Sentinel: Refactor rename script to use click and named arguments
- Updated `scripts/rename.py` to use the `click` library for robust argument parsing. - Refactored Makefile to pass project initialization parameters as named options. - Maintained security fixes and input validation.
1 parent 8d6350d commit c2bcfe0

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ help: # Show help
1212

1313
.PHONY: project
1414
project: # Rename project (run once)
15-
@uv run scripts/rename.py '$(subst ','\'',$(NAME))' '$(subst ','\'',$(DESCRIPTION))' '$(subst ','\'',$(AUTHOR))' '$(subst ','\'',$(EMAIL))' '$(subst ','\'',$(GITHUB))'
15+
@uv run scripts/rename.py \
16+
--name '$(subst ','\'',$(NAME))' \
17+
--description '$(subst ','\'',$(DESCRIPTION))' \
18+
--author '$(subst ','\'',$(AUTHOR))' \
19+
--email '$(subst ','\'',$(EMAIL))' \
20+
--github '$(subst ','\'',$(GITHUB))'
1621

1722
uv: # Install uv
1823
pipx install -f uv

scripts/rename.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,33 @@
11
import os
22
import re
33
import shutil
4-
import sys
54
from pathlib import Path
65

6+
import click
77

8-
def main():
9-
if len(sys.argv) != 6:
10-
print("Usage: python3 project_init.py <NAME> <DESCRIPTION> <AUTHOR> <EMAIL> <GITHUB>")
11-
sys.exit(1)
12-
13-
name = sys.argv[1]
14-
description = sys.argv[2]
15-
author = sys.argv[3]
16-
email = sys.argv[4]
17-
github = sys.argv[5]
188

9+
@click.command()
10+
@click.option("--name", required=True, help="Project new name")
11+
@click.option("--description", required=True, help="Project short description")
12+
@click.option("--author", required=True, help="Author name")
13+
@click.option("--email", required=True, help="Author email")
14+
@click.option("--github", required=True, help="GitHub username")
15+
def main(name: str, description: str, author: str, email: str, github: str):
1916
# Validate name to prevent directory traversal or other injection
2017
if not re.match(r"^[a-zA-Z0-9_-]+$", name):
21-
print(
22-
f"Error: Invalid project name '{name}'. Only alphanumeric characters, dashes, and underscores are allowed."
18+
raise click.UsageError(
19+
f"Invalid project name '{name}'. Only alphanumeric characters, dashes, and underscores are allowed."
2320
)
24-
sys.exit(1)
2521

2622
source = name.replace("-", "_").lower()
2723

28-
print(f"Initializing project '{name}' (source: '{source}')...")
24+
click.echo(f"Initializing project '{name}' (source: '{source}')...")
2925

3026
# 1. Rename project directory
3127
if os.path.isdir("project"):
3228
shutil.move("project", source)
3329
elif not os.path.isdir(source):
34-
print(f"Error: Neither 'project' nor '{source}' directory found.")
35-
sys.exit(1)
30+
raise click.ClickException(f"Error: Neither 'project' nor '{source}' directory found.")
3631

3732
# 2. File modifications
3833
replacements = [
@@ -52,15 +47,15 @@ def main():
5247
for filepath, pattern, replacement in replacements:
5348
path = Path(filepath)
5449
if not path.exists():
55-
print(f"Warning: File {filepath} not found, skipping.")
50+
click.echo(f"Warning: File {filepath} not found, skipping.")
5651
continue
5752

5853
content = path.read_text()
5954
new_content = re.sub(pattern, replacement, content, flags=re.MULTILINE)
6055
path.write_text(new_content)
61-
print(f"Updated {filepath}")
56+
click.echo(f"Updated {filepath}")
6257

63-
print("Project initialization complete.")
58+
click.echo("Project initialization complete.")
6459

6560

6661
if __name__ == "__main__":

0 commit comments

Comments
 (0)