walker

package module
v0.25.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: MIT Imports: 7 Imported by: 0

README

Walker GoDoc Go Report Card Coverage Status

Walker: File Ranger contains convenient iterators for walking a directory or fs.FS.

Documentation

Overview

Package Walker: File Ranger contains convenient iterators for walking a directory or fs.FS.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Path     string
	DirEntry fs.DirEntry
	// contains filtered or unexported fields
}

Entry is a single path/fs.DirEntry pair yielded by a Ranger. It knows whether to use package filepath or package path for its methods.

func (Entry) Base

func (e Entry) Base() string

Base returns the last element of Path, typically the filename. See path.Base and filepath.Base.

func (Entry) Dir

func (e Entry) Dir() string

Dir returns the directory of the Entry. Unlike path.Dir or filepath.Dir, it knows whether e represents a directory, in which case it returns its own path, not its parent directory.

func (Entry) Ext

func (e Entry) Ext() string

Ext returns the file name extension of Path. See path.Ext and filepath.Ext.

func (Entry) IsDir

func (e Entry) IsDir() bool

IsDir returns whether the DirEntry is a directory. If DirEntry is nil, it returns false.

func (Entry) Name

func (e Entry) Name() string

Name returns DirEntry.Name(). If DirEntry is nil, it returns "".

func (Entry) Split

func (e Entry) Split() (string, string)

Split splits path immediately following the final separator, separating it into a directory and file name component. See path.Split and filepath.Split.

type ErrorPolicy

type ErrorPolicy func(err error, e Entry) bool

ErrorPolicy is a function that returns whether to continue (true) or halt (false) on error by examining the error and the current Entry.

var OnErrPermissionIgnore ErrorPolicy = func(err error, e Entry) bool {
	return errors.Is(err, fs.ErrPermission)
}

OnErrPermissionIgnore is an ErrorPolicy that continues if an error is fs.ErrPermission; otherwise it halts on error.

var OnErrorHalt ErrorPolicy = onErrorHalt

OnErrorHalt is an ErrorPolicy that halts on any error.

var OnErrorIgnore ErrorPolicy = onErrorIgnore

OnErrorIgnore is an ErrorPolicy that always continues regardless of errors.

var OnErrorPanic ErrorPolicy = onErrorPanic

OnErrorPanic is an ErrorPolicy that panics on any error. Not recommended for production.

func OnErrorCollect

func OnErrorCollect(errs *[]error) ErrorPolicy

OnErrorCollect returns an ErrorPolicy that collects errors into the provided slice while continuing.

type FilterFunc

type FilterFunc func(Entry) bool

FilterFunc is a function type used to filter files and directories during the walk.

var MatchDotFile FilterFunc = MatchPrefixName(".")

MatchDotFile reports whether an Entry.Name() begins with a dot.

func And

func And(filters ...FilterFunc) FilterFunc

And chains FilterFuncs and returns whether they are all true.

func MatchExtension

func MatchExtension(extensions ...string) FilterFunc

MatchExtension creates a FilterFunc that filters files based on their extensions. It returns true if the file has any of the specified extensions. It is case insensitive.

func MatchGlobName

func MatchGlobName(patterns ...string) FilterFunc

MatchGlobName returns true if Entry.Name() matches any of the glob patterns.

func MatchGlobPath

func MatchGlobPath(patterns ...string) FilterFunc

MatchGlobPath returns true if the path matches any of the glob patterns.

func MatchPrefixName

func MatchPrefixName(prefix string) FilterFunc

MatchPrefixName creates a FilterFunc that matches if Entry.Name() starts with the given prefix.

func MatchPrefixPath

func MatchPrefixPath(prefix string) FilterFunc

MatchPrefixPath creates a FilterFunc that matches paths starting with the given prefix.

func MatchRegexp

func MatchRegexp(re *regexp.Regexp) FilterFunc

MatchRegexp returns true if the path matches the regular expression.

func MatchRegexpMust

func MatchRegexpMust(re string) FilterFunc

MatchRegexpMust compiles re using regexp.MustCompile and passes it to MatchRegexp.

func Not

func Not(f FilterFunc) FilterFunc

Not inverts a FilterFunc.

func Or

func Or(filters ...FilterFunc) FilterFunc

Or chains FilterFuncs and returns whether at least one is true.

type Ranger

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

Ranger provides a convenient way to walk through a directory structure.

