Golang implementation of https://lib.rs/crates/directories
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
directories/base/base.go

108 lines
2.1 KiB

//go:build !android && !ios
// +build !android,!ios
package base
import (
"errors"
"os"
"runtime"
)
func Home() (string, error) {
dir, err := os.UserHomeDir()
return dir, err
}
func Cache() (string, error) {
dir, err := os.UserCacheDir()
return dir, err
}
func Config() (string, error) {
dir, err := os.UserConfigDir()
return dir, err
}
func Data() (string, error) {
switch runtime.GOOS {
case "linux":
e, ok := os.LookupEnv("XDG_DATA_HOME")
if ok {
return e, nil
} else {
h, err := Home()
return h + "/.local/share", err
}
case "windows":
return Config()
case "darwin":
return Config()
}
return "", errors.New("could not determine location for Data()")
}
func DataLocal() (string, error) {
switch runtime.GOOS {
case "linux":
return Data()
case "windows":
return Cache()
case "darwin":
return Data()
}
return "", errors.New("could not determine location for DataLocal()")
}
func Executable() (string, error) {
switch runtime.GOOS {
case "linux":
e, ok := os.LookupEnv("XDG_BIN_HOME")
if ok {
return e, nil
} else {
h, err := Home()
return h + "/.local/bin", err
}
}
return "", errors.New("could not determine location for Executable()")
}
func Preferences() (string, error) {
switch runtime.GOOS {
case "linux":
return Config()
case "windows":
return Config()
case "darwin":
h, err := Home()
return h + "/Library/Preferences", err
}
return "", errors.New("could not determine location for Preferences()")
}
func Runtime() (string, error) {
switch runtime.GOOS {
case "linux":
e, ok := os.LookupEnv("XDG_CONFIG_HOME")
if ok {
return e, nil
}
}
return "", errors.New("could not determine location for Runtime()")
}
func State() (string, error) {
switch runtime.GOOS {
case "linux":
e, ok := os.LookupEnv("XDG_STATE_HOME")
if ok {
return e, nil
} else {
h, err := Home()
return h + "/.local/state", err
}
}
return "", errors.New("could not determine location for State()")
}