tins2021/io.go

44 lines
826 B
Go
Raw Permalink Normal View History

package tins2021
import (
2021-08-11 06:03:02 +00:00
"encoding/json"
"os"
"opslag.de/schobers/zntg"
)
const appName = "tins2021_qbitter"
2021-08-11 06:03:02 +00:00
func UserDir() (string, error) { return zntg.UserConfigDir(appName) }
func UserFile(name string) (string, error) { return zntg.UserConfigFile(appName, name) }
func LoadUserFileJSON(name string, v interface{}) error {
path, err := UserFile(name)
if err != nil {
return err
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.ErrNotExist
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return json.NewDecoder(f).Decode(v)
}
2021-08-11 06:03:02 +00:00
func SaveUserFileJSON(name string, v interface{}) error {
path, err := UserFile(name)
if err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return json.NewEncoder(f).Encode(v)
}