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
- Variables
- func CountAll(s string, words []*regexp.Regexp) (foundAll bool, matches [][][]int, counts []int, sum int)
- func Exts(hiddenDirs bool, exts ...string) string
- func Name(path string) (string, string)
- func RegexWords(words []string) []*regexp.Regexp
- func RunHandler(i Iterator, ih IterHandler) error
- func RunHandlerSource(ii IteratorSource, ih IterHandler) error
- type Dir
- type DirContents
- type File
- type GetByTypeHandler
- type GetContentsHandler
- type IterHandler
- type Iterator
- type IteratorSource
- type Match
- type MatchRoot
- type MultiGlob
- type MultiHandler
- type OSRepository
- type PathLength
- type Paths
- type RegexSearch
- type Repository
- type SearchResult
Constants ¶
const (
FilterHidden = `\/\.[^\/]*$`
)
Variables ¶
var Glob = filepath.Glob
Glob is a reference to filepath.Glob. It is left exposed for testing.
var ReadFile = os.ReadFile
TODO: move this into OSRepository ReadFile is a reference to os.ReadFile. It is left exposed for testing.
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 ¶
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 ¶
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 ¶
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 DirContents ¶
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 ¶
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 ¶
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 ¶
IteratorSource can generate an Iterator.
type Match ¶
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 ¶
MustRegexMatch makes an instance of Match using regex for all the values.
type MultiGlob ¶
type MultiGlob []string
MultiGlob performs multiple filepath.Glob operation and merges the values into a single slice with no duplicates.
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 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 }