重构代码, 更新密码函数,支持传入配置文件路径并增加错误处理

This commit is contained in:
Olia Lisa
2026-05-28 14:51:17 +08:00
parent 30a50c0292
commit 499b085f05
3 changed files with 30 additions and 22 deletions
+24 -16
View File
@@ -2,30 +2,38 @@
# 更新密码的函数
update_password() {
local SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # 脚本文件夹绝对路径
local CONFIG_FILE="$SCRIPT_DIR/../config/config.json"
local CONFIG_FILE="${1}"
if [[ -z "$CONFIG_FILE" ]]; then
echo "[Error] config_file配置文件路径 不能为空"
return 1
fi
# 生成一个长度为16的随机密码
local NEW_PASSWORD=$(gen_password 16)
local NEW_PASSWORD
NEW_PASSWORD=$(gen_password 16)
# 修改密码
sed -i 's/你的密码/'"$NEW_PASSWORD"'/g' $CONFIG_FILE
sed -i 's/你的密码/'"$NEW_PASSWORD"'/g' "$CONFIG_FILE"
echo "设置密码成功"
}
# 更新混淆密码
update_obfs_password() {
local SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # 脚本文件夹绝对路径
local CONFIG_FILE="$SCRIPT_DIR/../config/config.json"
local CONFIG_FILE="${1}"
if [[ -z "$CONFIG_FILE" ]]; then
echo "[Error] config_file配置文件路径 不能为空"
return 1
fi
# 生成一个长度为16的随机密码
local NEW_PASSWORD=$(gen_password 16)
local NEW_PASSWORD
NEW_PASSWORD=$(gen_password 16)
# 修改密码
sed -i 's/你的混淆密码/'"$NEW_PASSWORD"'/g' $CONFIG_FILE
sed -i 's/你的混淆密码/'"$NEW_PASSWORD"'/g' "$CONFIG_FILE"
echo "设置密码成功"
}