fileutil: add directory helpers, atomic file writing (#5477)

This commit is contained in:
Caleb Doxsey 2025-02-19 07:56:38 -07:00 committed by GitHub
parent b9fd926618
commit fbd1f34110
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 133 additions and 27 deletions

View file

@ -0,0 +1,36 @@
package fileutil
import (
"os"
"path/filepath"
"github.com/rs/zerolog/log"
)
// CacheDir returns $XDG_CACHE_HOME/pomerium, or $HOME/.cache/pomerium, or /tmp/pomerium/cache
func CacheDir() string {
dir, err := os.UserCacheDir()
if err == nil {
dir = filepath.Join(dir, "pomerium")
} else {
dir = filepath.Join(os.TempDir(), "pomerium", "cache")
log.Error().Msgf("user cache directory not set, defaulting to %s", dir)
}
return dir
}
// DataDir returns $XDG_DATA_HOME/pomerium, or $HOME/.local/share/pomerium, or /tmp/pomerium/data
func DataDir() string {
dir := os.Getenv("XDG_DATA_HOME")
if dir != "" {
dir = filepath.Join(dir, "pomerium")
} else {
if home, err := os.UserHomeDir(); err == nil {
dir = filepath.Join(home, ".local", "share", "pomerium")
} else {
dir = filepath.Join(os.TempDir(), "pomerium", "data")
}
log.Error().Msgf("user data directory not set, defaulting to %s", dir)
}
return dir
}