Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions src/lic_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ def get_license_key_interactive(licenses):
return keys[[licenses[k]["name"] for k in keys].index(selected)]


def get_author_input(provided_author=None):
"""Get author from arguments or interactive prompt."""
def get_author_input(provided_author=None, non_interactive=False):
"""Get author from arguments, git config, or interactive prompt."""
if provided_author:
console.print(f"[green]✓ Author: {provided_author}[/green]")
return provided_author

if non_interactive:
git_name = get_git_name()
if git_name:
console.print(f"[green]✓ Author: {git_name} (from git config)[/green]")
return git_name
raise ValueError("Author required in non-interactive mode (use --author or set git user.name)")

git_name = get_git_name()
author = questionary.text(
"Author:",
Expand All @@ -82,12 +89,17 @@ def get_author_input(provided_author=None):
return author


def get_year_input(provided_year=None):
def get_year_input(provided_year=None, non_interactive=False):
"""Get year from arguments or interactive prompt."""
if provided_year:
console.print(f"[green]✓ Year: {provided_year}[/green]")
return provided_year

if non_interactive:
current_year = str(datetime.now().year)
console.print(f"[green]✓ Year: {current_year} (current year)[/green]")
return current_year

year = questionary.text(
"Year:",
default=str(datetime.now().year),
Expand Down Expand Up @@ -129,14 +141,41 @@ def main():
help="Year for the license",
metavar="YEAR"
)
parser.add_argument(
"-n", "--non-interactive",
action="store_true",
help="Disable interactive prompts; --license required; --author optional (uses git config if available); --year defaults to current year"
)
parser.add_argument(
"--force",
action="store_true",
help="Overwrite LICENSE file if it exists"
)
args = parser.parse_args()

try:
# Check for existing LICENSE file
license_path = Path("LICENSE")
if license_path.exists() and not args.force:
if args.non_interactive:
raise FileExistsError("LICENSE already exists. Use --force to overwrite.")
else:
overwrite = questionary.confirm(
"LICENSE file already exists. Overwrite?",
default=False
).ask()
print("\033[A\033[K", end="")
if not overwrite:
console.print("[yellow]Cancelled[/yellow]")
return

# Get license key
if args.license:
key = args.license
console.print(f"[green]✓ {key}[/green]")
else:
if args.non_interactive:
raise ValueError("License required in non-interactive mode (use --license)")
console.print("[bold]Loading licenses...[/bold]")
with console.status("[bold cyan]Fetching from GitHub...", spinner="dots"):
licenses = fetch_licenses()
Expand All @@ -148,8 +187,8 @@ def main():
console.print(f"[green]✓ {licenses[key]['name']}[/green]")

# Get author and year
author = get_author_input(args.author)
year = get_year_input(args.year)
author = get_author_input(args.author, args.non_interactive)
year = get_year_input(args.year, args.non_interactive)

# Generate and save license
with console.status("[bold cyan]Generating license..."):
Expand Down