Skip to content

Commit

Permalink
fix(core): 优化 updateProperties函数,添加创建上级目录逻辑- 在更新属性文件之前,先创建上级目录(如果不存在)
Browse files Browse the repository at this point in the history
- 增加错误处理,如果创建目录失败则返回错误
- 这个修改提高了函数的健壮性,确保在目录不存在时也能正常工作
  • Loading branch information
kiririx committed Dec 30, 2024
1 parent 370c82d commit 16031c0
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ec/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"log"
"os"
"path/filepath"
"strings"
"sync"
)
Expand Down Expand Up @@ -370,6 +371,11 @@ func removeKeyFromProperties(path string, key string) error {

// updateProperties 更新或添加键值对到 properties 文件
func updateProperties(path string, key string, value string) error {
// 创建上级目录
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return fmt.Errorf("创建目录失败: %w", err)
}
// 尝试打开文件,如果文件不存在则创建新文件
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
Expand Down

0 comments on commit 16031c0

Please sign in to comment.