Files
lightcore-cli/lightcore_cli/application_commands.py
T

37 lines
1.7 KiB
Python

"""Named commands for the individual application admin APIs."""
from . import fangyus_routes, gysmart_routes, hengde_routes, shop_routes
from .system_commands import add_operation
APPLICATIONS = {
"fangyus": ("Fangyus 后台操作", fangyus_routes.ROUTES),
"hengde": ("恒德后台操作", hengde_routes.ROUTES),
"gysmart": ("GYSmart 后台操作", gysmart_routes.ROUTES),
"shop": ("Shop 业务管理", shop_routes.ROUTES),
}
def add_commands(commands, app):
login = commands.add_parser("login", help="交互登录并保存本机会话")
login.add_argument("--base-url", help="目标后端地址")
login.set_defaults(handler=lambda client, args: client.login(args.base_url, isolate_target=True), endpoint="/auth/login")
if app != "gysmart":
for name, description, handler in (
("refresh", "刷新本机会话", lambda client, _: client.refresh()),
("logout", "退出并清理本机 Token", lambda client, _: client.logout()),
):
auth = commands.add_parser(name, help=description)
auth.set_defaults(handler=handler, endpoint=f"/auth/{name}")
subcommands = {(): commands}
for group, operations in APPLICATIONS[app][1].items():
path = tuple(group.split())
for index, part in enumerate(path, start=1):
prefix = path[:index]
if prefix not in subcommands:
section = subcommands[prefix[:-1]].add_parser(part, help=f"{part} 管理")
subcommands[prefix] = section.add_subparsers(required=True)
actions = subcommands[path]
for action, (route, schema, mode) in operations.items():
add_operation(actions, None, action, route, route, None, mode, schema=schema)