walker

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: MIT Imports: 4 Imported by: 0

README

Walker

Package walker is a wrapper around fs.WalkDir using custom fs.WalkDirFunc to pass results of recursive directory walk to caller through a channel.

It makes easier to use the result concurrenly.

The package is quite small and presumably does not need extensive documentation. Just browse it on https://pkg.go.dev/github.com/dmfed/walker to see what is has to offer.

Also take a look at the examples in examples dir of the repository for an overview.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DiscardDirs - removes directories from output
	DiscardDirs = func(info Info) bool {
		return !info.DirEntry.IsDir()
	}

	// DiscardRegular removes regular files from output
	DiscardRegular = func(info Info) bool {
		return !info.DirEntry.Type().IsRegular()
	}
)

Functions

This section is empty.

Types

type FilterFunc

type FilterFunc func(Info) bool

FilterFunc is intended to use with Filter If FilterFunc returns true then the Info is accepted as valid and forwarded to the output channel of Filter. If FilterFunc returns false then the Info is silently discarded.

type Info

type Info struct {
	Path     string
	DirEntry fs.DirEntry
}

Info represents and entry located by fs.WalkDir Path is always relative to base path supplied to Walker for example if Walk is called with path == "/tmp" the returned entries will not contain "/tmp" in Path field.

type Walker

type Walker interface {
	// Walk runs fs.WalkDir under the hood and returns chan of Info.
	// All items found by fs.WalkDir are returned as is. Basically
	// Walk is a custom fs.WalkDirFunc and a wrapper around WalkDir intended
	// to send result to a channel. It also computes lazily waiting for reader
	// to pull results from the channel as opposed to fs.WalkDir (fire and forget,
	// then wait for all recursive calls to finish).
	Walk(ctx context.Context, path string, funcs ...FilterFunc) <-chan Info

	// Err explains why chan returned by Walk was closed. If context was
	// cancelled or context deadline exceeded or whatewer error the
	// WalkDirFunc function encountered it is returned by this method.
	// Err will return nil if chan Info has not yet been closed.
	Err() error
}

Walker crawls the specified path using fs.WalkDir and sends all encountered items as Info to the channel returned by Walk method.

func New

func New() Walker

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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