Files
hose-use/src/ekb_cli/commands/agent/rule_list.py
T
gitea c87dfa52a5 批量收口 commands/agent/* 边界:全部改用 _cmd 便捷函数
- rule_service.py 新增 list_rules_cmd / get_rule_cmd / create_rule_cmd
- services/__init__.py 导出以上三个 _cmd 函数
- commands/agent/ 下 8 个文件一律移除 `from ekb_cli.api import get_client`,
  改为调用 services 层对应的 _cmd 函数(自管 client 生命周期)
- 同步清理各文件中残留的局部 try/except EkbError,统一冒泡至全局 handler
- 验证:commands/ 目录内已无任何 api 层直接引用
2026-04-15 18:17:13 +08:00

72 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""ekb agent rules:列出企业所有审核规则(表现层)
不导入 api 层;通过 services.list_rules_cmd 获取数据。
EkbError 由 CLI 入口全局 handler 统一处理,本层不 catch。
"""
import typer
from rich.console import Console
from rich.table import Table
from rich import print as rprint
from ekb_cli.services import list_rules_cmd
console = Console()
DESC_MAX = 20
PURPOSE_MAX = 12
def cmd_rule_list(
full: bool = typer.Option(False, "--full", "-f", help="显示完整内容,不截断描述字段"),
level: str = typer.Option(None, "--level", "-l", help="按风险等级筛选:严重 / 一般"),
):
"""列出企业所有审核规则"""
page, items = list_rules_cmd(level=level)
if not items:
rprint("[dim](暂无规则)[/dim]")
return
title = (
f"审核规则(共 {page.totalItems} 条,筛选:{level})"
if level else
f"审核规则(共 {page.totalItems} 条)"
)
table = Table(title=title, show_lines=True)
table.add_column("规则编码", style="dim", no_wrap=True)
if full:
table.add_column("规则描述")
table.add_column("适用场景")
else:
table.add_column("规则描述", max_width=DESC_MAX, no_wrap=True)
table.add_column("适用场景", max_width=PURPOSE_MAX, no_wrap=True)
table.add_column("风险等级", no_wrap=True)
table.add_column("关联Agent", no_wrap=True)
table.add_column("更新日期", style="dim", no_wrap=True)
table.add_column("ID", style="dim", no_wrap=True)
any_truncated = False
total_chars = 0
for r in items:
level_str = f"[red]{r.level}[/red]" if r.level == "严重" else f"[yellow]{r.level}[/yellow]"
update_date = r.updateTime[:10]
total_chars += len(r.description) + len(r.purpose)
if not full and (len(r.description) > DESC_MAX or len(r.purpose) > PURPOSE_MAX):
any_truncated = True
table.add_row(
r.code,
r.description,
r.purpose,
level_str,
str(len(r.agents)),
update_date,
r.id,
)
console.print(table)
if any_truncated:
rprint(f"[yellow]⚠ 部分字段已截断,完整规则文本共 {total_chars} 字符[/yellow]")
rprint("[dim]使用 --full 参数获取完整内容:ekb agent rules --full[/dim]")