Skip to content

Commit 327eb0a

Browse files
tahsintunanqianl15
andauthored
Fix: dbos init uses incorrect app name for GitHub templates (#344)
Fixes #273 --------- Co-authored-by: Qian Li <liqian.cs@gmail.com>
1 parent 94960c2 commit 327eb0a

File tree

2 files changed

+98
-47
lines changed

2 files changed

+98
-47
lines changed

dbos/cli/cli.py

Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import typer
1313
from rich import print
1414
from rich.prompt import IntPrompt
15-
from typing_extensions import Annotated
15+
from typing_extensions import Annotated, List
1616

1717
from dbos._debug import debug_workflow, parse_start_command
1818

@@ -147,55 +147,16 @@ def init(
147147
] = False,
148148
) -> None:
149149
try:
150-
151150
git_templates = ["dbos-toolbox", "dbos-app-starter", "dbos-cron-starter"]
152151
templates_dir = get_templates_directory()
153-
templates = git_templates + [
154-
x.name for x in os.scandir(templates_dir) if x.is_dir()
155-
]
156-
157-
if config and template is None:
158-
template = templates[-1]
159152

160-
if template:
161-
if template not in templates:
162-
raise Exception(f"Template {template} not found in {templates_dir}")
163-
else:
164-
print("\n[bold]Available templates:[/bold]")
165-
for idx, template_name in enumerate(templates, 1):
166-
print(f" {idx}. {template_name}")
167-
while True:
168-
try:
169-
choice = IntPrompt.ask(
170-
"\nSelect template number",
171-
show_choices=False,
172-
show_default=False,
173-
)
174-
if 1 <= choice <= len(templates):
175-
template = templates[choice - 1]
176-
break
177-
else:
178-
print(
179-
"[red]Invalid selection. Please choose a number from the list.[/red]"
180-
)
181-
except (KeyboardInterrupt, EOFError):
182-
raise typer.Abort()
183-
except ValueError:
184-
print("[red]Please enter a valid number.[/red]")
185-
186-
if template in git_templates:
187-
project_name = template
188-
else:
189-
if project_name is None:
190-
project_name = typing.cast(
191-
str,
192-
typer.prompt("What is your project's name?", get_project_name()),
193-
)
194-
195-
if not _is_valid_app_name(project_name):
196-
raise Exception(
197-
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."
198-
)
153+
project_name, template = _resolve_project_name_and_template(
154+
project_name=project_name,
155+
template=template,
156+
config=config,
157+
git_templates=git_templates,
158+
templates_dir=templates_dir,
159+
)
199160

200161
if template in git_templates:
201162
create_template_from_github(app_name=project_name, template_name=template)
@@ -207,6 +168,67 @@ def init(
207168
print(f"[red]{e}[/red]")
208169

209170

171+
def _resolve_project_name_and_template(
172+
project_name: Optional[str],
173+
template: Optional[str],
174+
config: bool,
175+
git_templates: List[str],
176+
templates_dir: str,
177+
) -> tuple[str, str]:
178+
templates = git_templates + [
179+
x.name for x in os.scandir(templates_dir) if x.is_dir()
180+
]
181+
182+
if config and template is None:
183+
template = templates[-1]
184+
185+
if template:
186+
if template not in templates:
187+
raise Exception(f"Template {template} not found in {templates_dir}")
188+
else:
189+
print("\n[bold]Available templates:[/bold]")
190+
for idx, template_name in enumerate(templates, 1):
191+
print(f" {idx}. {template_name}")
192+
while True:
193+
try:
194+
choice = IntPrompt.ask(
195+
"\nSelect template number",
196+
show_choices=False,
197+
show_default=False,
198+
)
199+
if 1 <= choice <= len(templates):
200+
template = templates[choice - 1]
201+
break
202+
else:
203+
print(
204+
"[red]Invalid selection. Please choose a number from the list.[/red]"
205+
)
206+
except (KeyboardInterrupt, EOFError):
207+
raise typer.Abort()
208+
except ValueError:
209+
print("[red]Please enter a valid number.[/red]")
210+
211+
if template in git_templates:
212+
if project_name is None:
213+
project_name = template
214+
else:
215+
if project_name is None:
216+
project_name = typing.cast(
217+
str,
218+
typer.prompt("What is your project's name?", get_project_name()),
219+
)
220+
221+
if not _is_valid_app_name(project_name):
222+
raise Exception(
223+
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."
224+
)
225+
226+
assert project_name is not None, "Project name cannot be None"
227+
assert template is not None, "Template name cannot be None"
228+
229+
return project_name, template
230+
231+
210232
@app.command(
211233
help="Run your database schema migrations using the migration commands in 'dbos-config.yaml'"
212234
)

tests/test_cli.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from dbos.cli._template_init import get_templates_directory
2+
from dbos.cli.cli import _resolve_project_name_and_template
3+
4+
5+
def test_resolve_project_name_and_template() -> None:
6+
git_templates = ["dbos-toolbox", "dbos-app-starter", "dbos-cron-starter"]
7+
templates_dir = get_templates_directory()
8+
9+
# dbos init my-app -t dbos-toolbox
10+
project_name, template = _resolve_project_name_and_template(
11+
project_name="my-app",
12+
template="dbos-toolbox",
13+
config=False,
14+
git_templates=git_templates,
15+
templates_dir=templates_dir,
16+
)
17+
assert project_name == "my-app"
18+
assert template == "dbos-toolbox"
19+
20+
# dbos init -t dbos-toolbox
21+
project_name, template = _resolve_project_name_and_template(
22+
project_name=None,
23+
template="dbos-toolbox",
24+
config=False,
25+
git_templates=git_templates,
26+
templates_dir=templates_dir,
27+
)
28+
assert project_name == "dbos-toolbox"
29+
assert template == "dbos-toolbox"

0 commit comments

Comments
 (0)