From a6415a1d60e68ee85130a1b3c26d2be7b042b8ae Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 9 Aug 2021 19:15:08 +0200 Subject: [PATCH] Added UserCache{File,Dir} and renamed User{File,Dir} to UserConfig{File,Dir}. --- io.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/io.go b/io.go index f45470b..7756881 100644 --- a/io.go +++ b/io.go @@ -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 }