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 +}