Added methods to retrieve the path to the user configuration dir.

This commit is contained in:
Sander Schobers 2020-05-15 11:38:21 +02:00
parent 5a1e5f6f7f
commit c0c5235d5a

24
io.go
View File

@ -8,6 +8,7 @@ import (
"image/png" "image/png"
"io" "io"
"os" "os"
"path/filepath"
) )
// CreateJSONDecoder create a new a generic JSON decoder for the specified value. // 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. // PNGEncoder is a generic PNG encoder.
func PNGEncoder(w io.Writer, value interface{}) error { return png.Encode(w, value.(image.Image)) } 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
}