realfs

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 8 Imported by: 2

README

go-real-fs

A real filesystem that behaves normally and implements fs.FS and wraps os.DirFS.

Using fs.FS to represent filesystems has many advantages, but it can be clunky to work with for reasons outlined in this rejected proposal.

go-real-fs implements this behaviour like so:

package main

import (
    "github.com/sean9999/go-real-fs"
)

func main(){

    realFs := realfs.New()
    data, _ := realFs.ReadFile("/etc/passwd")
    fmt.Println("here is the contents of /etc/passwd")
    os.Stdout.Write(data)

}

go-real-fs simply rewrites paths from context-dependant to root-relative:

func (rfs realFS) correctPath(relativePath string) (string, error) {

	fullPath, err := filepath.Abs(relativePath)
	if err != nil {
		return "", err
	}
	return filepath.Rel("/", fullPath)

}

It's power comes from the fact that it satisfies interfaces from the fs package, allowing for modularity and testability. Let's say you wanted to create a storage-agnostic and testable key-value store:


type Database struct {
    filesystem realfs.WritableFs
}

func (d *Database) Get(name string) ([]byte, error) {
    return d.filesystem.OpenFile(name)
}

func (d *Databse) Set(name string, content []byte) error {
    return d.filesystem.WriteFile(name, content, 0640)
}

func (d *Database) Delete(name string) error {
    return d.filesystem.Remove(name)
}

To instantiate a real Databse you can do:


db := Database{realfs.NewWritable()} 

And to test is equally easy:


func TestDatabase(t *testing.T) {

    


}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewWritable added in v0.0.3

func NewWritable() writableFs

Types

type NullDevice added in v0.1.0

type NullDevice struct {
	io.Writer
}

func (NullDevice) Open added in v0.1.0

func (b NullDevice) Open(_ string) (fs.File, error)

func (NullDevice) OpenFile added in v0.1.0

func (b NullDevice) OpenFile(name string, flag int, perm fs.FileMode) (WritableFile, error)

func (NullDevice) Read added in v0.1.0

func (b NullDevice) Read(_ []byte) (int, error)

func (NullDevice) ReadDir added in v0.1.0

func (b NullDevice) ReadDir(_ string) ([]fs.DirEntry, error)

func (NullDevice) ReadFile added in v0.1.0

func (b NullDevice) ReadFile(_ string) ([]byte, error)

func (NullDevice) Remove added in v0.1.0

func (b NullDevice) Remove(_ string) error

func (NullDevice) Stat added in v0.1.0

func (b NullDevice) Stat(_ string) (fs.FileInfo, error)

func (NullDevice) WriteFile added in v0.1.0

func (b NullDevice) WriteFile(_ string, _ []byte, _ fs.FileMode) error

type RealFS

type RealFS interface {
	fs.StatFS
	fs.ReadFileFS
	fs.ReadDirFS
}

RealFS is real filesystem that wraps os.DirFS and implements fs.FS

func New added in v0.0.2

func New() RealFS

type TestFS added in v0.1.0

type TestFS struct {
	// contains filtered or unexported fields
}

func NewTestFs added in v0.1.0

func NewTestFs() TestFS

func (TestFS) Open added in v0.1.0

func (tw TestFS) Open(name string) (fs.File, error)

func (TestFS) OpenFile added in v0.1.0

func (tw TestFS) OpenFile(name string, _ int, _ fs.FileMode) (WritableFile, error)

func (TestFS) ReadDir added in v0.1.0

func (tw TestFS) ReadDir(name string) ([]fs.DirEntry, error)

func (TestFS) ReadFile added in v0.1.0

func (tw TestFS) ReadFile(name string) ([]byte, error)

func (TestFS) Remove added in v0.1.0

func (tw TestFS) Remove(name string) error

func (TestFS) Stat added in v0.1.0

func (tw TestFS) Stat(name string) (fs.FileInfo, error)

func (TestFS) WriteFile added in v0.1.0

func (tw TestFS) WriteFile(name string, data []byte, mode fs.FileMode) error

type TestFSFile added in v0.1.0

type TestFSFile struct {
	*fstest.MapFile

	Info *finfo
	// contains filtered or unexported fields
}

func (*TestFSFile) Close added in v0.1.0

func (_ *TestFSFile) Close() error

func (*TestFSFile) IsDir added in v0.1.0

func (f *TestFSFile) IsDir() bool

func (*TestFSFile) Name added in v0.1.0

func (f *TestFSFile) Name() string

func (*TestFSFile) Read added in v0.1.0

func (f *TestFSFile) Read(p []byte) (int, error)

func (*TestFSFile) Seek added in v0.1.0

func (f *TestFSFile) Seek(offest int64, whence int) (int64, error)

func (*TestFSFile) Stat added in v0.1.0

func (f *TestFSFile) Stat() (fs.FileInfo, error)

func (*TestFSFile) Write added in v0.1.1

func (f *TestFSFile) Write(b []byte) (int, error)

type WritableFile added in v0.0.3

type WritableFile interface {
	fs.File
	io.Writer
	io.Seeker
	Name() string
}

type WritableFs added in v0.0.3

type WritableFs interface {
	RealFS
	WriteFile(string, []byte, fs.FileMode) error
	OpenFile(string, int, fs.FileMode) (WritableFile, error)
	Remove(string) error
}

WritableFs is a writable filesystem

Jump to

Keyboard shortcuts

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