lfile

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package lfile provides utilities around file interactions beyond those in the standard library.

For now, the Search functionality of Match is more of placeholder.

Package lfiles provides utility functions around file and directory operations.

Index

Constants

View Source
const (
	FilterHidden = `\/\.[^\/]*$`
)

Variables

Glob is a reference to filepath.Glob. It is left exposed for testing.

View Source
var ReadFile = os.ReadFile

TODO: move this into OSRepository ReadFile is a reference to os.ReadFile. It is left exposed for testing.

View Source
var Stat = os.Stat

Stat is a reference to os.Stat. It is left exposed for testing.

Functions

func CountAll

func CountAll(s string, words []*regexp.Regexp) (foundAll bool, matches [][][]int, counts []int, sum int)

CountAll searches the string for the given words. If all words are not present, all return values will be their zero value. If all words are found, bool will be true, the second value indicates the locations of every word, the third return is the count of each word and the fourth return is the total number of matches.

func Exts

func Exts(hiddenDirs bool, exts ...string) string

Exts builds a regular expression for file extensions. If hiddenDirs is false files that have a parent directory that is hidden (name begins with .) those will be ommited.

func Name

func Name(path string) (string, string)

Name returns the last portion of a path as it's name.

  • "/foo/bar.txt" => "/foo/","bar.txt"
  • "/foo/bar/" => "/foo/","bar"
  • "foo.txt" => "", "foo.txt"
  • foo/ => "", "foo"

The second returned value is the name and the first is the preceeding portion.

func RegexWords

func RegexWords(words []string) []*regexp.Regexp

RegexWords creates a slice of case insensitive Regexp for each word.

func RunHandler

func RunHandler(i Iterator, ih IterHandler) error

RunHandler will create an Iter from Iterator and call the HandleIter method on the IterHandler for each value in the iterator.

func RunHandlerSource

func RunHandlerSource(ii IteratorSource, ih IterHandler) error

RunHandler will create an Iter from Iterator and call the HandleIter method on the IterHandler for each value in the iterator.

Types

type Dir

type Dir interface {
	ReadDir(n int) ([]os.DirEntry, error)
	Name() string
}

Dir is fulfilled by *os.File.

type DirContents

type DirContents struct {
	Name, Path string
	SubDirs    []string
	Files      []string
}

DirContents is returned from GetDirContents.

func GetDirContents

func GetDirContents(f Dir) (*DirContents, error)

GetDirContents splits the full path of the Dir into it's name and partial path and returns the sub-dirs and files in sorted slices.

type File

type File interface {
	Dir
	io.Reader
	io.WriteCloser
	Stat() (os.FileInfo, error)
}

File provides an interface fulfilled by *os.File. This allows for testing without relying on the actual file system.

type GetByTypeHandler

type GetByTypeHandler struct {
	Files, Dirs []string
}

GetByTypeHandler records all the files and directories the Iterator visits and seperates them by type.

func (*GetByTypeHandler) HandleIter

func (bt *GetByTypeHandler) HandleIter(i Iterator)

HandleIter fulfills IterHandler and records the current location based on the type.

type GetContentsHandler

type GetContentsHandler map[string][]byte

GetContentsHandler reads the contents of all files into a map.

func (GetContentsHandler) HandleIter

func (c GetContentsHandler) HandleIter(i Iterator)

HandleIter fulfills IterHandler. If the current value of the Iterator is a file, it's contents are entered into the GetContentsHandler map.

type IterHandler

type IterHandler interface {
	HandleIter(Iterator)
}

IterHandler represents something that will handle each value in the iterator.

type Iterator

type Iterator interface {
	liter.Iter[string]
	Path() string
	Data() []byte
	Err() error
	Stat() os.FileInfo
	Reset() (done bool)
}

Iterator over a set of files and directories.

type IteratorSource

type IteratorSource interface {
	Iterator() (i Iterator, done bool)
}

IteratorSource can generate an Iterator.

type Match

