Added UserCache{File,Dir} and renamed User{File,Dir} to UserConfig{File,Dir}.

This commit is contained in:
Sander Schobers 2021-08-09 19:15:08 +02:00
parent 5e4afbe038
commit a6415a1d60

33
io.go
View File

@ -132,8 +132,31 @@ func NewTempDir(prefix string) (*Dir, error) {
return &Dir{path}, nil
}
// UserDir gives back the user configuration directory with given name.
func UserDir(name string) (string, error) {
// UserCacheDir gives back the user configuration directory with given name.
func UserCacheDir(name string) (string, error) {
config, err := os.UserCacheDir()
if err != nil {
return "", err
}
dir := filepath.Join(config, name)
err = os.MkdirAll(dir, 0777)
if err != nil {
return "", err
}
return dir, nil
}
// UserCacheFile gives back the path to the file with the specified name and the directory named app.
func UserCacheFile(app, name string) (string, error) {
dir, err := UserCacheDir(app)
if err != nil {
return "", err
}
return filepath.Join(dir, name), nil
}
// UserConfigDir gives back the user configuration directory with given name.
func UserConfigDir(name string) (string, error) {
config, err := os.UserConfigDir()
if err != nil {
return "", err
@ -146,9 +169,9 @@ func UserDir(name string) (string, error) {
return dir, nil
}
// UserFile gives back the path to the file with the specified name and the directory named app.
func UserFile(app, name string) (string, error) {
dir, err := UserDir(app)
// UserConfigFile gives back the path to the file with the specified name and the directory named app.
func UserConfigFile(app, name string) (string, error) {
dir, err := UserConfigDir(app)
if err != nil {
return "", err
}