-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures.go
47 lines (38 loc) · 924 Bytes
/
fixtures.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
package fixtures
import (
"bytes"
"encoding/gob"
"os"
"path/filepath"
"runtime"
)
// basepath is the root directory of this package.
var basepath string
func init() {
_, currentFile, _, _ := runtime.Caller(0)
basepath = filepath.Dir(currentFile)
}
// Path returns the absolute path the given relative file or directory path,
// relative to the "fixtures" directory.
// If rel is already absolute, it is returned unmodified.
func Path(rel string) string {
if filepath.IsAbs(rel) {
return rel
}
return filepath.Join(basepath, rel)
}
func Read(filepath string) ([]byte, error) {
return os.ReadFile(filepath)
}
func Write(filepath string, data []byte) error {
return os.WriteFile(filepath, data, 0644)
}
func Encode(filePath string, a any) error {
var network bytes.Buffer
enc := gob.NewEncoder(&network)
err := enc.Encode(a)
if err != nil {
return err
}
return Write(filePath, network.Bytes())
}