diff --git a/config/config.go b/config/config.go index 1cf2faa..5b6f551 100644 --- a/config/config.go +++ b/config/config.go @@ -124,19 +124,6 @@ func (config *Config) Load (name string) (err error) { return } - var configHome string - configHome, err = getConfigHome() - if err != nil { return } - println(configHome) - - configDirsString := os.Getenv("XDG_CONFIG_DIRS") - if configDirsString == "" { - configDirsString = "/etc/xdg/" - } - - configDirs := strings.Split(configDirsString, ":") - configDirs = append(configDirs, configHome) - for _, directory := range configDirs { path := filepath.Join(directory, name, name + ".conf") @@ -247,10 +234,6 @@ func (config *Config) Save (name string) (err error) { return } - var configHome string - configHome, err = getConfigHome() - if err != nil { return } - err = os.MkdirAll(configHome, 0755) if err != nil { return } @@ -266,7 +249,8 @@ func (config *Config) Save (name string) (err error) { return } -// SaveTo writes the configuration data to the specified io.Writer. +// SaveTo writes the configuration data to the specified io.Writer. Keys are +// alphabetically sorted. func (config *Config) SaveTo (writer io.Writer) (err error) { keys := make([]string, len(config.Parameters)) index := 0 @@ -334,19 +318,6 @@ func parseColor (value string) (valueColor color.Color, err error) { return } -func getConfigHome () (home string, err error) { - var homeDirectory string - homeDirectory, err = os.UserHomeDir() - if err != nil { return } - - home = os.Getenv("XDG_CONFIG_HOME") - if home == "" { - home = filepath.Join(homeDirectory, "/.config/") - } - - return -} - func nameIsIllegal (name string) (legal bool) { legal = strings.ContainsAny(name, "/\\|:.%") return diff --git a/config/xdg.go b/config/xdg.go new file mode 100644 index 0000000..1eee13d --- /dev/null +++ b/config/xdg.go @@ -0,0 +1,54 @@ +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) + + configHome = os.Getenv("XDG_DATA_HOME") + if configHome == "" { + configHome = filepath.Join(homeDirectory, "/.local/share/") + } + + cacheHome = os.Getenv("XDG_CACHE_HOME") + if cacheHome == "" { + configHome = filepath.Join(homeDirectory, "/.cache/") + } +} + +// 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 +}