virt

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 12 Imported by: 2

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(file fs.File) ([]byte, error)

func Open

func Open(f *File) fs.File

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 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(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
	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(file fs.File) (entry *File, err error)

From 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) 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.

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) 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) RemoveAll

func (dir OS) RemoveAll(path string) error

func (OS) WriteFile

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

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) 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