60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
// Package env manages the daemon-global environment for ollie.
|
|
// It loads $XDG_CONFIG_HOME/ollie/env and provides defaults,
|
|
// formatted for export to frontends via ollie/env.
|
|
package env
|
|
|
|
import (
|
|
"bufio"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"ollie/paths"
|
|
)
|
|
|
|
// EnsureDefaults loads $XDG_CONFIG_HOME/ollie/env and sets default values for any
|
|
// OLLIE_* vars not already present in the process environment.
|
|
func EnsureDefaults() {
|
|
LoadFile(filepath.Join(paths.CfgDir(), "env"))
|
|
}
|
|
|
|
// Set sets a variable in the process environment.
|
|
func Set(k, v string) { os.Setenv(k, v) } //nolint:errcheck
|
|
|
|
// LoadFile reads KEY=VALUE pairs from path and sets any key not already
|
|
// present in the environment. Lines beginning with # and blank lines are
|
|
// ignored. Missing file is silently ignored.
|
|
func LoadFile(path string) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer f.Close()
|
|
|
|
sc := bufio.NewScanner(f)
|
|
for sc.Scan() {
|
|
line := strings.TrimSpace(sc.Text())
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
k, v, ok := strings.Cut(line, "=")
|
|
if !ok {
|
|
continue
|
|
}
|
|
k = strings.TrimSpace(k)
|
|
if i := strings.Index(v, " #"); i >= 0 {
|
|
v = v[:i]
|
|
}
|
|
v = strings.TrimSpace(v)
|
|
if len(v) >= 2 && (v[0] == '"' && v[len(v)-1] == '"' || v[0] == '\'' && v[len(v)-1] == '\'') {
|
|
v = v[1 : len(v)-1]
|
|
}
|
|
if k != "" && os.Getenv(k) == "" {
|
|
os.Setenv(k, paths.ExpandHome(os.ExpandEnv(v))) //nolint:errcheck
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get returns a variable from the process environment.
|
|
func Get(k string) string { return os.Getenv(k) }
|