Based entirely on the documentation and not rust code.
Pulled in handy Golang stuff where appropriate.
This commit is contained in:
Martyn 2021-12-29 01:49:56 +00:00
parent 4426128c32
commit 73d7493c5d
2 changed files with 107 additions and 0 deletions

104
base/base.go Normal file
View File

@ -0,0 +1,104 @@
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.UserCacheDir()
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()")
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.martyn.berlin/martyn/golang-directories
go 1.17