type Match struct {
	SkipDir filter.Filter[string]
	Find    struct {
		File, Dir filter.Filter[string]
	}
}

Match will walk a directory and match files and subdirectories. SkipDir can be used to skip directories and their contents. The distinction between skipped directories and directories that not found is that when a directory is skipped, all of it's contents are skipped, when a directory is not found, it's contents are visited, but the directory itself will not be value returned by the iterator.

func MustRegexMatch

func MustRegexMatch(findFile, findDir, skipDir string) Match

MustRegexMatch makes an instance of Match using regex for all the values.

func NewMatch

func NewMatch(findFile, findDir, skipDir filter.Filter[string]) Match

NewMatch makes a new instance of Match

func (Match) Root

func (m Match) Root(root string) MatchRoot

Root to Match against, return MatchRoot which fulfills IteratorSource.

type MatchRoot

type MatchRoot struct {
	Match
	Root string
}

MatchRoot combines a Match instance with a Root. It fulfills IteratorSource.

func (MatchRoot) Iterator

func (mr MatchRoot) Iterator() (i Iterator, done bool)

Iterator fulfills IteratorSource returning an Iterator to iterate over all the matches starting from the root.

type MultiGlob

type MultiGlob []string

MultiGlob performs multiple filepath.Glob operation and merges the values into a single slice with no duplicates.

func (MultiGlob) Iterator

func (mg MultiGlob) Iterator() (Iterator, bool)

Iter will iterate over the files found by the MultiGlob.

func (MultiGlob) Paths

func (mg MultiGlob) Paths() ([]string, error)

Paths found using MultiGlob.

func (MultiGlob) Recursive

func (mg MultiGlob) Recursive(base, pattern string) MultiGlob

Recursive adds two glob patterns, one to the base and one recursive.

type MultiHandler

type MultiHandler []IterHandler

MultiHandler is a slice of IterHandler. HandleIter will call HandleIter on each IterHandler in the slice

func (MultiHandler) HandleIter

func (mh MultiHandler) HandleIter(i Iterator)

HandleIter will call HandleIter on each IterHandler in the slice

type OSRepository

type OSRepository struct{}

OSRepository fulfills Repository by using functions from the "os" package.

func (OSRepository) Create

func (OSRepository) Create(name string) (File, error)

Create is a wrapper for os.Create

func (OSRepository) Open

func (OSRepository) Open(name string) (File, error)

Open is a wrapper for os.Open

func (OSRepository) Remove

func (OSRepository) Remove(name string) error

Remove is a wrapper for os.Remove

type PathLength

type PathLength int

PathLength is used to trim filenames to a set number of parts.

func (PathLength) Trim

func (pln PathLength) Trim(filename string) string

Trim the filname so it will have at most PathLength number of parts, including the filename. The returned value will never begin with filepath.Separator.

type Paths

type Paths []string

Paths to be iterated over.

func (Paths) Iterator

func (fn Paths) Iterator() (Iterator, bool)

Iter fulfills Iterator, iterating over the files in Filenames.

type RegexSearch

type RegexSearch struct {
	Terms   []*regexp.Regexp
	Results []SearchResult
}

func (*RegexSearch) HandleIter

func (rs *RegexSearch) HandleIter(i Iterator)

type Repository

type Repository interface {
	Open(name string) (File, error)
	Create(name string) (File, error)
	Remove(name string) error
}

Repository is an interface to the file system.

type SearchResult

type SearchResult struct {
	// File where the matches were found
	File string
	// Matches locations within file
	// TODO: this currently encapsulates the FindAllStringSubmatchIndex
	// when I probably just want to find the whole regex. For more generic
	// searches, I'll just want the [term][start/end]; or I can squash them down
	// so they're all the start/end in order
	Matches [][][]int
	// Counts for each search term
	Counts []int
	// Total number of counts
	Sum int
}

Directories

Path Synopsis
Package lfilemock provides some mock file objects.
Package lfilemock provides some mock file objects.

Jump to

Keyboard shortcuts

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