From c0c5235d5a56f06f6f3366a336d1fa5639c06edf Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Fri, 15 May 2020 11:38:21 +0200 Subject: [PATCH] Added methods to retrieve the path to the user configuration dir. --- io.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/io.go b/io.go index 8a99469..9b3dbb6 100644 --- a/io.go +++ b/io.go @@ -8,6 +8,7 @@ import ( "image/png" "io" "os" + "path/filepath" ) // CreateJSONDecoder create a new a generic JSON decoder for the specified value. @@ -89,3 +90,26 @@ var _ EncoderFn = PNGEncoder // PNGEncoder is a generic PNG encoder. func PNGEncoder(w io.Writer, value interface{}) error { return png.Encode(w, value.(image.Image)) } + +// UserDir gives back the user configuration directory with given name. +func UserDir(name string) (string, error) { + config, err := os.UserConfigDir() + if err != nil { + return "", err + } + dir := filepath.Join(config, name) + err = os.MkdirAll(dir, 0777) + if err != nil { + return "", err + } + 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) + if err != nil { + return "", err + } + return filepath.Join(dir, name), nil +}