WFS provides an interface that abstracts the OS filesystem, enabling support for custom writable filesystems and improved testing.
The Go standard library provides powerful filesystem primitives, but handling files across different storage backends can be cumbersome. While fs.FS is a flexible and useful abstraction, it is inherently read-only by design. wfs extends this concept by providing a writable filesystem interface, allowing seamless integration with custom backends, such as in-memory filesystems for testing.
go get github.com/eriicafes/wfswfs provides interfaces and top-level functions for working with files and directories.
Create an OS writable filesystem or an in-memory writable filesystem for use in tests.
// os filesystem
fsys := wfs.OS()
// in-memory filesystem
fsys := wfs.Map(fstest.MapFS{})A wfs.FS implements fs.FS, wfs.FileFS and wfs.DirFS.
A wfs.FS implementation can read, write, create and delete files and directories.
type FS interface {
fs.FS
FileFS
DirFS
}A wfs.FileFS implementation can create, read, write and delete files.
type FileFS interface {
OpenFile(name string, flag int, perm fs.FileMode) (File, error)
Rename(oldpath, newpath string) error
Remove(name string) error
RemoveAll(path string) error
}A wfs.DirFS implementation can create directories.
type DirFS interface {
Mkdir(name string, perm fs.FileMode) error
MkdirAll(path string, perm fs.FileMode) error
}A wfs.File implementation extends fs.File with additional methods.
type File interface {
fs.File
io.WriteSeeker
io.ReaderAt
io.WriterAt
Truncate(size int64) error
Name() string
}Creates a new file or truncates an existing one, returning a writable file handle.
f, err := wfs.Create(fsys, "filename")Writes data to a file, creating or replacing it while preserving file permissions.
err := wfs.WriteFile(fsys, "filename", []byte(`data`), fs.ModePerm)