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
|
|
|
"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
|
|
|
|
|
2024-12-16 13:59:11 +00:00
|
|
|
stateDirPath = toolkit.EnvOr(
|
2024-09-09 14:34:00 +00:00
|
|
|
"STATE_DIRECTORY",
|
|
|
|
func() string { return filepath.Join(xdg.StateHome, "isle") },
|
|
|
|
)
|
2024-09-07 13:11:04 +00:00
|
|
|
|
2024-12-16 13:59:11 +00:00
|
|
|
runtimeDirPath = toolkit.EnvOr(
|
2024-09-09 14:34:00 +00:00
|
|
|
"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
|
|
|
})
|