All checks were successful
		
		
	
	Test / Create distribution (push) Successful in 34s
				
			Test / Sandbox (push) Successful in 2m16s
				
			Test / Hakurei (push) Successful in 3m10s
				
			Test / Hpkg (push) Successful in 4m1s
				
			Test / Sandbox (race detector) (push) Successful in 4m14s
				
			Test / Hakurei (race detector) (push) Successful in 4m57s
				
			Test / Flake checks (push) Successful in 1m28s
				
			This now places rundir inside the fallback runtime dir, so special case in internal/outcome is avoided. Signed-off-by: Ophestra <cat@gensokyo.uk>
		
			
				
	
	
		
			66 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Package env provides the [Paths] struct for efficiently building paths from the environment.
 | 
						|
package env
 | 
						|
 | 
						|
import (
 | 
						|
	"log"
 | 
						|
	"os"
 | 
						|
	"strconv"
 | 
						|
 | 
						|
	"hakurei.app/container/check"
 | 
						|
	"hakurei.app/hst"
 | 
						|
)
 | 
						|
 | 
						|
// Paths holds paths copied from the environment and is used to create [hst.Paths].
 | 
						|
type Paths struct {
 | 
						|
	// TempDir is returned by [os.TempDir].
 | 
						|
	TempDir *check.Absolute
 | 
						|
	// RuntimePath is copied from $XDG_RUNTIME_DIR.
 | 
						|
	RuntimePath *check.Absolute
 | 
						|
}
 | 
						|
 | 
						|
// Copy expands [Paths] into [hst.Paths].
 | 
						|
func (env *Paths) Copy(v *hst.Paths, userid int) {
 | 
						|
	if env == nil || env.TempDir == nil || v == nil {
 | 
						|
		panic("attempting to use an invalid Paths")
 | 
						|
	}
 | 
						|
 | 
						|
	v.TempDir = env.TempDir
 | 
						|
	v.SharePath = env.TempDir.Append("hakurei." + strconv.Itoa(userid))
 | 
						|
 | 
						|
	if env.RuntimePath == nil {
 | 
						|
		// fall back to path in share since hakurei has no hard XDG dependency
 | 
						|
		v.RuntimePath = v.SharePath.Append("compat")
 | 
						|
	} else {
 | 
						|
		v.RuntimePath = env.RuntimePath
 | 
						|
	}
 | 
						|
	v.RunDirPath = v.RuntimePath.Append("hakurei")
 | 
						|
}
 | 
						|
 | 
						|
// CopyPaths returns a populated [Paths].
 | 
						|
func CopyPaths() *Paths { return CopyPathsFunc(log.Fatalf, os.TempDir, os.Getenv) }
 | 
						|
 | 
						|
// CopyPathsFunc returns a populated [Paths],
 | 
						|
// using the provided [log.Fatalf], [os.TempDir], [os.Getenv] functions.
 | 
						|
func CopyPathsFunc(
 | 
						|
	fatalf func(format string, v ...any),
 | 
						|
	tempdir func() string,
 | 
						|
	getenv func(key string) string,
 | 
						|
) *Paths {
 | 
						|
	const xdgRuntimeDir = "XDG_RUNTIME_DIR"
 | 
						|
 | 
						|
	var env Paths
 | 
						|
 | 
						|
	if tempDir, err := check.NewAbs(tempdir()); err != nil {
 | 
						|
		fatalf("invalid TMPDIR: %v", err)
 | 
						|
		panic("unreachable")
 | 
						|
	} else {
 | 
						|
		env.TempDir = tempDir
 | 
						|
	}
 | 
						|
 | 
						|
	if a, err := check.NewAbs(getenv(xdgRuntimeDir)); err == nil {
 | 
						|
		env.RuntimePath = a
 | 
						|
	}
 | 
						|
 | 
						|
	return &env
 | 
						|
}
 |