Documentation
¶
Overview ¶
Package fsift provides filter-driven fs.WalkDir helpers: composable FileFilters, single-pass file discovery, and a filtered fs.FS decorator that applies those filters transparently to any consumer of the wrapped FS.
Index ¶
- func Filtered(base fs.FS, filters ...FileFilter) fs.FS
- func FindFirst(ctx context.Context, dirFS fs.FS, predicates ...FileFilter) (map[int]string, error)
- func ListFiles(ctx context.Context, dirFS fs.FS, filters ...FileFilter) ([]string, error)
- func Walk(ctx context.Context, dirFS fs.FS, filters ...FileFilter) iter.Seq[string]
- type FileFilter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filtered ¶
func Filtered(base fs.FS, filters ...FileFilter) fs.FS
Filtered returns base wrapped so ReadDir omits any entry a filter skips, applying the same prune-on-descend semantics as ListFiles: skipping a directory hides its whole subtree. Filters receive each entry's path relative to the FS root. Open is left untouched, so a caller holding an exact path can still read a file inside a pruned directory.
func FindFirst ¶
FindFirst walks dirFS applying filters and, for each file yielded, tests it against each predicate. When a predicate matches its first file, it is removed from the active set and its match is recorded. The walk terminates as soon as every predicate has been satisfied. Returns a map from predicate index to the first matching path; predicates with no match are absent. ctx cancellation is honoured — the walk stops and the matches collected so far are returned alongside the context error, so a cancelled search is never mistaken for a completed one.
Typical use: detect workspace marker files (go.mod, package.json, Cargo.toml) in a single walk — one match per marker type stops further searching for that type, and the walk ends when all types are found.
func ListFiles ¶
ListFiles walks dirFS and returns the paths of all regular files that pass every filter. Filters are applied with the prune-on-descend semantics described on FileFilter: a filter returning true for a directory skips the entire subtree. ctx cancellation is honoured — the walk stops and the collected results so far are returned alongside the context error.
func Walk ¶
Walk returns an iterator over file paths in dirFS, applying filters with the same semantics as ListFiles: a filter returning true on a directory prunes the subtree (fs.SkipDir); a filter returning true on a file skips that file. The iterator honours ctx — if the context is cancelled the walk stops and the cancellation error is swallowed (the caller controls the loop via break).
Types ¶
type FileFilter ¶
FileFilter is a predicate over a filesystem entry. Returning true on a directory prunes the subtree (equivalent to fs.SkipDir); returning true on a regular file skips that file.
func MaxDepth ¶
func MaxDepth(maxDepth int) FileFilter
MaxDepth returns a FileFilter that prunes directories deeper than n levels from the walk root. Depth is counted as the number of path separators in the directory's path relative to the root (the root itself is depth 0). Files at depth n are still visited; only descending past depth n is blocked.
func SkipGlobs ¶
func SkipGlobs(patterns ...string) FileFilter
SkipGlobs returns a FileFilter that skips any entry whose base name matches one of the shell patterns (path.Match syntax: *, ?, [set]). It applies to files and directories alike, so SkipGlobs("node_modules", "*.gen.go") prunes the node_modules subtree and skips generated files. A malformed pattern never matches.
func SkipHiddenDirs ¶
func SkipHiddenDirs() FileFilter
SkipHiddenDirs returns a FileFilter that prunes hidden directories (names starting with '.').
func SkipHiddenFiles ¶
func SkipHiddenFiles() FileFilter
SkipHiddenFiles returns a FileFilter that skips hidden files (names starting with '.').
func SkipVendorDirs ¶
func SkipVendorDirs(extra ...string) FileFilter
SkipVendorDirs returns a FileFilter that prunes common vendor/build output directories: node_modules, target, vendor, dist, .venv, plus any additional names supplied by the caller.