ftree

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 6 Imported by: 0

README

Ftree

ftree provides tools for building and traversing "file trees". A Walker can be used to walk a FileTree with Steppers, which are conditionally given the files they want to process. This allows for multiple functions to read the same file.

Usage

FileTree
Building a FileTree

To build a FileTree, use the Build function with any valid path:

ft, err := ftree.Build("/path/to/root")
if err != nil {
    // Handle error
}
Traversing a FileTree

To traverse the FileTree and perform actions on each directory, use the Traverse method of the Dir type:

ft.Traverse(func(d *ftree.Dir) {
    // Do something with the directory
})
Finding a Directory or File

To find a directory in the FileTree, use the Find method of FileTree :

dir := ft.Find("/path/to/dir")

To find a file in the FileTree, use the FindFile method of FileTree:

file := ft.FindFile("/path/to/file.txt")
Walker
Building a Walker

To build a Walker, use the BuildWalker function with any valid path and one or more Stepper interfaces:

walker, err := ftree.BuildWalker("/path/to/root", myStepper, myStepper2)
if err != nil {
	// Handle error
}
Making a Stepper

To make a Stepper, define a struct that implements the Stepper interface:

type MyStepper struct{}

func (s *MyStepper) Wants(ext string) bool {
    return ext == ".txt"
}

func (s *MyStepper) Walk(e ftree.Entry, r io.Reader) error {
    // Do something with the file
    return nil
}

In this example, we define a custom Stepper called MyStepper. This Stepper only wants to process files with the .txt extension, as indicated by the Wants method. The Walk method of the Stepper performs some action on the file, such as reading its contents.

Walking a FileTree with Steppers

To walk a FileTree and perform actions on each file, use the Walk method of the Walker:

walker, err := ftree.BuildWalker("/path/to/root",
	myStepper,
	myStepper2
)
if err != nil {
    // Handle error
}

walker.AddStepper(myStepper3)

err = walker.Walk()
if err != nil {
    // Handle error
}

The Walker can be built with one or more Stepper interfaces as parameters to BuildWalker and additional Steppers can be added with the AddStepper method. When the Walk method is called, the Walker will process each file with the Steppers that it contains, only reading files which are wanted. Each file is read only once, even if it is wanted by multiple Steppers.

Walking a FileTree

To walk a FileTree and perform actions on each file, use the Walker type:

walker, err := ftree.BuildWalker("/path/to/root",
	myStepper1,
	myStepper2,
)
if err != nil {
    // Handle error
}

err = walker.Walk()
if err != nil {
    // Handle error
}

The BuildWalker function takes one or more Stepper interfaces as arguments. These Stepper interfaces define the actions to be taken on each file. Only files with the desired extensions are processed by the Stepper.

type MyStepper struct{}

func (s *MyStepper) Wants(ext string) bool {
    return ext == ".txt"
}

func (s *MyStepper) Walk(e ftree.Entry, r io.Reader) error {
    // Do something with the file
    return nil
}

walker, err := ftree.BuildWalker("/path/to/root", &MyStepper{})
if err != nil {
    // Handle error
}

err = walker.Walk()
if err != nil {
    // Handle error
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dir

type Dir struct {
	// Fp contains the absolute path, relative path and current directory name
	Fp       Fpath            `json:"fpath,omitempty"`
	Children map[string]*Dir  `json:"children,omitempty"`
	Files    map[string]Entry `json:"files,omitempty"`
}

Dir is a directory in the filetree. It contains a map of its children directories and a map of its files.

func (*Dir) Traverse

func (d *Dir) Traverse(f func(*Dir))

Traverse traverses the file tree recursively, calling the function f on each directory.

type Entry

type Entry interface {
	// Ext returns the file extension in lowercase
	Ext() string

	// Abs returns the absolute path to the file
	Abs() string

	// Rel returns the relative path from the root of where it was scanned
	// to the file. This is the path that is used to find the file in the
	// FileTree.
	Rel() string

	// Dir returns the directory name of the file
	Dir() string

	fs.DirEntry
}

type FileTree

type FileTree struct {
	Root *Dir `json:"head"`
}

FileTree is a tree of directories and files.

func Build

func Build(root string) (*FileTree, error)

Build builds a filetree with the given path as root directory.

func (*FileTree) Find

func (ft *FileTree) Find(path string) *Dir

Find finds a directory in the filetree by its path. The path requested must define the full path relative to the root directory.

func (*FileTree) FindFile

func (ft *FileTree) FindFile(path string) Entry

FindFile finds a file in the filetree by its path.

func (*FileTree) Traverse

func (ft *FileTree) Traverse(f func(*Dir))

Traverse calls traverse on the root directory of the file tree.

type Fpath

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

func NewFpath

func NewFpath(root string) (*Fpath, error)

NewFpath creates a new Fpath struct from a path

func (*Fpath) Abs

func (f *Fpath) Abs() string

Abs returns the absolute path to the file

func (*Fpath) Dir

func (f *Fpath) Dir() string

Dir returns the directory name of the file

func (*Fpath) Rel

func (f *Fpath) Rel() string

Rel returns the relative path from the root of where it was scanned to the file. This is the path that is used to find the file in the FileTree.

type Stepper

type Stepper interface {
	// Walk is ran on each file in the FileTree.
	Walk(e Entry, r io.Reader) error
	// Given an extension, returns true if the Stepper wants to read the file.
	Wants(ext string) bool
}

Stepper is an interface used to act in place of fs.WalkDirFunc. To determine if a Stepper wants to read a file, the extension of a file is passed onto the want method.

type Walker

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

Walker is used to walk a FileTree and run Steppers on each file. This makes it easier to do things like read files which multiple Steppers may want to read.

func BuildWalker

func BuildWalker(root string, steppers ...Stepper) (*Walker, error)

BuildWalker returns a new Walker with the given root and steppers.

func NewWalker

func NewWalker(ft *FileTree) *Walker

NewWalker returns a new Walker with the given FileTree.

func (*Walker) AddStepper

func (w *Walker) AddStepper(s Stepper)

AddStepper adds a Stepper to the Walker.

func (*Walker) Walk

func (w *Walker) Walk() error

Walk walks the FileTree and runs the Steppers on each file. Only files which are wanted are ever read. Walk is ran by traversing the filetree directory by directory.

Jump to

Keyboard shortcuts

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