aferox

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2025 License: MIT Imports: 13 Imported by: 1

README

aferox

The aferox packages expands on github.com/spf13/afero by adding more afero.Fs implementations, as well as various afero.Fs utility functions.

Go Doc

github

The github package adds multiple implementations of afero.Fs for interacting with the GitHub API as if it were a filesystem. In general it can turn a GitHub url into an afero.Fs.

fs := github.NewFs(github.NewClient(nil))

file, _ := fs.Open("https://github.com/unmango")

// ["go", "thecluster", "pulumi-baremetal", ...]
file.Readdirnames(420)

This package lives in a separate module to avoid adding a dependendy on GitHub to aferox.

Go Doc

go get github.com/unmango/aferox/github

ignore

The ignore package adds a filtering afero.Fs that accepts a .gitignore file and ignores paths matched by it.

base := afero.NewMemMapFs()

gitignore, _ := os.Open("path/to/my/.gitignore")

fs, _ := ignore.NewFsFromGitIgnoreReader(base, gitignore)

filter

The filter package adds a filtering implementation of afero.Fs similar to afero.RegExpFs, but for predicates.

base := afero.NewMemMapFs()

fs := filter.NewFs(base, func(path string) bool {
	return filepath.Ext(path) == ".go"
})

docker

The docker package adds a docker afero.Fs implementation for operating on the filesystem of a container.

client := client.NewClientWithOpts(client.FromEnv)

fs := docker.NewFs(client, "my-container-id")

This package lives in a separate module to avoid adding a dependency on docker to aferox.

Go Doc

go get github.com/unmango/aferox/docker

testing

The testing package adds helper stubs for mocking filesystems in tests.

fs := &testing.Fs{
	CreateFunc: func(name string) (afero.File, error) {
		return nil, errors.New("simulated error")
	}
}

writer

The writer package adds a readonly afero.Fs implementation that dumps all file writes to the provided io.Writer. Currently paths are ignored and there are no delimeters separating files.

buf := &bytes.Buffer{}
fs := writer.NewFs(buf)

_ = afero.WriteFile(fs, "test.txt", []byte("testing"), os.ModePerm)
_ = afero.WriteFile(fs, "other.txt", []byte("blah"), os.ModePerm)

// "testingblah"
buf.String()

context

The context package adds the context.Fs interface for filesystem implementations that accept a context.Context per operation. It has a basic test suite and generally works but should be considered a 🚧 work in progress 🚧.

The context package re-exports various functions and types from the standard context packge for convenience. Currently the creation functions focus on adapting external context.Fs implementations to an afero.Fs to be used with the other utility functions.

var base context.Fs = mypkg.NewEffectfulFs()

fs := context.BackgroundFs(base)

var accessor context.AccessorFunc = func() context.Context {
	return context.Background()
}

// Equivalent to `context.BackgroundFs`
fs := context.NewFs(base, accessor)

The context.AferoFs interface is a union of afero.Fs and context.Fs, i.e. exposing both fs.Create and fs.CreateContext. I'm not sure if this actually has any value but it exists.

The context.Discard function adapts an afero.Fs to a context.AferoFs by ignoring the context.Context argument.

base := afero.NewMemMapFs()

var fs context.AferoFs = context.Discard(base)

protofs

The protofs module contains a silly little idea I had for communicating filesystem information over gRPC. The protobuf definitions are over at https://github.com/unmango/protofs/. It... works? It is not well tested and probably performs poorly. Use at your own risk.

Go Doc

Server
// Serve any afero.Fs implementation
var fs afero.Fs

server := grpc.NewServer()
protofsv1alpha1.RegisterFsServer(server, fs)
protofsv1alpha1.RegisterFileServer(server, fs)

lis, _ := net.Listen("unix", "/tmp/fs.sock")
server.Serve(lis)
Client
conn, _ := grpc.NewClient("unix:///tmp/fs.sock",
  grpc.WithTransportCredentials(insecure.NewCredentials()),
)

var fs afero.Fs = protofsv1alpha1.NewFs(conn)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContinueOnError

func ContinueOnError(options *iterOptions)

func Copy

func Copy(src, dest afero.Fs) error

func Fold

func Fold[T any](fsys afero.Fs, root string, folder FoldFunc[T], initial T) (acc T, err error)

func FromContext

func FromContext(ctx context.Context) afero.Fs

func Iter

func Iter(fsys afero.Fs, root string, options ...IterOption) iter.Seq3[string, fs.FileInfo, error]

