44 lines
826 B
Go
44 lines
826 B
Go
package tins2021
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
|
|
"opslag.de/schobers/zntg"
|
|
)
|
|
|
|
const appName = "tins2021_qbitter"
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|