feat(vps-xray): 新增 update_env_field 就地更新 .env 单个字段

This commit is contained in:
2026-08-06 16:02:22 +08:00
parent 920774d7d7
commit 39b0aee2c9
2 changed files with 51 additions and 0 deletions
+17
View File
@@ -127,6 +127,23 @@ resolve_restart_config() {
return 0
}
# 就地更新 .env 中的单个字段:存在则替换,不存在则追加。
# 刻意不复用 save_env——后者是 cat > 全量重写,会抹掉用户自己加的注释与字段;
# 轻量入口只该改自己那一行。
update_env_field() {
local file="$1" key="$2" value="$3" esc
[ -f "$file" ] || return 1
if grep -q "^${key}=" "$file"; then
# 用 | 作分隔符,并转义 value 中的 | & \,避免值里的 / 破坏 sed
esc=$(printf '%s' "$value" | sed 's/[|&\\]/\\&/g')
sed -i "s|^${key}=.*|${key}=${esc}|" "$file"
else
printf '%s=%s\n' "$key" "$value" >> "$file"
fi
return 0
}
resolve_restart_config || exit 1
# 过渡兼容:harden_system / start_service / save_env 仍引用 DAILY_RESTART