Skip to content
25 changes: 21 additions & 4 deletions astrbot/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import click

from . import __version__
from .commands import conf, init, password, plug, run
from .commands import config, init, password, plugin, run, service

logo_tmpl = r"""
___ _______.___________..______ .______ ______ .___________.
Expand All @@ -17,7 +17,23 @@
"""


@click.group()
class AstrBotCLIGroup(click.Group):
COMMAND_ALIASES = {
"conf": "config",
"plug": "plugin",
}

def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command | None:
command = super().get_command(ctx, cmd_name)
if command is not None:
return command
alias_target = self.COMMAND_ALIASES.get(cmd_name)
if alias_target is None:
return None
return super().get_command(ctx, alias_target)


@click.group(cls=AstrBotCLIGroup)
@click.version_option(__version__, prog_name="AstrBot")
def cli() -> None:
"""The AstrBot CLI"""
Expand Down Expand Up @@ -52,9 +68,10 @@ def help(command_name: str | None) -> None:
cli.add_command(init)
cli.add_command(run)
cli.add_command(help)
cli.add_command(plug)
cli.add_command(conf)
cli.add_command(plugin)
cli.add_command(config)
cli.add_command(password)
cli.add_command(service)

if __name__ == "__main__":
cli()
10 changes: 7 additions & 3 deletions astrbot/cli/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from .cmd_conf import conf
from .cmd_conf import conf as config
from .cmd_init import init
from .cmd_password import password
from .cmd_plug import plug
from .cmd_plug import plug as plugin
from .cmd_run import run
from .cmd_service import service

__all__ = ["conf", "init", "password", "plug", "run"]
conf = config
plug = plugin

__all__ = ["config", "conf", "init", "password", "plugin", "plug", "run", "service"]
2 changes: 1 addition & 1 deletion astrbot/cli/commands/cmd_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def _set_dashboard_password(config: dict[str, Any], raw_password: str) -> None:
_set_nested_item(config, "dashboard.password_change_required", False)


@click.group(name="conf")
@click.group(name="config")
def conf() -> None:
"""Configuration management commands

Expand Down
2 changes: 1 addition & 1 deletion astrbot/cli/commands/cmd_plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)


@click.group()
@click.group(name="plugin")
def plug() -> None:
"""Plugin management"""

Expand Down
12 changes: 11 additions & 1 deletion astrbot/cli/commands/cmd_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from ..utils import check_astrbot_root, check_dashboard, get_astrbot_root

DASHBOARD_RESET_PASSWORD_ENV = "ASTRBOT_DASHBOARD_RESET_PASSWORD"


async def run_astrbot(astrbot_root: Path) -> None:
"""Run AstrBot"""
Expand All @@ -28,8 +30,13 @@ async def run_astrbot(astrbot_root: Path) -> None:

@click.option("--reload", "-r", is_flag=True, help="Auto-reload plugins")
@click.option("--port", "-p", help="AstrBot Dashboard port", required=False, type=str)
@click.option(
"--reset-password",
is_flag=True,
help="Force reset the dashboard initial password on startup.",
)
@click.command()
def run(reload: bool, port: str) -> None:
def run(reload: bool, port: str, reset_password: bool) -> None:
"""Run AstrBot"""
try:
os.environ["ASTRBOT_CLI"] = "1"
Expand All @@ -50,6 +57,9 @@ def run(reload: bool, port: str) -> None:
click.echo("Plugin auto-reload enabled")
os.environ["ASTRBOT_RELOAD"] = "1"

if reset_password:
os.environ[DASHBOARD_RESET_PASSWORD_ENV] = "1"

lock_file = astrbot_root / "astrbot.lock"
lock = FileLock(lock_file, timeout=5)
with lock.acquire():
Expand Down
Loading
Loading