Skip to content

Commit 4ccd4ed

Browse files
Specify language on 'codegen init' in CLI (#289)
Co-authored-by: codegen-team <[email protected]>
1 parent 14198fb commit 4ccd4ed

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/codegen/cli/commands/init/main.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
from codegen.cli.commands.init.render import get_success_message
1111
from codegen.cli.git.url import get_git_organization_and_repo
1212
from codegen.cli.rich.codeblocks import format_command
13+
from codegen.cli.utils.constants import ProgrammingLanguage
1314
from codegen.cli.workspace.initialize_workspace import initialize_codegen
1415

1516

1617
@click.command(name="init")
1718
@click.option("--repo-name", type=str, help="The name of the repository")
1819
@click.option("--organization-name", type=str, help="The name of the organization")
1920
@click.option("--fetch-docs", is_flag=True, help="Fetch docs and examples (requires auth)")
20-
def init_command(repo_name: str | None = None, organization_name: str | None = None, fetch_docs: bool = False):
21+
@click.option("--language", type=click.Choice(["python", "typescript"], case_sensitive=False), help="Override automatic language detection")
22+
def init_command(repo_name: str | None = None, organization_name: str | None = None, fetch_docs: bool = False, language: str | None = None):
2123
"""Initialize or update the Codegen folder."""
2224
# Print a message if not in a git repo
2325
try:
@@ -31,6 +33,11 @@ def init_command(repo_name: str | None = None, organization_name: str | None = N
3133
rich.print(format_command("codegen init"))
3234
sys.exit(1)
3335

36+
# Convert language string to enum if provided
37+
programming_language = None
38+
if language:
39+
programming_language = ProgrammingLanguage[language.upper()]
40+
3441
# Only create session if we need to fetch docs
3542
session = CodegenSession() if fetch_docs else None
3643
codegen_dir = Path.cwd() / CODEGEN_DIR if not session else session.codegen_dir
@@ -46,11 +53,13 @@ def init_command(repo_name: str | None = None, organization_name: str | None = N
4653
cwd_org, cwd_repo = get_git_organization_and_repo(session.git_repo)
4754
session.config.organization_name = session.config.organization_name or cwd_org
4855
session.config.repo_name = session.config.repo_name or cwd_repo
56+
if programming_language:
57+
session.config.programming_language = programming_language
4958
session.write_config()
5059

5160
action = "Updating" if is_update else "Initializing"
52-
rich.print("") # Add a newline before the spinner
53-
codegen_dir, docs_dir, examples_dir = initialize_codegen(action, session=session, fetch_docs=fetch_docs)
61+
rich.print("")
62+
codegen_dir, docs_dir, examples_dir = initialize_codegen(action, session=session, fetch_docs=fetch_docs, programming_language=programming_language)
5463

5564
# Print success message
5665
rich.print(f"✅ {action} complete\n")

src/codegen/cli/workspace/initialize_workspace.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import shutil
22
from contextlib import nullcontext
33
from pathlib import Path
4+
from typing import Optional
45

56
import requests
67
import rich
@@ -13,23 +14,23 @@
1314
from codegen.cli.git.repo import get_git_repo
1415
from codegen.cli.git.url import get_git_organization_and_repo
1516
from codegen.cli.rich.spinners import create_spinner
17+
from codegen.cli.utils.constants import ProgrammingLanguage
1618
from codegen.cli.utils.notebooks import create_notebook
1719
from codegen.cli.workspace.docs_workspace import populate_api_docs
1820
from codegen.cli.workspace.examples_workspace import populate_examples
1921
from codegen.cli.workspace.venv_manager import VenvManager
2022

2123

2224
def initialize_codegen(
23-
status: Status | str = "Initializing",
24-
session: CodegenSession | None = None,
25-
fetch_docs: bool = False,
25+
status: Status | str = "Initializing", session: CodegenSession | None = None, fetch_docs: bool = False, programming_language: Optional[ProgrammingLanguage] = None
2626
) -> tuple[Path, Path, Path]:
2727
"""Initialize or update the codegen directory structure and content.
2828
2929
Args:
3030
status: Either a Status object to update, or a string action being performed ("Initializing" or "Updating")
3131
session: Optional CodegenSession for fetching docs and examples
3232
fetch_docs: Whether to fetch docs and examples (requires auth)
33+
programming_language: Optional override for the programming language
3334
3435
Returns:
3536
Tuple of (codegen_folder, docs_folder, examples_folder)
@@ -111,7 +112,11 @@ def initialize_codegen(
111112
populate_examples(session, EXAMPLES_FOLDER, response.examples, status_obj)
112113

113114
# Set programming language
114-
session.config.programming_language = str(response.language)
115+
if programming_language:
116+
session.config.programming_language = programming_language
117+
else:
118+
session.config.programming_language = str(response.language)
119+
115120
session.write_config()
116121

117122
return CODEGEN_FOLDER, DOCS_FOLDER, EXAMPLES_FOLDER

0 commit comments

Comments
 (0)