Try to auto-detect installation directory

This commit is contained in:
Patrick Pacher
2020-07-22 09:26:11 +02:00
parent 1fa6cbcc6b
commit 088fef6f55

View File

@@ -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
}