Example
package main

import (
	"fmt"
	"testing/fstest"

	"github.com/earthboundkid/walker"
)

func main() {
	// Demo filesystem
	fsys := fstest.MapFS{
		".a-stuff/file-1.jpeg": &fstest.MapFile{},
		"b-file-2.txt":         &fstest.MapFile{},
		"c/file-3.png":         &fstest.MapFile{},
		"d/file-4.jpeg":        &fstest.MapFile{},
		"e-file-5.txt":         &fstest.MapFile{},
	}

	// Make a new Ranger that halts on error
	tr := walker.New(fsys, ".", walker.OnErrorHalt)

	fmt.Println("Files:")
	for path := range tr.FilePaths() {
		fmt.Println("-", path)
	}
	// Do a final error check
	if tr.HasError() {
		panic(tr.Err())
	}
}
Output:

Files:
- .a-stuff/file-1.jpeg
- b-file-2.txt
- c/file-3.png
- d/file-4.jpeg
- e-file-5.txt
Example (Matching)
package main

import (
	"fmt"
	"testing/fstest"

	"github.com/earthboundkid/walker"
)

func main() {
	// Demo filesystem
	fsys := fstest.MapFS{
		".a-stuff/file-1.jpeg": &fstest.MapFile{},
		"b-file-2.txt":         &fstest.MapFile{},
		"c/file-3.png":         &fstest.MapFile{},
		"d/file-4.jpeg":        &fstest.MapFile{},
		"e-file-5.txt":         &fstest.MapFile{},
	}

	// Make a new Ranger that ignores permission errors and halts for other problems
	tr := walker.New(fsys, ".", walker.OnErrPermissionIgnore)
	// Ignore dot files and dot directories
	tr.Exclude(walker.MatchDotFile)
	tr.ExcludeDir(walker.MatchDotFile)
	// Only list PNG and JPEG files
	tr.Include(walker.Or(
		walker.MatchExtension(".png"),
		walker.MatchExtension(".jpeg"),
	))

	fmt.Println("Files:")
	for path := range tr.FilePaths() {
		fmt.Println("-", path)
	}
	// Do a final error check
	if tr.HasError() {
		panic(tr.Err())
	}
}
Output:

Files:
- c/file-3.png
- d/file-4.jpeg

func New

func New(fsys fs.FS, root string, erp ErrorPolicy) Ranger

New creates a new *Ranger with the given root directory. Pass a nil fsys to use filepath.WalkFunc and walk the OS filesystem instead of an fs.FS. The default Ranger includes all files and directories. There is no default ErrorPolicy.

func (*Ranger) Entries

func (tr *Ranger) Entries() iter.Seq[Entry]

Entries returns a sequence of Entries for matching files and directories.

func (*Ranger) Err

func (tr *Ranger) Err() error

Err returns the last error encountered during walking, if any.

func (*Ranger) Exclude

func (tr *Ranger) Exclude(f FilterFunc)

Exclude tells the Ranger to exclude matching files when iterating. Files matched by Exclude take precedence over files matched by Include.

func (*Ranger) ExcludeDir

func (tr *Ranger) ExcludeDir(f FilterFunc)

ExcludeDir tells the Ranger not to recursing into matching directories. Directories matched by ExcludeDir take precedence over directories matched by IncludeDir.

func (*Ranger) FileEntries

func (tr *Ranger) FileEntries() iter.Seq[Entry]

FileEntries returns a sequence of Entries for matching files, ignoring directories.

func (*Ranger) FilePaths

func (tr *Ranger) FilePaths() iter.Seq[string]

FilePaths returns a sequence of file paths, ignoring directories.

func (*Ranger) HasError

func (tr *Ranger) HasError() bool

HasError returns true if an error has been encountered during the last walk.

func (*Ranger) Include

func (tr *Ranger) Include(f FilterFunc)

Include tells the Ranger to include matching files when iterating. The default is to include all files.

func (*Ranger) IncludeDir

func (tr *Ranger) IncludeDir(f FilterFunc)

IncludeDir tells the Ranger to recursing into matching directories. The default is to include all directories.

func (*Ranger) SkipDir

func (tr *Ranger) SkipDir()

SkipDir signals to a Ranger during iteration that the current directory should be skipped. It is an error to call SkipDir when not iterating.

Jump to

Keyboard shortcuts

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