-
Notifications
You must be signed in to change notification settings - Fork 8
/
iofs.go
34 lines (27 loc) · 882 Bytes
/
iofs.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
package filesystem
import (
"fmt"
"io/fs"
"github.com/spf13/afero"
"github.com/ARM-software/golang-utils/utils/commonerrors"
)
type wrappedFS struct {
filesystem FS
}
func (w *wrappedFS) Open(name string) (fs.File, error) {
return w.filesystem.GenericOpen(name)
}
// ConvertToIOFilesystem converts a filesystem FS to a io/fs FS
func ConvertToIOFilesystem(filesystem FS) (fs.FS, error) {
if filesystem == nil {
return nil, fmt.Errorf("%w: missing filesystem", commonerrors.ErrUndefined)
}
return &wrappedFS{filesystem: filesystem}, nil
}
// ConvertFromIOFilesystem converts an io/fs FS into a FS
func ConvertFromIOFilesystem(filesystem fs.FS) (FS, error) {
if filesystem == nil {
return nil, fmt.Errorf("%w: missing filesystem", commonerrors.ErrUndefined)
}
return NewVirtualFileSystem(afero.FromIOFS{FS: filesystem}, Custom, IdentityPathConverterFunc), nil
}