| 1 |
package memfs |
| 2 |
|
| 3 |
type Option interface { |
| 4 |
setOption(*fsOption) |
| 5 |
} |
| 6 |
|
| 7 |
type fsOption struct { |
| 8 |
openHook func(path string, existingContent []byte, origErr error) ([]byte, error) |
| 9 |
} |
| 10 |
|
| 11 |
type openHookOption struct { |
| 12 |
hook func(string, []byte, error) ([]byte, error) |
| 13 |
} |
| 14 |
|
| 15 |
func (o *openHookOption) setOption(fsOpt *fsOption) { |
| 16 |
fsOpt.openHook = o.hook |
| 17 |
} |
| 18 |
|
| 19 |
// WithOpenHook returns an Option that sets a hook function to be called |
| 20 |
// when opening files in the MemFS. |
| 21 |
// |
| 22 |
// The hook function takes three parameters: |
| 23 |
// - path: the path of the file being opened |
| 24 |
// - content: the original content of the file (may be nil if the file doesn't exist) |
| 25 |
// - origError: the original error returned when trying to open the file (may be nil) |
| 26 |
// |
| 27 |
// The hook function returns: |
| 28 |
// - []byte: the new content of the file |
| 29 |
// - error: any error that occurred during the hook's execution |
| 30 |
func WithOpenHook(f func(string, []byte, error) ([]byte, error)) Option { |
| 31 |
return &openHookOption{ |
| 32 |
hook: f, |
| 33 |
} |
| 34 |
} |