feat(cli): 实现 nyahome config 命令
允许通过 nyahome 终端命令修改设置 同时再次调整数据库有关的内部常量的位置
This commit is contained in:
@@ -5,7 +5,7 @@ from pathlib import Path
|
||||
from typing import Mapping
|
||||
|
||||
from nyahome.cli.cli import console
|
||||
from nyahome.database.engine import db_driver_available, db_type_allowlist
|
||||
from nyahome.data import db_type_allowlist, db_driver_available
|
||||
|
||||
|
||||
class CliWarning:
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
from typing import Annotated
|
||||
|
||||
import typer
|
||||
from rich.table import Table
|
||||
|
||||
from nyahome.config import Config, config_manager
|
||||
|
||||
from .cli import console
|
||||
|
||||
config_app = typer.Typer()
|
||||
|
||||
|
||||
@config_app.command(name="list")
|
||||
def list_all_configs() -> None:
|
||||
"""
|
||||
列出所有 NyaHome 定义的设置项。直接输出,可能包含敏感信息。
|
||||
|
||||
同时包含默认值和当前值。在 NyaHome 首次运行时,所有设置项都会以默认值存储。
|
||||
"""
|
||||
config_manager.sync_load_config()
|
||||
ci = Config()
|
||||
|
||||
table = Table(title="NyaHome 设置")
|
||||
table.add_column("设置键名", style="cyan", no_wrap=True)
|
||||
table.add_column("值类型", style="bright_black", no_wrap=True)
|
||||
table.add_column("当前值", style="white")
|
||||
table.add_column("默认值", style="bright_black")
|
||||
|
||||
for key, value in config_manager.get_config().items():
|
||||
default_value = getattr(ci, key)
|
||||
table.add_row(key, type(default_value).__name__, str(value), str(default_value))
|
||||
|
||||
console.print(table)
|
||||
|
||||
|
||||
@config_app.command(name="set")
|
||||
def set_config_item(
|
||||
key: Annotated[str, typer.Argument(help="设置键名")],
|
||||
value: Annotated[list[str], typer.Argument(help="设置键新值,类型会自动转换,多个输入将被视作列表")],
|
||||
) -> None:
|
||||
"""
|
||||
修改一项设置。
|
||||
|
||||
目前,NyaHome 的设置键所支持的值类型包括:str int bool list
|
||||
"""
|
||||
config_manager.sync_load_config()
|
||||
if len(value) == 1:
|
||||
value = value[0]
|
||||
try:
|
||||
config_manager.set(key, value)
|
||||
except AttributeError:
|
||||
console.print(f"[yellow]设置失败,设置键 [cyan]{key}[/cyan] 不存在。[/yellow]")
|
||||
config_manager.sync_save_config()
|
||||
console.print(f"已经将设置项 [cyan]{key}[/cyan] 的值设置为 [cyan]{value}[/cyan]")
|
||||
|
||||
|
||||
@config_app.command(name="reset")
|
||||
def reset_config_item(
|
||||
key: Annotated[str, typer.Argument(help="设置键名")],
|
||||
) -> None:
|
||||
"""
|
||||
重置一项设置至默认值。
|
||||
"""
|
||||
config_manager.sync_load_config()
|
||||
try:
|
||||
config_manager.reset(key)
|
||||
except AttributeError:
|
||||
console.print(f"[yellow]设置失败,设置键 [cyan]{key}[/cyan] 不存在。[/yellow]")
|
||||
config_manager.sync_save_config()
|
||||
console.print(f"已经将设置项 [cyan]{key}[/cyan] 的值重置为默认值。")
|
||||
Reference in New Issue
Block a user