aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/environ.go
blob: 25f4496486c4ebefba3bf56c21a82ffc80d1108f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package internal

import (
	"fmt"
	"os"
	"path"
	"strconv"
	"sync"

	"git.ophivana.moe/cat/fortify/internal/verbose"
)

// state that remain constant for the lifetime of the process
// fetched and cached here

const (
	xdgRuntimeDir = "XDG_RUNTIME_DIR"
)

// SystemConstants contains state from the operating system
type SystemConstants struct {
	// path to shared directory e.g. /tmp/fortify.%d
	SharePath string `json:"share_path"`
	// XDG_RUNTIME_DIR value e.g. /run/user/%d
	RuntimePath string `json:"runtime_path"`
	// application runtime directory e.g. /run/user/%d/fortify
	RunDirPath string `json:"run_dir_path"`
}

var (
	scVal  SystemConstants
	scOnce sync.Once
)

func copySC() {
	sc := SystemConstants{
		SharePath: path.Join(os.TempDir(), "fortify."+strconv.Itoa(os.Geteuid())),
	}

	verbose.Println("process share directory at", sc.SharePath)

	// runtimePath, runDirPath
	if r, ok := os.LookupEnv(xdgRuntimeDir); !ok {
		fmt.Println("Env variable", xdgRuntimeDir, "unset")
		os.Exit(1)
	} else {
		sc.RuntimePath = r
		sc.RunDirPath = path.Join(sc.RuntimePath, "fortify")
		verbose.Println("XDG runtime directory at", sc.RunDirPath)
	}

	scVal = sc
}

// GetSC returns a populated SystemConstants value
func GetSC() SystemConstants {
	scOnce.Do(copySC)
	return scVal
}