virt

package module
v0.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2025 License: MIT Imports: 14 Imported by: 5

README

virt

Go Reference

Virtual file utilities. Extracted from bud.

Features

  • Create filesystems from different types (e.g. Map, Tree, List, OS)
  • Supports printing out filesystems in a tree format
  • Supports writing and syncing filesystems
  • Supports serializing and deserializing filesystems
  • Supports creating filesystems that exclude certain files

Install

go get github.com/matthewmueller/virt

Contributors

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Now = func() time.Time {
	return time.Now()
}

Now may be overridden for testing purposes

Functions

func Exclude

func Exclude(fsys fs.FS, fn func(path string) bool) fs.FS

func MarshalJSON

func MarshalJSON(path string, file fs.File) ([]byte, error)

func Merge added in v0.0.7

func Merge(fileSystems ...fs.FS) *mergedFS

Merge the filesystems together. When there are conflicts, the earlier filesystem has priority.

func Mount added in v0.0.7

func Mount(dir string, fsys fs.FS) fs.FS

func Print

func Print(fsys fs.FS, subpaths ...string) (string, error)

Print out a virtual filesystem.

func Sync

func Sync(from fs.FS, toDir string, subpaths ...string) error

func SyncFS

func SyncFS(from fs.FS, to FS, subpaths ...string) error

Sync files from one filesystem to another at subpath

func To added in v0.0.9

func To(f *File) fs.File

To converts a *virt.File to an fs.File.

func UnmarshalJSON

func UnmarshalJSON(file []byte) (fs.File, error)

func Write

func Write(fsys fs.FS, toDir string, subpaths ...string) error

Write a fileystem to a directory. Unlike sync, it does not attempt to remove files that are not in the source filesystem. This is sugar on top of WriteFS, commonly used for testing.

func WriteFS

func WriteFS(from fs.FS, to FS, subpaths ...string) error

WriteFS writes files from one filesystem to another at subpath. Unlike sync, it does not attempt to remove files that are not in the source filesystem.

Types

type DirEntry

type DirEntry struct {
	Path    string
	Size    int64
	Mode    fs.FileMode
	ModTime time.Time
}

func FromEntry added in v0.0.2

func FromEntry(path string, de fs.DirEntry) (*DirEntry, error)

FromEntry converts a fs.DirEntry to a virtual DirEntry

func (*DirEntry) Info

func (e *DirEntry) Info() (fs.FileInfo, error)

func (*DirEntry) IsDir

func (e *DirEntry) IsDir() bool

func (*DirEntry) Name

func (e *DirEntry) Name() string

func (*DirEntry) Type

func (e *DirEntry) Type() fs.FileMode

type FS

type FS interface {
	fs.FS
	OpenFile(name string, flag int, perm fs.FileMode) (RWFile, error)
	MkdirAll(path string, perm fs.FileMode) error
	WriteFile(name string, data []byte, perm fs.FileMode) error
	RemoveAll(path string) error
}

FS is a virtual filesystem interface. It extends the fs.FS interface with methods for creating and removing files and directories.

func Sub

func Sub(fsys FS, dir string) (FS, error)

Sub returns a new filesystem rooted at dir.

type File

type File struct {
	Path    string
	Data    []byte
	Mode    fs.FileMode
	ModTime time.Time
	Entries []*DirEntry
}

func From

func From(fsys fs.FS, path string) (entry *File, err error)

FromFile a file to a virtual file

func FromFile added in v0.0.9

func FromFile(path string, file fs.File) (*File, error)

FromFile a file to a virtual file

func (*File) Entry added in v0.0.2

func (f *File) Entry() *DirEntry

Entry returns a DirEntry for the file.

func (*File) Hash added in v0.0.10

func (f *File) Hash(h hash.Hash) []byte

Hash the results of a file

func (*File) Info

func (f *File) Info() (fs.FileInfo, error)

Returns the file info. Implements the fs.DirEntry interface.

func (*File) IsDir

func (f *File) IsDir() bool

Returns true if entry is a directory. Implements the fs.DirEntry interface.

func (*File) Name

func (f *File) Name() string

Name of the entry. Implements the fs.DirEntry interface.

func (*File) Stamp

func (f *File) Stamp() (stamp string, err error)

Stamp helps quickly determine if a file has changed.

func (*File) Type

func (f *File) Type() fs.FileMode

Returns the type of entry. Implements the fs.DirEntry interface.

func (*File) Write added in v0.0.12

func (f *File) Write(p []byte) (int, error)

Write data to the file

func (*File) WriteString added in v0.0.12

func (f *File) WriteString(s string) (int, error)

WriteString writes a string to the file

type List

type List []*File

List is meant to be a simple list of files. It's not a tree of files and you can't walk it. Use Tree if you need a more capable filesystem. This filesytem is not safe for concurrent use.

func (*List) MkdirAll

func (fsys *List) MkdirAll(path string, perm fs.FileMode) error

Mkdir create a directory.

func (List) Open

func (fsys List) Open(path string) (fs.File, error)

func (List) OpenFile added in v0.0.10

func (fsys List) OpenFile(path string, flag int, perm fs.FileMode) (RWFile, error)

func (*List) RemoveAll

func (fsys *List) RemoveAll(path string) error

Remove removes a path

func (*List) WriteFile

func (fsys *List) WriteFile(path string, data []byte, perm fs.FileMode) error

WriteFile writes a file

type Map

type Map map[string]string

Map is a simple in-memory filesystem. This filesytem is not safe for concurrent use.

func (Map) Open

func (m Map) Open(name string) (fs.File, error)

type OS

type OS string

OS creates a new OS filesystem rooted at the given directory. TODO: create an os_windows for opening on multiple drives with the same API: https://github.com/golang/go/issues/44279#issuecomment-955766528

func (OS) MkdirAll

func (dir OS) MkdirAll(path string, perm fs.FileMode) error

func (OS) Open

func (dir OS) Open(name string) (fs.File, error)

func (OS) OpenFile added in v0.0.10

func (dir OS) OpenFile(name string, flag int, perm fs.FileMode) (RWFile, error)

func (OS) ReadDir added in v0.0.10

func (dir OS) ReadDir(name string) ([]fs.DirEntry, error)

func (OS) RemoveAll

func (dir OS) RemoveAll(path string) error

func (OS) WriteFile

func (dir OS) WriteFile(name string, data []byte, perm fs.FileMode) error

type RWFile added in v0.0.11

type RWFile interface {
	fs.File
	io.WriteCloser
}

RWFile is a virtual file interface. It extends fs.FS to support reading and writing files.

type Tree

type Tree map[string]*File

File represents a file or directory in a virtual filesystem. Unlike virt.Map, the Tree filesystem can be traversed (similar to fstest.MapFS) and written to.

func (Tree) MkdirAll

func (t Tree) MkdirAll(path string, perm fs.FileMode) error

Mkdir create a directory.

func (Tree) Open

func (fsys Tree) Open(path string) (fs.File, error)

func (Tree) OpenFile added in v0.0.10

func (fsys Tree) OpenFile(path string, flag int, perm fs.FileMode) (RWFile, error)

func (Tree) RemoveAll

func (t Tree) RemoveAll(path string) error

Remove removes a path

func (Tree) WriteFile

func (t Tree) WriteFile(path string, data []byte, perm fs.FileMode) error

WriteFile writes a file TODO: WriteFile should fail if path.Dir(name) doesn't exist

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL