From 1ac23403e5686d719b551ece35395a2d77b11c82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=85=8E=E9=A5=BC=E6=9E=9C=E5=AD=90=E5=8D=B7=E9=B2=A8?= =?UTF-8?q?=E9=B1=BC=E8=BE=A3=E6=A4=92?= Date: Fri, 7 Feb 2025 10:17:56 +0800 Subject: [PATCH] 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 --- scripts/run/cursor_mac_id_modifier.sh | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/scripts/run/cursor_mac_id_modifier.sh b/scripts/run/cursor_mac_id_modifier.sh index 8b1e4e6..b892a13 100644 --- a/scripts/run/cursor_mac_id_modifier.sh +++ b/scripts/run/cursor_mac_id_modifier.sh @@ -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 } # 生成新的配置