feat(main.go): actually detect instance path

This commit is contained in:
Paul Fey 2025-02-27 16:47:53 +01:00
parent 1a8a0896aa
commit ff2ade5e84

26
main.go
View file

@ -13,7 +13,7 @@ import (
func main() {
running := true
instance_path, err := filepath.Abs("/home/pauljako/boundaries")
instance_path, err := get_instance_path()
if err != nil {
log.Fatal(err)
}
@ -80,3 +80,27 @@ func accept_clients(ln *net.Listener, instance *string, running *bool) {
}()
}
}
func get_instance_path() (string, error) {
if len(os.Args) == 2 {
return filepath.Abs(os.Args[1])
}
// When bndd has been started by boundaries, this can be used to get the instance it used
app_dir, has := os.LookupEnv("APP_DIR")
if has {
return filepath.Abs(filepath.Join(app_dir, "..", ".."))
}
// Fallback to the user-path
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
home_instance_path, err := filepath.Abs(filepath.Join(home, "boundaries"))
if exists, err := exists(home_instance_path); exists && err == nil {
return home_instance_path, nil
}
return "", errors.New("no suitable instance path found")
}