-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_callbacks.go
148 lines (117 loc) · 3.38 KB
/
file_callbacks.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package aferomock
import (
"io/fs"
"github.com/spf13/afero"
)
var _ fs.File = (*FileCallbacks)(nil)
// FileCallbacks is a callback-based mock for afero.File.
type FileCallbacks struct {
CloseFunc func() error
NameFunc func() string
ReadFunc func(p []byte) (int, error)
ReadAtFunc func(p []byte, off int64) (int, error)
ReaddirFunc func(count int) ([]fs.FileInfo, error)
ReaddirnamesFunc func(n int) ([]string, error)
SeekFunc func(offset int64, whence int) (int64, error)
StatFunc func() (fs.FileInfo, error)
SyncFunc func() error
TruncateFunc func(size int64) error
WriteFunc func(p []byte) (int, error)
WriteAtFunc func(p []byte, off int64) (int, error)
WriteStringFunc func(s string) (int, error)
}
// Close satisfies the afero.File interface.
func (f FileCallbacks) Close() error {
return f.CloseFunc()
}
// Name satisfies the afero.File interface.
func (f FileCallbacks) Name() string {
return f.NameFunc()
}
// Read satisfies the afero.File interface.
func (f FileCallbacks) Read(p []byte) (int, error) {
return f.ReadFunc(p)
}
// ReadAt satisfies the afero.File interface.
func (f FileCallbacks) ReadAt(p []byte, off int64) (int, error) {
return f.ReadAtFunc(p, off)
}
// Readdir satisfies the afero.File interface.
func (f FileCallbacks) Readdir(count int) ([]fs.FileInfo, error) {
return f.ReaddirFunc(count)
}
// Readdirnames satisfies the afero.File interface.
func (f FileCallbacks) Readdirnames(n int) ([]string, error) {
return f.ReaddirnamesFunc(n)
}
// Seek satisfies the afero.File interface.
func (f FileCallbacks) Seek(offset int64, whence int) (int64, error) {
return f.SeekFunc(offset, whence)
}
// Stat satisfies the afero.File interface.
func (f FileCallbacks) Stat() (fs.FileInfo, error) {
return f.StatFunc()
}
// Sync satisfies the afero.File interface.
func (f FileCallbacks) Sync() error {
return f.SyncFunc()
}
// Truncate satisfies the afero.File interface.
func (f FileCallbacks) Truncate(size int64) error {
return f.TruncateFunc(size)
}
// Write satisfies the afero.File interface.
func (f FileCallbacks) Write(p []byte) (int, error) {
return f.WriteFunc(p)
}
// WriteAt satisfies the afero.File interface.
func (f FileCallbacks) WriteAt(p []byte, off int64) (int, error) {
return f.WriteAtFunc(p, off)
}
// WriteString satisfies the afero.File interface.
func (f FileCallbacks) WriteString(s string) (int, error) {
return f.WriteStringFunc(s)
}
// OverrideFile overrides the afero.File methods with the provided callbacks.
func OverrideFile(file afero.File, c FileCallbacks) FileCallbacks { //nolint: cyclop,dupl
if c.CloseFunc == nil {
c.CloseFunc = file.Close
}
if c.NameFunc == nil {
c.NameFunc = file.Name
}
if c.ReadFunc == nil {
c.ReadFunc = file.Read
}
if c.ReadAtFunc == nil {
c.ReadAtFunc = file.ReadAt
}
if c.ReaddirFunc == nil {
c.ReaddirFunc = file.Readdir
}
if c.ReaddirnamesFunc == nil {
c.ReaddirnamesFunc = file.Readdirnames
}
if c.SeekFunc == nil {
c.SeekFunc = file.Seek
}
if c.StatFunc == nil {
c.StatFunc = file.Stat
}
if c.SyncFunc == nil {
c.SyncFunc = file.Sync
}
if c.TruncateFunc == nil {
c.TruncateFunc = file.Truncate
}
if c.WriteFunc == nil {
c.WriteFunc = file.Write
}
if c.WriteAtFunc == nil {
c.WriteAtFunc = file.WriteAt
}
if c.WriteStringFunc == nil {
c.WriteStringFunc = file.WriteString
}
return c
}