Skip to content

Fix: dbos init uses incorrect app name for GitHub templates #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
111 changes: 65 additions & 46 deletions dbos/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,55 +147,16 @@ def init(
] = False,
) -> None:
try:

git_templates = ["dbos-toolbox", "dbos-app-starter", "dbos-cron-starter"]
templates_dir = get_templates_directory()
templates = git_templates + [
x.name for x in os.scandir(templates_dir) if x.is_dir()
]

if config and template is None:
template = templates[-1]

if template:
if template not in templates:
raise Exception(f"Template {template} not found in {templates_dir}")
else:
print("\n[bold]Available templates:[/bold]")
for idx, template_name in enumerate(templates, 1):
print(f" {idx}. {template_name}")
while True:
try:
choice = IntPrompt.ask(
"\nSelect template number",
show_choices=False,
show_default=False,
)
if 1 <= choice <= len(templates):
template = templates[choice - 1]
break
else:
print(
"[red]Invalid selection. Please choose a number from the list.[/red]"
)
except (KeyboardInterrupt, EOFError):
raise typer.Abort()
except ValueError:
print("[red]Please enter a valid number.[/red]")

if template in git_templates:
project_name = template
else:
if project_name is None:
project_name = typing.cast(
str,
typer.prompt("What is your project's name?", get_project_name()),
)

if not _is_valid_app_name(project_name):
raise Exception(
f"{project_name} is an invalid DBOS app name. App names must be between 3 and 30 characters long and contain only lowercase letters, numbers, dashes, and underscores."
)
project_name, template = _resolve_project_name_and_template(
project_name=project_name,
template=template,
config=config,
git_templates=git_templates,
templates_dir=templates_dir,
)

if template in git_templates:
create_template_from_github(app_name=project_name, template_name=template)
Expand All @@ -207,6 +168,64 @@ def init(
print(f"[red]{e}[/red]")


def _resolve_project_name_and_template(
project_name: Optional[str],
template: Optional[str],
config: bool,
git_templates: list[str],
templates_dir: str,
) -> tuple[str, str]:
templates = git_templates + [
x.name for x in os.scandir(templates_dir) if x.is_dir()
]

if config and template is None:
template = templates[-1]

if template:
if template not in templates:
raise Exception(f"Template {template} not found in {templates_dir}")
else:
print("\n[bold]Available templates:[/bold]")
for idx, template_name in enumerate(templates, 1):
print(f" {idx}. {template_name}")
while True:
try:
choice = IntPrompt.ask(
"\nSelect template number",
show_choices=False,
show_default=False,
)
if 1 <= choice <= len(templates):
template = templates[choice - 1]
break
else:
print(
"[red]Invalid selection. Please choose a number from the list.[/red]"
)
except (KeyboardInterrupt, EOFError):
raise typer.Abort()
except ValueError:
print("[red]Please enter a valid number.[/red]")

if template in git_templates:
if project_name is None:
project_name = template
else:
if project_name is None:
project_name = typing.cast(
str,
typer.prompt("What is your project's name?", get_project_name()),
)

if not _is_valid_app_name(project_name):
raise Exception(
f"{project_name} is an invalid DBOS app name. App names must be between 3 and 30 characters long and contain only lowercase letters, numbers, dashes, and underscores."
)

return project_name, template


@app.command(
help="Run your database schema migrations using the migration commands in 'dbos-config.yaml'"
)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from dbos.cli.cli import _resolve_project_name_and_template, get_templates_directory


def test_resolve_project_name_and_template() -> None:
git_templates = ["dbos-toolbox", "dbos-app-starter", "dbos-cron-starter"]
templates_dir = get_templates_directory()

# dbos init my-app -t dbos-toolbox
project_name, template = _resolve_project_name_and_template(
project_name="my-app",
template="dbos-toolbox",
config=False,
git_templates=git_templates,
templates_dir=templates_dir,
)
assert project_name == "my-app"
assert template == "dbos-toolbox"

# dbos init -t dbos-toolbox
project_name, template = _resolve_project_name_and_template(
project_name=None,
template="dbos-toolbox",
config=False,
git_templates=git_templates,
templates_dir=templates_dir,
)
assert project_name == "dbos-toolbox"
assert template == "dbos-toolbox"
Loading