finder

package module
v0.0.0-...-837dba9 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2016 License: MIT Imports: 9 Imported by: 0

README

github.com/nochso/finder

Released under MIT license GitHub release SemVer 2.0.0 Build Status Coverage Status Go Report Card

A fluent interface around Go's path/filepath.Walk.

Installation

go get github.com/nochso/finder

Usage

See documentation on godoc.

Changes

See the CHANGELOG for a full history of versions and their changes.

Versioning

This package adheres to semantic versioning 2.0.0.

TL;DR If you use version 1.* you should never have problems using future 1.* versions. Only a major release e.g. 2.0.0 will break backwards compatibility.

License

This package is released under the MIT license. See LICENSE for the full license.

Documentation

Overview

Package finder provides a fluent interface around Go's path/filepath.Walk

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByExtension

func ByExtension(i, j Item) bool

ByExtension sorts by file extension in ascending order.

func ByModified

func ByModified(i, j Item) bool

ByModified sorts by modification time in ascending order.

func ByName

func ByName(i, j Item) bool

ByName sorts by the base name in ascending order.

func ByPath

func ByPath(i, j Item) bool

ByPath sorts by path in ascending order.

func BySize

func BySize(i, j Item) bool

BySize sorts by file size in ascending order.

Types

type Finder

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

Finder contains options for a file/directory search.

func New

func New() *Finder

New returns a new finder.

By default it will search for both files and directories.

Any errors that occur during setup or walking are collected in `Finder.Error()`.

func (*Finder) Chan

func (f *Finder) Chan() <-chan Item

Chan returns a channel receiving all found items.

func (*Finder) Depth

func (f *Finder) Depth(min, max int) *Finder

Depth filters based on the relative depth of the directory. Parameters min and max are one-based. Max is ignored if it's lower than min.

Depth(1, 1)  // root level only
Depth(2, -1) // anything deeper than root

func (*Finder) Dirs

func (f *Finder) Dirs() *Finder

Dirs makes the finder return directories only.

func (*Finder) Each

func (f *Finder) Each(fn func(Item))

Each calls func fn with each found item. Any errors that occur while walking will be added to `Finder.Error()`.

func (*Finder) Error

func (f *Finder) Error() MultiErr

func (*Finder) Files

func (f *Finder) Files() *Finder

Files makes the finder return files only.

func (*Finder) Filter

func (f *Finder) Filter(m Matcher) *Finder

Filter items using a custom Matcher. Signature of a matcher:

func(Item) bool

func (*Finder) IgnoreDots

func (f *Finder) IgnoreDots() *Finder

IgnoreDots ignores directories with a leading dot.

func (*Finder) IgnoreVCS

func (f *Finder) IgnoreVCS() *Finder

IgnoreVCS ignores directories used by common version control systems.

func (*Finder) In

func (f *Finder) In(directories ...string) *Finder

In searches in the given list of directories.

func (*Finder) Name

func (f *Finder) Name(n string) *Finder

Name matches a file or directory name using gobwas/glob

See https://github.com/gobwas/glob

func (*Finder) NameRegex

func (f *Finder) NameRegex(n string) *Finder

NameRegex matches a file or directory name using package regexp.

func (*Finder) NotName

func (f *Finder) NotName(n string) *Finder

NotName excludes a file or directory name using gobwas/glob

See https://github.com/gobwas/glob

func (*Finder) NotNameRegex

func (f *Finder) NotNameRegex(n string) *Finder

NotNameRegex excludes a file or directory name using package regexp.

func (*Finder) NotPath

func (f *Finder) NotPath(p string) *Finder

NotPath excludes folders from the search using gobwas/glob

p is matched against the item's RelPath() See https://github.com/gobwas/glob

func (*Finder) Path

func (f *Finder) Path(p string) *Finder

Path narrows down the folders to be searched using gobwas/glob

p is matched against the item's RelPath() See https://github.com/gobwas/glob

func (*Finder) Size

func (f *Finder) Size(min, max int64) *Finder

Size filters by minimum and maximum file size. Max is ignored if it's lower than min.

Size(0, 1024)    // <=1kB
Size(1024, 1024) // ==1kB
Size(1024, -1)   // >=1kB

func (*Finder) ToSlice

func (f *Finder) ToSlice() ItemSlice

ToSlice returns a slice of all found items. Any errors that occur while walking will be added to `Finder.Error()`.

type Item

type Item struct {
	os.FileInfo
	// contains filtered or unexported fields
}

Item represents a single file or directory.

func (Item) Depth

func (i Item) Depth() int

Depth returns the depth of the item based on RelPath. It starts at 1 (one).

func (Item) Path

func (i Item) Path() string

Path returns the path including the folder it was found in.

func (Item) RelPath

func (i Item) RelPath() string

RelPath returns the path relative to the searched directory.

func (Item) String

func (i Item) String() string

String returns the path

type ItemSlice

type ItemSlice []Item

ItemSlice is a list of items.

func (ItemSlice) Size

func (is ItemSlice) Size() uint64

Size returns the sum of all file sizes.

func (ItemSlice) Sort

func (is ItemSlice) Sort(lessFn func(i, j Item) bool)

Sort the slice given a function that mimics `Less` of `sort.Interface`.

func (ItemSlice) ToStringSlice

func (is ItemSlice) ToStringSlice() []string

ToStringSlice returns a string slice with each item's path.

type Matcher

type Matcher func(Item) bool

Matcher checks whether an Item matches.

type MultiErr

type MultiErr []error

MultiErr combines multiple errors.

func (MultiErr) Error

func (e MultiErr) Error() string

Jump to

Keyboard shortcuts

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