2024-09-07 13:11:04 +00:00
|
|
|
package daecommon
|
|
|
|
|
|
|
|
import (
|
2024-09-09 14:34:00 +00:00
|
|
|
"isle/toolkit"
|
2024-09-07 13:11:04 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/adrg/xdg"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EnvVars are variables which are derived based on the environment which the
|
|
|
|
// process is running in.
|
|
|
|
type EnvVars struct {
|
2024-09-09 14:34:00 +00:00
|
|
|
StateDir toolkit.Dir
|
|
|
|
RuntimeDir toolkit.Dir
|
2024-09-07 13:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEnvVars will return the EnvVars of the current processes, as determined by
|
|
|
|
// the process's environment.
|
2024-09-09 14:34:00 +00:00
|
|
|
var GetEnvVars = sync.OnceValue(func() EnvVars {
|
2024-09-07 13:11:04 +00:00
|
|
|
// RUNTIME_DIRECTORY/STATE_DIRECTORY are used by the systemd service in
|
|
|
|
// conjunction with the RuntimeDirectory/StateDirectory directives.
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
var (
|
|
|
|
res EnvVars
|
|
|
|
|
|
|
|
stateDirPath = envOr(
|
|
|
|
"STATE_DIRECTORY",
|
|
|
|
func() string { return filepath.Join(xdg.StateHome, "isle") },
|
|
|
|
)
|
2024-09-07 13:11:04 +00:00
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
runtimeDirPath = envOr(
|
|
|
|
"RUNTIME_DIRECTORY",
|
|
|
|
func() string { return filepath.Join(xdg.RuntimeDir, "isle") },
|
|
|
|
)
|
2024-09-07 13:11:04 +00:00
|
|
|
)
|
|
|
|
|
2024-09-09 14:34:00 +00:00
|
|
|
h := new(toolkit.MkDirHelper)
|
|
|
|
res.StateDir, _ = h.Maybe(toolkit.MkDir(stateDirPath, true))
|
|
|
|
res.RuntimeDir, _ = h.Maybe(toolkit.MkDir(runtimeDirPath, true))
|
|
|
|
|
|
|
|
if err := h.Err(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
2024-09-07 13:11:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Jigs
|
|
|
|
|
|
|
|
func envOr(name string, fallback func() string) string {
|
|
|
|
if v := os.Getenv(name); v != "" {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return fallback()
|
|
|
|
}
|