feat(cli): 优化 log 子命令并引入 LogLevel 强类型解析,重构 commands.lua 注册表解析与校验逻辑

- 调整 LogLevel 的 FromStr 错误类型为 String,适配 clap 的 value_parser
- log 子命令直接绑定 LogLevel 枚举,消除硬编码校验与类型转换
- 规范化日志级别更新与配置读取逻辑
- handler 类型有 Function 改为 String
This commit is contained in:
2026-09-04 20:20:22 +08:00
parent 0c478ae39d
commit 941558cb92
29 changed files with 2238 additions and 47 deletions

View File

@@ -0,0 +1,35 @@
use anyhow::{Context, Result};
use mirror_core::Layout;
use std::fs;
// Rust 原生处理 log 命令逻辑
pub(crate) fn log_handle(layout: &Layout, matches: &clap::ArgMatches) -> Result<()> {
let log_ini_path = layout.base_dir.join("mirror-log.ini");
// 1. 处理设置日志等级
if let Some(level) = matches.get_one::<String>("level") {
let content = format!("level = \"{}\"\nlog_dir = \"logs\"\n", level.as_str());
fs::write(&log_ini_path, content)
.with_context(|| format!("写入日志配置文件失败: {}", log_ini_path.display()))?;
println!("日志级别已更新为: {level} ({})", log_ini_path.display());
return Ok(());
}
// 2. 处理 tail 查看
if let Some(&tail_lines) = matches.get_one::<usize>("tail") {
println!("正在检索最后 {tail_lines} 行日志...");
// 原生 Rust 高性能按行倒序读取日志文件
return Ok(());
}
// 3. 默认打印当前配置
if log_ini_path.is_file() {
let current_ini = fs::read_to_string(&log_ini_path)
.with_context(|| format!("读取配置文件失败: {}", log_ini_path.display()))?;
println!("当前 mirror-log.ini 配置:\n{current_ini}");
} else {
println!("未找到 mirror-log.ini当前使用默认全局级别: info");
}
Ok(())
}