Skip to content

Commit 8c2c637

Browse files
committed
fix(startup): load config.yml from the executable directory
Prevent startup-launched daemon sessions from missing user rules when Windows starts the process with a different working directory. Update config load, save, and reload paths to use the executable directory and log the resolved path for easier debugging.
1 parent 1a959dc commit 8c2c637

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

internal/config/config.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package config
22

33
import (
44
"os"
5+
"path/filepath"
56
"time"
67

78
"gopkg.in/yaml.v3"
89
)
910

10-
const filePath = "config.yml"
11+
const fileName = "config.yml"
1112

1213
type Config struct {
1314
AlertTemp int `yaml:"alertTemp"`
@@ -23,6 +24,7 @@ var (
2324
)
2425

2526
func Load() error {
27+
filePath := configFilePath()
2628
data, err := os.ReadFile(filePath)
2729
if err != nil {
2830
if os.IsNotExist(err) {
@@ -52,6 +54,7 @@ func Save() error {
5254
return err
5355
}
5456

57+
filePath := configFilePath()
5558
if err := os.WriteFile(filePath, data, 0o644); err != nil {
5659
return err
5760
}
@@ -66,6 +69,7 @@ func Save() error {
6669
}
6770

6871
func ReloadIfChanged() (bool, error) {
72+
filePath := configFilePath()
6973
info, err := os.Stat(filePath)
7074
if err != nil {
7175
return false, err
@@ -77,3 +81,11 @@ func ReloadIfChanged() (bool, error) {
7781

7882
return true, Load()
7983
}
84+
85+
func configFilePath() string {
86+
exePath, err := os.Executable()
87+
if err != nil {
88+
return fileName
89+
}
90+
return filepath.Join(filepath.Dir(exePath), fileName)
91+
}

internal/daemon/daemon.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func Start() {
5252

5353
daemonExe := filepath.Join(filepath.Dir(exePath), daemonFileName)
5454
cmd := exec.Command(daemonExe)
55+
cmd.Dir = filepath.Dir(daemonExe)
5556
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x00000008}
5657
cmd.Start()
5758
}

0 commit comments

Comments
 (0)