path

package module
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 12 Imported by: 12

README

path

Build codecov Go Report Card Go Reference

Overview

A simple library to handle file path input in Go.

Features

  • Hanlde absolute and relative paths
  • Globbing (must be quoted)
  • List files in directories recursivly
  • Optional regex to filter results
  • Cli via flag, see example

Caveats

When passing in globbed patterns via cli you must quote them, if you dont bash will expand them and could result in undesired results. go run cmd/main.go -path "/my/globbed/path/*"

Example

package main

import (
	"flag"
	"fmt"

	"github.com/kmulvey/path"
)

func main() {
	var files path.Entry
	flag.Var(&files, "path", "path to files")
	flag.Parse()

	fmt.Println("user input: ", files.AbsolutePath)
	fmt.Println("number of files found within this path: ", len(files.Children))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxDepth uint8 = 255 // arbitrary, but hopefully enough

Functions

func Contains added in v0.8.0

func Contains(input []Entry, needle string) bool

func OnlyNames added in v0.6.0

func OnlyNames(input []Entry) []string

OnlyNames returns a slice of absolute paths (strings) from a given Entry slice.

func WatchDir added in v0.12.0

func WatchDir(ctx context.Context, inputPath string, recursiveDepth uint8, includeRoot bool, files chan WatchEvent, errors chan error, filters ...WatchFilter)

WatchDir will watch a directory indefinitely for changes and publish them on the given files channel with optional filters.

Types

type DateEntitiesFilter added in v0.12.0

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

DateEntitiesFilter filters fs events by matching ensuring ModTime is within the given date range.

func NewDateEntitiesFilter added in v0.12.0

func NewDateEntitiesFilter(from, to time.Time) DateEntitiesFilter

type DateWatchFilter added in v0.12.0

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

DateWatchFilter filters fs events by matching ensuring ModTime is within the given date range.

func NewDateWatchFilter added in v0.12.0

func NewDateWatchFilter(from, to time.Time) DateWatchFilter

type DirEntitiesFilter added in v0.14.0

type DirEntitiesFilter struct {
}

DirEntitiesFilter only returns sub directories of the target.

func NewDirEntitiesFilter added in v0.14.0

func NewDirEntitiesFilter() DirEntitiesFilter

type DirWatchFilter added in v0.14.0

type DirWatchFilter struct{}

DirWatchFilter only returns sub directories of the target.

func NewDirWatchFilter added in v0.14.0

func NewDirWatchFilter() DirWatchFilter

type EntriesFilter added in v0.12.0

type EntriesFilter interface {
	// contains filtered or unexported methods
}

EntitiesFilter interface facilitates filtering entry slices.

type Entry added in v0.6.0

type Entry struct {
	FileInfo     fs.FileInfo
	AbsolutePath string
	Children     []Entry
}

Entry is the currency of this package.

func FilterEntities added in v0.12.0

func FilterEntities(files []Entry, filters ...EntriesFilter) []Entry

FilterEntities removes files from the slice if they are not accepted by the given filter function.

func List added in v0.14.0

func List(inputPath string, levelsDeep uint8, includeRoot bool, filters ...EntriesFilter) ([]Entry, error)

List is just a convience function to get a slice of files

func NewEntry added in v0.12.0

func NewEntry(inputPath string, levelsDeep uint8, filters ...EntriesFilter) (Entry, error)

NewEntry is the public constructor for creating an Entry. The levelsDeep param controls the level of recursion when collecting file info in subdirectories. levelsDeep == 0 will only create an entry for inputPath. Consider the number of files that may be under the root directory and the memory required to represent them when choosing this value.

func (*Entry) Flatten added in v1.16.0

func (e *Entry) Flatten(includeRoot bool) ([]Entry, error)

List recursively lists all files with optional filters. If includeRoot is true the root directory "inputPath" is included in the results.

func (*Entry) Get added in v1.15.0

func (e *Entry) Get() string

Get fulfils the flag.Getter interface https://pkg.go.dev/flag#Getter.

func (*Entry) IsDir added in v0.9.0

func (e *Entry) IsDir() bool

func (*Entry) Set added in v1.15.0

func (e *Entry) Set(s string) error

Set fulfils the flag.Value interface https://pkg.go.dev/flag#Value.

func (*Entry) String added in v0.6.0

func (e *Entry) String() string

String fulfils the flag.Value interface https://pkg.go.dev/flag#Value.

type FileEntitiesFilter added in v0.14.0

type FileEntitiesFilter struct {
}

FileEntitiesFilter only returns files.

func NewFileEntitiesFilter added in v0.14.0

func NewFileEntitiesFilter() FileEntitiesFilter

type FileWatchFilter added in v0.14.0

type FileWatchFilter struct{}

FileWatchFilter only returns files.

func NewFileWatchFilter added in v0.14.0

func NewFileWatchFilter() FileWatchFilter

type OpWatchFilter added in v0.13.0

type OpWatchFilter struct {
	Ops []fsnotify.Op
}

OpWatchFilter filters fs events by fsnotify.Op event type.

func NewOpWatchFilter added in v0.13.0

func NewOpWatchFilter(ops ...fsnotify.Op) OpWatchFilter

type PermissionsEntitiesFilter added in v0.12.0

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

PermissionsEntitiesFilter filters fs events by ensuring the given file permissions are within the given range.

func NewPermissionsEntitiesFilter added in v0.12.0

func NewPermissionsEntitiesFilter(min, max uint32) PermissionsEntitiesFilter

type PermissionsWatchFilter added in v0.12.0

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

PermissionsWatchFilter filters fs events by ensuring the given file permissions are within the given range.

func NewPermissionsWatchFilter added in v0.12.0

func NewPermissionsWatchFilter(min, max uint32) PermissionsWatchFilter

type RegexEntitiesFilter added in v0.12.0

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

RegexEntitiesFilter filters fs events by matching file names to a given regex.

func NewRegexEntitiesFilter added in v0.12.0

func NewRegexEntitiesFilter(filterRegex *regexp.Regexp) RegexEntitiesFilter

type RegexWatchFilter added in v0.12.0

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

RegexWatchFilter filters fs events by matching file names to a given regex.

func NewRegexWatchFilter added in v0.12.0

func NewRegexWatchFilter(filterRegex *regexp.Regexp) RegexWatchFilter

type SizeEntitiesFilter added in v0.12.0

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

SizeEntitiesFilter filters fs events by ensuring the given file within the given size range (in bytes).

func NewSizeEntitiesFilter added in v0.12.0

func NewSizeEntitiesFilter(min, max int64) SizeEntitiesFilter

type SizeWatchFilter added in v0.12.0

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

SizeWatchFilter filters fs events by ensuring the given file within the given size range (in bytes). Directories are always returned true.

func NewSizeWatchFilter added in v0.12.0

func NewSizeWatchFilter(min, max int64) SizeWatchFilter

type SkipMapEntitiesFilter added in v0.12.0

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

SkipMapEntitiesFilter filters fs events by ensuring the given file is NOT within the given map.

func NewSkipMapEntitiesFilter added in v0.12.0

func NewSkipMapEntitiesFilter(skipMap map[string]struct{}) SkipMapEntitiesFilter

type SkipMapWatchFilter added in v0.12.0

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

SkipMapWatchFilter filters fs events by ensuring the given file is NOT within the given map.

func NewSkipMapWatchFilter added in v0.12.0

func NewSkipMapWatchFilter(skipMap map[string]struct{}) SkipMapWatchFilter

type WatchEvent added in v0.12.1

type WatchEvent struct {
	Entry
	fsnotify.Op
}

WatchEvent is a wrapper for Entry and fsnotify.Op.

type WatchFilter added in v0.12.0

type WatchFilter interface {
	// contains filtered or unexported methods
}

WatchFilter interface facilitates filtering of file events.

Jump to

Keyboard shortcuts

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