forked from matryer/persist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpersist.go
48 lines (39 loc) · 1.07 KB
/
persist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// SPDX-License-Identifier: MIT
package persist
import (
"encoding/json"
"os"
"github.com/pkg/errors"
)
// Save saves a representation of v to the file at path.
func Save(f *os.File, v interface{}) error {
data, err := json.MarshalIndent(v, "", "\t")
if err != nil {
return errors.Wrapf(err, "error marshaling value of type %T", v)
}
_, err = f.Seek(0, 0)
if err != nil {
return errors.Wrap(err, "error seeking to beginning of file")
}
err = f.Truncate(0)
if err != nil {
return errors.Wrap(err, "error truncating file")
}
_, err = f.Write(data)
if err != nil {
return errors.Wrap(err, "error copying data into file")
}
_, err = f.Seek(0, 0)
return errors.Wrap(err, "error seeking to beginning of file")
}
// Load loads the file at path into v.
// Use os.IsNotExist() to see if the returned error is due
// to the file being missing.
func Load(f *os.File, v interface{}) error {
_, err := f.Seek(0, 0)
if err != nil {
return errors.Wrap(err, "error reetting reader")
}
err = json.NewDecoder(f).Decode(v)
return errors.Wrap(err, "error decoding value")
}