Skip to content

Commit 6716307

Browse files
committed
fix: v0.5.2 - 配置文件和数据库使用可执行文件所在目录
修复内容: - 配置文件默认在可执行文件所在目录创建 (config.toml) - 数据库默认在可执行文件所在目录的 data 子目录创建 - 解决 /opt/project 权限问题 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 973d43b commit 6716307

3 files changed

Lines changed: 36 additions & 19 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Variables
44
APP_NAME := mask-ctl
5-
VERSION := 0.5.1
5+
VERSION := 0.5.2
66
BUILD_DIR := build
77
BIN_DIR := $(BUILD_DIR)/bin
88
CMD_DIR := cmd/coding-plan-mask

cmd/coding-plan-mask/main.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@ import (
1919
)
2020

2121
var (
22-
version = "0.5.1"
22+
version = "0.5.2"
2323
commit = "unknown"
2424
date = "unknown"
2525
)
2626

27+
// getExecutableDir 获取可执行文件所在目录
28+
func getExecutableDir() string {
29+
execPath, err := os.Executable()
30+
if err != nil {
31+
// 回退到当前工作目录
32+
wd, _ := os.Getwd()
33+
return wd
34+
}
35+
return filepath.Dir(execPath)
36+
}
37+
2738
func main() {
2839
// 检查子命令
2940
if len(os.Args) > 1 {
@@ -91,8 +102,9 @@ func main() {
91102
logger := initLogger(cfg.Debug)
92103
defer logger.Sync()
93104

94-
// 初始化存储
95-
dataDir := filepath.Join(filepath.Dir(cfg.GetConfigPath()), "data")
105+
// 初始化存储 - 数据库在可执行文件所在目录的 data 子目录
106+
execDir := getExecutableDir()
107+
dataDir := filepath.Join(execDir, "data")
96108
store, err := storage.New(dataDir)
97109
if err != nil {
98110
logger.Fatal("初始化存储失败", zap.Error(err))
@@ -123,21 +135,14 @@ func showStats(args []string) {
123135
configPath := fs.String("config", "", "配置文件路径")
124136
_ = fs.Parse(args)
125137

126-
// 确定数据目录
138+
// 确定数据目录 - 默认在可执行文件所在目录
127139
var dataDir string
128140
if *configPath != "" {
129141
// 从配置文件路径推导数据目录
130142
dataDir = filepath.Join(filepath.Dir(*configPath), "data")
131143
} else {
132-
// 尝试从 systemd 服务获取配置路径
133-
serviceConfig := "/opt/project/coding-plan-mask/config/config.toml"
134-
if _, err := os.Stat(serviceConfig); err == nil {
135-
dataDir = filepath.Join(filepath.Dir(serviceConfig), "data")
136-
} else {
137-
// 默认路径
138-
homeDir, _ := os.UserHomeDir()
139-
dataDir = filepath.Join(homeDir, ".config", "coding-plan-mask", "data")
140-
}
144+
// 使用可执行文件所在目录
145+
dataDir = filepath.Join(getExecutableDir(), "data")
141146
}
142147
dbPath := filepath.Join(dataDir, "proxy.db")
143148

internal/config/config.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,30 @@ func DefaultConfig() *Config {
145145
}
146146
}
147147

148-
// DefaultConfigPath 默认配置文件路径
149-
const DefaultConfigPath = "/opt/project/coding-plan-mask/config/config.toml"
148+
// getExecutableDir 获取可执行文件所在目录
149+
func getExecutableDir() string {
150+
execPath, err := os.Executable()
151+
if err != nil {
152+
// 回退到当前工作目录
153+
wd, _ := os.Getwd()
154+
return wd
155+
}
156+
return filepath.Dir(execPath)
157+
}
158+
159+
// getDefaultConfigPath 获取默认配置文件路径(在可执行文件所在目录)
160+
func getDefaultConfigPath() string {
161+
return filepath.Join(getExecutableDir(), "config.toml")
162+
}
150163

151164
// LoadConfig 从文件加载配置
152165
func LoadConfig(path string) (*Config, error) {
153166
cfg := DefaultConfig()
154-
cfg.configPath = path
155167

156168
if path == "" {
157-
path = DefaultConfigPath
158-
cfg.configPath = path
169+
path = getDefaultConfigPath()
159170
}
171+
cfg.configPath = path
160172

161173
// 记录配置路径
162174
absPath, _ := filepath.Abs(path)

0 commit comments

Comments
 (0)