feat: add yes option to init command (#9514)

This commit is contained in:
Soulter
2026-08-02 22:52:15 +08:00
committed by GitHub
parent 9bb294d8cc
commit c68a2ea83b
2 changed files with 37 additions and 6 deletions
+16 -6
View File
@@ -29,12 +29,17 @@ def _initialize_config_from_env(astrbot_root: Path) -> None:
click.echo("Initialized data/cmd_config.json with dashboard initial password.")
async def initialize_astrbot(astrbot_root: Path) -> None:
"""Execute AstrBot initialization logic"""
async def initialize_astrbot(astrbot_root: Path, yes: bool = False) -> None:
"""Execute AstrBot initialization logic.
Args:
astrbot_root: AstrBot root directory path.
yes: Whether to skip the installation confirmation.
"""
dot_astrbot = astrbot_root / ".astrbot"
if not dot_astrbot.exists():
if click.confirm(
if yes or click.confirm(
f"Install AstrBot to this directory? {astrbot_root}",
default=True,
abort=True,
@@ -59,8 +64,13 @@ async def initialize_astrbot(astrbot_root: Path) -> None:
@click.command()
def init() -> None:
"""Initialize AstrBot"""
@click.option("--yes", "-y", is_flag=True, help="Skip confirmation prompts")
def init(yes: bool) -> None:
"""Initialize AstrBot.
Args:
yes: Whether to skip confirmation prompts.
"""
from ..utils import get_astrbot_root
click.echo("Initializing AstrBot...")
@@ -71,7 +81,7 @@ def init() -> None:
try:
with lock.acquire():
asyncio.run(initialize_astrbot(astrbot_root))
asyncio.run(initialize_astrbot(astrbot_root, yes=yes))
click.echo("Done! You can now run 'astrbot run' to start AstrBot")
except Timeout:
raise click.ClickException(
+21
View File
@@ -1,11 +1,32 @@
import json
import pytest
from click.testing import CliRunner
from astrbot.cli.commands import cmd_init
from astrbot.core.utils.auth_password import verify_dashboard_password
def test_init_yes_skips_install_confirmation(monkeypatch, tmp_path):
async def fake_check_dashboard(_data_path):
return None
def fail_confirm(*_args, **_kwargs):
pytest.fail("-y should skip the installation confirmation")
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(cmd_init, "check_dashboard", fake_check_dashboard)
monkeypatch.setattr(cmd_init.click, "confirm", fail_confirm)
result = CliRunner().invoke(cmd_init.init, ["-y"])
assert result.exit_code == 0, result.output
assert (tmp_path / ".astrbot").exists()
assert (tmp_path / "data" / "config").is_dir()
assert (tmp_path / "data" / "plugins").is_dir()
assert (tmp_path / "data" / "temp").is_dir()
@pytest.mark.asyncio
async def test_init_without_initial_password_env_does_not_create_config(
monkeypatch,