Skip to content

Commit 5a1bec8

Browse files
committed
fix(cli):增加清空日志
1 parent 164c653 commit 5a1bec8

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

  • src/module/scripts

src/module/scripts/cli

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ is_running() {
108108
[ -n "$(get_pid "$SING_BOX_BIN")" ]
109109
}
110110

111-
# 确保 CLI 依赖的日志/节点/运行时目录存在
111+
# 确保 CLI 依赖的日志/运行时目录存在
112+
# 注: 默认节点目录 (DEFAULT_NODES_DIR) 不在此创建——它仅在真正写入节点时
113+
# 由 subscription.sh 的 prepare_output_dir 按需创建,避免每次任意命令都
114+
# 重建用户可能已主动删除的空 default 目录。
112115
ensure_cli_dirs() {
113116
ensure_dir "$LOG_DIR" "无法创建日志目录: $LOG_DIR"
114-
ensure_dir "$DEFAULT_NODES_DIR" "无法创建默认节点目录: $DEFAULT_NODES_DIR"
115117
ensure_dir "$RUNTIME_DIR" "无法创建运行时目录: $RUNTIME_DIR"
116118
}
117119

@@ -159,6 +161,8 @@ show_service_help() {
159161
cli service restart 重启服务
160162
cli service logs [service|core|sub] [行数]
161163
查看日志
164+
cli service logs-clear [service|core|sub]
165+
清空日志
162166
EOF
163167
}
164168

@@ -323,6 +327,29 @@ cmd_service_logs() {
323327
tail -n "$lines" "$file"
324328
}
325329

330+
#######################################
331+
# 清空日志
332+
# 参数:
333+
# $1 日志类型 (service/core/sub,默认 service)
334+
# 返回: 无 (将对应日志文件清零)
335+
#######################################
336+
cmd_service_logs_clear() {
337+
local target="${1:-service}"
338+
local file
339+
340+
# 映射日志类型到文件路径
341+
case "$target" in
342+
service) file="$SERVICE_LOG" ;;
343+
core | sing-box | singbox) file="$CORE_LOG" ;;
344+
sub | subscription) file="$SUB_LOG" ;;
345+
*) die_cli "未知日志类型: $target" ;;
346+
esac
347+
348+
# 文件存在则清零;不存在视为已空,直接成功返回
349+
[ -f "$file" ] && : > "$file"
350+
info "已清空日志: $target"
351+
}
352+
326353
# service 命令组分发
327354
cmd_service() {
328355
local subcmd="${1:-status}"
@@ -334,6 +361,7 @@ cmd_service() {
334361
stop) cmd_service_stop "$@" ;;
335362
restart) cmd_service_restart "$@" ;;
336363
logs) cmd_service_logs "$@" ;;
364+
logs-clear) cmd_service_logs_clear "$@" ;;
337365
help | -h | --help) show_service_help ;;
338366
*) die_cli "用法错误,使用 cli service help 查看帮助" ;;
339367
esac

0 commit comments

Comments
 (0)