stone/config/xdg.go

55 lines
1.2 KiB
Go
Raw Normal View History

2022-11-26 21:47:59 -07:00
package config
import "os"
import "strings"
import "path/filepath"
var homeDirectory string
var configHome string
var configDirs []string
var dataHome string
var cacheHome string
func init () {
var err error
homeDirectory, err = os.UserHomeDir()
if err != nil {
panic("could not get user home directory: " + err.Error())
}
configHome = os.Getenv("XDG_CONFIG_HOME")
if configHome == "" {
configHome = filepath.Join(homeDirectory, "/.config/")
}
configDirsString := os.Getenv("XDG_CONFIG_DIRS")
if configDirsString == "" {
configDirsString = "/etc/xdg/"
}
configDirs = append(strings.Split(configDirsString, ":"), configHome)
2022-11-26 22:16:10 -07:00
dataHome = os.Getenv("XDG_DATA_HOME")
if dataHome == "" {
dataHome = filepath.Join(homeDirectory, "/.local/share/")
2022-11-26 21:47:59 -07:00
}
cacheHome = os.Getenv("XDG_CACHE_HOME")
if cacheHome == "" {
2022-11-26 22:16:10 -07:00
cacheHome = filepath.Join(homeDirectory, "/.cache/")
2022-11-26 21:47:59 -07:00
}
}
// DataHome returns the path to the directory where user data should be stored.
func DataHome (name string) (home string) {
home = filepath.Join(dataHome, name)
return
}
// CacheHome returns the path to the directory where cache files should be
// stored.
func CacheHome (name string) (home string) {
home = filepath.Join(cacheHome, name)
return
}