Add more xdg stuff

This commit is contained in:
Sasha Koshka 2022-11-26 23:47:59 -05:00
parent 19895e6049
commit 1435c02354
2 changed files with 56 additions and 31 deletions

View File

@ -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

54
config/xdg.go Normal file
View File

@ -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
}