51 lines
829 B
Go
51 lines
829 B
Go
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
|
|
}
|
|
dir := filepath.Join(config, "tins2020")
|
|
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
|
|
}
|