Ophestra Umiker
8bdae74ebe
State query command has been moved to main where it belongs, "system" information are now fetched in app.New and stored in *App with accessors for relevant values. Exit (cleanup-related) functions are separated into its dedicated "final" package. Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package util
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"git.ophivana.moe/cat/fortify/internal/final"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
const (
|
|
systemdCheckPath = "/run/systemd/system"
|
|
|
|
home = "HOME"
|
|
xdgConfigHome = "XDG_CONFIG_HOME"
|
|
|
|
PulseServer = "PULSE_SERVER"
|
|
PulseCookie = "PULSE_COOKIE"
|
|
)
|
|
|
|
// SdBooted implements https://www.freedesktop.org/software/systemd/man/sd_booted.html
|
|
func SdBooted() (bool, error) {
|
|
_, err := os.Stat(systemdCheckPath)
|
|
if err != nil {
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
err = nil
|
|
}
|
|
return false, err
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// DiscoverPulseCookie try various standard methods to discover the current user's PulseAudio authentication cookie
|
|
func DiscoverPulseCookie() string {
|
|
if p, ok := os.LookupEnv(PulseCookie); ok {
|
|
return p
|
|
}
|
|
|
|
if p, ok := os.LookupEnv(home); ok {
|
|
p = path.Join(p, ".pulse-cookie")
|
|
if s, err := os.Stat(p); err != nil {
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
|
final.Fatal("Error accessing PulseAudio cookie:", err)
|
|
// unreachable
|
|
return p
|
|
}
|
|
} else if !s.IsDir() {
|
|
return p
|
|
}
|
|
}
|
|
|
|
if p, ok := os.LookupEnv(xdgConfigHome); ok {
|
|
p = path.Join(p, "pulse", "cookie")
|
|
if s, err := os.Stat(p); err != nil {
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
|
final.Fatal("Error accessing PulseAudio cookie:", err)
|
|
// unreachable
|
|
return p
|
|
}
|
|
} else if !s.IsDir() {
|
|
return p
|
|
}
|
|
}
|
|
|
|
final.Fatal(fmt.Sprintf("Cannot locate PulseAudio cookie (tried $%s, $%s/pulse/cookie, $%s/.pulse-cookie)",
|
|
PulseCookie, xdgConfigHome, home))
|
|
return ""
|
|
}
|