Documentation ¶
Overview ¶
Package memfs provide a library for mapping file system into memory and to generate a go file.
Usage ¶
By default only file with size less or equal to 5 MB will be included in memory. To increase the default size set the MaxFileSize (in bytes). For example, to set maximum file size to 10 MB,
memfs.MaxFileSize = 1024 * 1024 * 10
The first step is to create new instance of memfs using "New()".
incs := []string{ `.*/include`, `.*\.(css|html|js)$`, } excs := []string{ `.*/exclude`, } mfs, err := memfs.New(incs, excs)
and then we mount the directory that we want into memory using "Mount()",
err := mfs.Mount("./testdata")
Later, if we want to get the file from memory, call "Get()" and access the content with "node.V". Remember that if file size is larger that maximum, the content will need to be read manually,
node, err := mfs.Get() if err != nil { // Handle file not exist } if node.mode.IsDir() { // Handle directory. } if node.V == nil { // Handle large file. node.V, err = ioutil.ReadFile(child.SysPath) } // Do something with content of file system.
Thats it!
Go Generate ¶
memfs also support generating the files into Go generated source file. After we Mount the directory, we can call,
mfs.GoGenerate("mypackage", "output/path/file.go")
The Go generated file will be defined with package named "mypackage" in file "output/path/file.go".
Index ¶
- Variables
- type MemFS
- func (mfs *MemFS) AddChild(parent *Node, fi os.FileInfo) (child *Node, err error)
- func (mfs *MemFS) Get(path string) (node *Node, err error)
- func (mfs *MemFS) GoGenerate(pkgName, out string) (err error)
- func (mfs *MemFS) IsMounted() bool
- func (mfs *MemFS) ListNames() (paths []string)
- func (mfs *MemFS) Mount(dir string) error
- func (mfs *MemFS) RemoveChild(parent *Node, child *Node) (removed *Node)
- func (mfs *MemFS) Unmount()
- func (mfs *MemFS) Update(node *Node, newInfo os.FileInfo)
- type Node
- type PathNode
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // MaxFileSize define maximum file size that can be stored on memory. // The default value is 5 MB. MaxFileSize int64 = 1024 * 1024 * 5 //nolint: gochecknoglobals // Development define a flag to bypass file in memory. If its // true, any call to Get will result in direct read to file system. Development bool //nolint: gochecknoglobals // GeneratedPathNode contains the mapping of path and node. Its will // be used and initialized by ".go" file generated from GoGenerate(). GeneratedPathNode *PathNode //nolint: gochecknoglobals )
Functions ¶
This section is empty.
Types ¶
type MemFS ¶
type MemFS struct {
// contains filtered or unexported fields
}
MemFS contains directory tree of file system in memory.
func New ¶
New create and initialize new memory file system using list of regular expresssion for including or excluding files.
The includes and excludes pattern applied to path of file in file system, not to the path in memory.
The "withContent" parameter tell the MemFS to read the content of file and detect its content type. If this paramater is false, the content of file will not be mapped to memory, the MemFS will behave as directory tree.
On directory that contains output from GoGenerate(), the includes and excludes does not have any effect, since the content of path and nodes will be overwritten by GeneratedPathNode.
Example ¶
incs := []string{ `.*/include`, `.*\.(css|html|js)$`, } excs := []string{ `.*/exclude`, } mfs, err := New(incs, excs, true) if err != nil { log.Fatal(err) } err = mfs.Mount("./testdata") if err != nil { log.Fatal(err) } node, err := mfs.Get("/index.html") if err != nil { log.Fatal(err) } fmt.Printf("%s", node.V)
Output: <html></html>
func (*MemFS) Get ¶
Get the node representation of file in memory. If path is not exist it will return os.ErrNotExist.
func (*MemFS) GoGenerate ¶
GoGenerate write the tree nodes as Go generated source file. If pkgName is not defined it will be default to "main". If out is not defined it will be default "memfs_generate.go" and saved in current directory.
func (*MemFS) IsMounted ¶ added in v0.6.0
IsMounted will return true if a directory in file system has been mounted to memory; otherwise it will return false.
func (*MemFS) Mount ¶
Mount the directory recursively into the memory as root directory. For example, if we mount directory "/tmp" and "/tmp" contains file "a", to access file "a" we call Get("/a"), not Get("/tmp/a").
Mount does not have any effect if current directory contains ".go" generated file from GoGenerate().
func (*MemFS) RemoveChild ¶ added in v0.6.0
RemoveChild remove a child on parent, including its map on PathNode. If child is not part if node's childrens it will return nil.
func (*MemFS) Unmount ¶ added in v0.6.0
func (mfs *MemFS) Unmount()
Unmount the root directory from memory.
type Node ¶
type Node struct { SysPath string // The original file path in system. Path string // Absolute file path in memory. Name string // File name. ContentType string // File type per MIME, e.g. "application/json". ModTime time.Time // ModTime contains file modification time. Mode os.FileMode // File mode. Size int64 // Size of file. V []byte // Content of file. Parent *Node // Pointer to parent directory. Childs []*Node // List of files in directory. }
Node represent a single file.
type PathNode ¶ added in v0.6.0
type PathNode struct {
// contains filtered or unexported fields
}
PathNode contains a mapping between path and Node.
func NewPathNode ¶ added in v0.6.0
func NewPathNode() *PathNode
NewPathNode create and initialize new PathNode.