支持debug参数,debug时从本地目录读取配置

This commit is contained in:
Pan Qiancheng 2025-04-15 16:10:51 +08:00
parent 9c14e806dc
commit 82a7798b2b
2 changed files with 30 additions and 1 deletions

View File

@ -31,6 +31,15 @@ func isForce() bool {
return len(os.Args) > 1 && os.Args[1] == constants.ForceFlag
}
func isDebug() bool {
for _, arg := range os.Args[1:] {
if arg == constants.DebugFlag {
return true
}
}
return false
}
func init() {
// 初始化配置
@ -157,7 +166,26 @@ func run() error {
// initConfig 初始化配置
func initConfig() error {
viper.AddConfigPath(constants.ConfigPath)
var configDir string
// 检查是否包含 --debug 参数
if isDebug() {
// 调试模式:使用常量路径
configDir = constants.ConfigPath
} else {
// 非调试模式:使用可执行文件路径拼接
exePath, err := os.Executable()
if err != nil {
return err
}
exeDir := filepath.Dir(exePath)
configDir = filepath.Join(exeDir, constants.ConfigPath)
}
viper.AddConfigPath(configDir)
viper.SetConfigName("config")
viper.SetConfigType("yaml")
return viper.ReadInConfig()
}

View File

@ -13,6 +13,7 @@ const (
PidFilePath = "/tmp/bash_service.pid" // PID 文件路径
DaemonFlag = "-daemon" // 后台进程标志
ForceFlag = "-force" // 强制结束之前的后台进程
DebugFlag = "-debug" // 强制结束之前的后台进程
ConfigFileMode = 0644 // 文件权限
ConfigPath = "./config" // 配置文件路径
LogFileMode = 0755 // 日志文件权限