Browse Source

feat: Enhance configuration modification with error handling and file existence check

- Added file existence check before configuration modification
- Implemented error logging for file modification failures
- Added return value checks for sed operations
- Improved error handling in `modify_or_add_config()` function
- Maintained existing configuration key replacement and addition logic
pull/217/head v0.0.75
煎饼果子卷鲨鱼辣椒 4 months ago
parent
commit
1ac23403e5
  1. 18
      scripts/run/cursor_mac_id_modifier.sh

18
scripts/run/cursor_mac_id_modifier.sh

@ -168,15 +168,27 @@ modify_or_add_config() {
local value="$2" local value="$2"
local file="$3" local file="$3"
if [ ! -f "$file" ]; then
log_error "文件不存在: $file"
return 1
fi
# 检查key是否存在 # 检查key是否存在
if grep -q "\"$key\":" "$file"; then if grep -q "\"$key\":" "$file"; then
# key存在,执行替换 # key存在,执行替换
sed -i '' -e "s/\"$key\":[[:space:]]*\"[^\"]*\"/\"$key\": \"$value\"/" "$file"
if ! sed -i '' -e "s/\"$key\":[[:space:]]*\"[^\"]*\"/\"$key\": \"$value\"/" "$file"; then
log_error "修改配置失败: $key"
return 1
fi
else else
# key不存在,添加新的key-value对 # key不存在,添加新的key-value对
# 在最后一个}前添加新行
sed -i '' -e "s/}$/,\n \"$key\": \"$value\"\n}/" "$file"
if ! sed -i '' -e "s/}$/,\n \"$key\": \"$value\"\n}/" "$file"; then
log_error "添加配置失败: $key"
return 1
fi
fi fi
return 0
} }
# 生成新的配置 # 生成新的配置

Loading…
Cancel
Save