tins2020/io.go

51 lines
841 B
Go
Raw Normal View History

2020-05-08 16:38:26 +00:00
package tins2020
import (
"encoding/json"
"os"
"path/filepath"
)
func DecodeJSON(path string, v interface{}) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
err = json.NewDecoder(f).Decode(v)
if err != nil {
return err
}
return nil
}
func EncodeJSON(path string, v interface{}) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return json.NewEncoder(f).Encode(v)
}
func UserDir() (string, error) {
config, err := os.UserConfigDir()
if err != nil {
return "", err
}
2020-05-11 00:52:42 +00:00
dir := filepath.Join(config, "tins2020_flowers_sim")
2020-05-08 16:38:26 +00:00
err = os.MkdirAll(dir, 0777)
if err != nil {
return "", err
}
return dir, nil
}
func UserFile(name string) (string, error) {
dir, err := UserDir()
if err != nil {
return "", err
}
return filepath.Join(dir, name), nil
}