diff --git a/cmds/portmaster-start/main.go b/cmds/portmaster-start/main.go index 1c2a4d20..683c3bbf 100644 --- a/cmds/portmaster-start/main.go +++ b/cmds/portmaster-start/main.go @@ -7,6 +7,7 @@ import ( "log" "os" "os/signal" + "path/filepath" "strings" "syscall" @@ -137,7 +138,12 @@ func configureDataRoot() error { dataDir = os.Getenv("PORTMASTER_DATA") } - // check data dir + // if it's still empty try to auto-detect it + if dataDir == "" { + dataDir = detectInstallationDir() + } + + // finally, if it's still empty the user must provide it if dataDir == "" { return errors.New("please set the data directory using --data=/path/to/data/dir") } @@ -203,3 +209,23 @@ func updateRegistryIndex() { registry.SelectVersions() } + +func detectInstallationDir() string { + exePath, err := filepath.Abs(os.Args[0]) + if err != nil { + return "" + } + + parent := filepath.Dir(exePath) + stableJSONFile := filepath.Join(parent, "updates", "stable.json") + stat, err := os.Stat(stableJSONFile) + if err != nil { + return "" + } + + if stat.IsDir() { + return "" + } + + return parent +}