func NewWriter

func NewWriter(w io.Writer) afero.Fs

func OpenFirst

func OpenFirst(fsys afero.Fs, root string, options ...IterOption) (afero.File, error)

func OpenSingle

func OpenSingle(fsys afero.Fs, root string, options ...IterOption) (afero.File, error)

func SetContext

func SetContext(fs afero.Fs, ctx context.Context) error

func SkipDirs

func SkipDirs(options *iterOptions)

func StatFirst

func StatFirst(fsys afero.Fs, root string, options ...IterOption) (fs.FileInfo, error)

func StatSingle

func StatSingle(fsys afero.Fs, root string, options ...IterOption) (fs.FileInfo, error)

Types

type ErrFilter

type ErrFilter func(error) error

type FoldFunc

type FoldFunc[T any] func(string, fs.FileInfo, T, error) (T, error)

type IterOption

type IterOption func(*iterOptions)

func FilterErrors

func FilterErrors(filter ErrFilter) IterOption

type ReadOnlyFile added in v0.2.1

type ReadOnlyFile struct{}

ReadOnlyFile can be embedded in structs to assist with defining read-only implementations of afero.File

func (*ReadOnlyFile) Readdir added in v0.2.2

func (s *ReadOnlyFile) Readdir(count int) ([]os.FileInfo, error)

Readdir implements afero.File.

func (*ReadOnlyFile) Readdirnames added in v0.2.2

func (s *ReadOnlyFile) Readdirnames(n int) ([]string, error)

Readdirnames implements afero.File.

func (*ReadOnlyFile) Seek added in v0.2.1

func (r *ReadOnlyFile) Seek(offset int64, whence int) (int64, error)

Seek implements afero.File.

func (*ReadOnlyFile) Sync added in v0.2.1

func (r *ReadOnlyFile) Sync() error

Sync implements afero.File.

func (*ReadOnlyFile) Truncate added in v0.2.1

func (r *ReadOnlyFile) Truncate(size int64) error

Truncate implements afero.File.

func (*ReadOnlyFile) Write added in v0.2.1

func (r *ReadOnlyFile) Write(p []byte) (n int, err error)

Write implements afero.File.

func (*ReadOnlyFile) WriteAt added in v0.2.1

func (r *ReadOnlyFile) WriteAt(p []byte, off int64) (n int, err error)

WriteAt implements afero.File.

func (*ReadOnlyFile) WriteString added in v0.2.1

func (r *ReadOnlyFile) WriteString(s string) (ret int, err error)

WriteString implements afero.File.

type ReadOnlyFs added in v0.2.3

type ReadOnlyFs struct{}

ReadOnlyFs can be embedded in structs to assist with defining read-only implementations of afero.Fs

func (*ReadOnlyFs) Chmod added in v0.2.3

func (r *ReadOnlyFs) Chmod(name string, mode os.FileMode) error

Chmod implements afero.Fs.

func (*ReadOnlyFs) Chown added in v0.2.3

func (r *ReadOnlyFs) Chown(name string, uid int, gid int) error

Chown implements afero.Fs.

func (*ReadOnlyFs) Chtimes added in v0.2.3

func (r *ReadOnlyFs) Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes implements afero.Fs.

func (*ReadOnlyFs) Create added in v0.2.3

func (r *ReadOnlyFs) Create(name string) (afero.File, error)

Create implements afero.Fs.

func (*ReadOnlyFs) Mkdir added in v0.2.3

func (r *ReadOnlyFs) Mkdir(name string, perm os.FileMode) error

Mkdir implements afero.Fs.

func (*ReadOnlyFs) MkdirAll added in v0.2.3

func (r *ReadOnlyFs) MkdirAll(path string, perm os.FileMode) error

MkdirAll implements afero.Fs.

func (*ReadOnlyFs) Remove added in v0.2.3

func (r *ReadOnlyFs) Remove(name string) error

Remove implements afero.Fs.

func (*ReadOnlyFs) RemoveAll added in v0.2.3

func (r *ReadOnlyFs) RemoveAll(path string) error

RemoveAll implements afero.Fs.

func (*ReadOnlyFs) Rename added in v0.2.3

func (r *ReadOnlyFs) Rename(oldname string, newname string) error

Rename implements afero.Fs.

type Writer

type Writer = writer.Fs

Directories

Path Synopsis
docker module
github module
protofs module
gfs

Jump to

Keyboard shortcuts

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