Skip to content

Commit bdf631c

Browse files
committed
feat(init): Add options for URL, directory, and ZMK version
The "zmk init" command now supports arguments for setting the URL and clone directory directly instead of prompting for them. It also now has a --zmk-version option, which switches to the given revision of ZMK prior to running "west update". If the selected revision does not exist, then it leaves the version unchanged. Finishes implementing zmkfirmware#52
1 parent 5326dbb commit bdf631c

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

zmk/commands/init.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import subprocess
88
import webbrowser
99
from pathlib import Path
10+
from typing import Annotated
1011
from urllib.parse import urlparse
1112

1213
import rich
@@ -29,7 +30,24 @@
2930
TEXT_WIDTH = 80
3031

3132

32-
def init(ctx: typer.Context) -> None:
33+
def init(
34+
ctx: typer.Context,
35+
url: Annotated[
36+
str | None, typer.Argument(help="URL of an existing repository to clone.")
37+
] = None,
38+
name: Annotated[
39+
str | None,
40+
typer.Argument(help="Directory name where the repo should be cloned."),
41+
] = None,
42+
revision: Annotated[
43+
str | None,
44+
typer.Option(
45+
"--zmk-version",
46+
metavar="REVISION",
47+
help="Use the specified version of ZMK instead of the default.",
48+
),
49+
] = None,
50+
) -> None:
3351
"""Create a new ZMK config repo or clone an existing one."""
3452

3553
console = rich.get_console()
@@ -38,12 +56,28 @@ def init(ctx: typer.Context) -> None:
3856
_check_dependencies()
3957
_check_for_existing_repo(cfg)
4058

41-
url = _get_repo_url()
42-
name = _get_directory_name(url)
59+
if url is None:
60+
url = _get_repo_url()
61+
62+
if name is None:
63+
name = _get_directory_name(url)
4364

4465
_clone_repo(url, name)
4566

4667
repo = Repo(Path() / name)
68+
69+
if revision:
70+
try:
71+
repo.ensure_west_ready()
72+
repo.set_zmk_version(revision)
73+
except ValueError as ex:
74+
console.print()
75+
console.print(
76+
f'[yellow]Failed to switch to ZMK revision "{revision}":[/yellow]'
77+
)
78+
console.print(ex)
79+
console.print()
80+
4781
repo.run_west("update")
4882

4983
console.print()

0 commit comments

Comments
 (0)