mirror of
https://github.com/yuaotian/go-cursor-help.git
synced 2025-06-08 12:32:06 +08:00
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
This commit is contained in:
parent
88541eaeda
commit
1ac23403e5
@ -168,15 +168,27 @@ modify_or_add_config() {
|
||||
local value="$2"
|
||||
local file="$3"
|
||||
|
||||
if [ ! -f "$file" ]; then
|
||||
log_error "文件不存在: $file"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# 检查key是否存在
|
||||
if grep -q "\"$key\":" "$file"; then
|
||||
# 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
|
||||
# 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
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# 生成新的配置
|
||||
|
Loading…
x
Reference in New Issue
Block a user