feat(cli): 优化 log 子命令并引入 LogLevel 强类型解析,重构 commands.lua 注册表解析与校验逻辑
- 调整 LogLevel 的 FromStr 错误类型为 String,适配 clap 的 value_parser - log 子命令直接绑定 LogLevel 枚举,消除硬编码校验与类型转换 - 规范化日志级别更新与配置读取逻辑 - handler 类型有 Function 改为 String
This commit is contained in:
37
commands/commands.lua
Normal file
37
commands/commands.lua
Normal file
@@ -0,0 +1,37 @@
|
||||
-- commands/commands.lua
|
||||
function command(mod_path)
|
||||
-- 无论输入 "a/b/c" 还是 "a\b\c",统一替换为标准的 "a.b.c"
|
||||
local normalized = mod_path:gsub("[/\\]", ".")
|
||||
|
||||
return {
|
||||
__is_lazy_command = true,
|
||||
module = normalized
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
return {
|
||||
-- 1. 单文件子命令 (commands/use.lua)
|
||||
["use"] = {
|
||||
description = "Switch the active version of a tool",
|
||||
handler = command("use")
|
||||
},
|
||||
|
||||
-- 2. 单文件子命令 (commands/list.lua)
|
||||
["list"] = {
|
||||
description = "List available and currently activated versions",
|
||||
handler = command("list")
|
||||
},
|
||||
|
||||
-- 3. 复杂多文件子命令 (加载 commands/log/init.lua)
|
||||
["log"] = {
|
||||
description = "View or configure mirror runtime logs",
|
||||
handler = command("log")
|
||||
},
|
||||
|
||||
-- 4. 支持别名快捷映射 (例如 mr ls 等价于 mr list)
|
||||
["ls"] = {
|
||||
description = "Alias for list",
|
||||
handler = command("list")
|
||||
}
|
||||
}
|
||||
24
commands/log/init.lua
Normal file
24
commands/log/init.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
-- commands/log/init.lua
|
||||
local argparse = require("argparse")
|
||||
local utils = require("log.utils") -- 支持相对目录级 require
|
||||
|
||||
return function(raw_args)
|
||||
local parser = argparse("mr log", "Manage and view mirror logs.")
|
||||
parser:option("-l --level", "Set log level"):choices({"trace", "debug", "info", "warn", "error"})
|
||||
parser:option("-t --tail", "Show last N lines"):convert(tonumber)
|
||||
|
||||
local ok, args = pcall(function() return parser:parse(raw_args) end)
|
||||
if not ok then
|
||||
error("__ARGPARSE_HELP__")
|
||||
end
|
||||
|
||||
if args.level then
|
||||
print(utils.format_level(args.level))
|
||||
mr.fs.write(__MIRROR_DIR__ .. "/mirror-log.ini", "level = \"" .. args.level .. "\"\n")
|
||||
print("✔ Updated log level.")
|
||||
elseif args.tail then
|
||||
print(string.format("Tailing last %d lines...", args.tail))
|
||||
else
|
||||
print("Log module ready. Run 'mr log --help' for details.")
|
||||
end
|
||||
end
|
||||
8
commands/log/utils.lua
Normal file
8
commands/log/utils.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
-- commands/log/utils.lua
|
||||
local M = {}
|
||||
|
||||
function M.format_level(level)
|
||||
return string.format("==> [LOG LEVEL: %s] <==", string.upper(level))
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user