folder

package
v0.2.1-beta Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package folder provides a typed abstraction over filesystem directory paths. It offers utilities for path manipulation, creation, listing, and file search.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("file not found")

ErrNotFound indicates a file matching the search criteria was not found.

Functions

This section is empty.

Types

type CriteriaFunc

type CriteriaFunc func(file.File) (bool, error)

CriteriaFunc defines a file matching predicate. Returns true if a file matches the criteria, false otherwise.

type Folder

type Folder string

Folder represents a filesystem directory path. Provides methods for directory operations including creation removal, path manipulation, and file searching.

func CreateRandomInDir

func CreateRandomInDir(dir, pattern string) (Folder, error)

CreateRandomInDir creates a uniquely named directory. Creates a directory with a random name inside the specified directory. Use empty string for directory to create in system temp directory. Pattern is used as a prefix for the random directory name.

func Cwd

func Cwd() (Folder, error)

Cwd returns the current working directory as a Folder.

func FromFile

func FromFile(f file.File) Folder

FromFile creates a Folder from a file's parent directory. Extracts the directory component from the given file path. See `New` for details on path normalization. Note: This does not create the directory, only constructs the path.

func Home

func Home() (Folder, error)

Home returns the user's home directory as a Folder.

func New

func New(paths ...string) Folder

New creates a Folder from one or more path components. Joins the paths using filepath.Join and normalizes the result to use forward slashes. Note: This does not create the directory, only constructs the path.

func NewInTempDir

func NewInTempDir(paths ...string) Folder

NewInTempDir creates a Folder path in the system temp directory. Combines the system temp directory with the provided path components. Note: This does not create the directory, only constructs the path.

func (Folder) Absolute

func (f Folder) Absolute() Folder

Absolute returns the absolute path of the folder.

func (Folder) AsFile

func (f Folder) AsFile() file.File

AsFile converts the folder path to a File type.

func (Folder) Base

func (f Folder) Base() string

Base returns the last component of the folder path.

func (Folder) Chmod

func (f Folder) Chmod(mode fs.FileMode) error

Chmod modifies the directory's permission bits.

func (Folder) Create

func (f Folder) Create(perm ...fs.FileMode) error

Create ensures the directory and its parents exist.

An optional permission mode can be provided (default 0o755). If multiple values are given, only the first is used.

func (Folder) Dir added in v0.1.11

func (f Folder) Dir() Folder

Dir returns the parent directory of this folder.

func (Folder) Exists

func (f Folder) Exists() bool

Exists checks if the directory exists in the filesystem.

func (Folder) Expanded

func (f Folder) Expanded() Folder

Expanded resolves environment variables including `~` home directory references.

func (Folder) FindFile

func (f Folder) FindFile(criteria ...CriteriaFunc) (file.File, error)

FindFile searches for a file matching all given criteria. Recursively searches the directory tree and returns the first matching file, with the shortest path. Returns ErrNotFound if no file matches all criteria.

func (Folder) FindFiles added in v0.1.5

func (f Folder) FindFiles(criteria ...CriteriaFunc) (files.Files, error)

FindFiles searches for files matching all given criteria. Recursively searches the directory tree and returns all matching files. Returns an empty slice if no files match all criteria.

func (Folder) Glob

func (f Folder) Glob(pattern string) (files.Files, error)

Glob returns all files matching the given pattern within the folder. Uses filepath.Glob semantics (non-recursive, standard glob syntax).

func (Folder) Info added in v0.0.13

func (f Folder) Info() (fs.FileInfo, error)

Info retrieves the file information for the directory.

func (Folder) IsAbs

func (f Folder) IsAbs() bool

IsAbs reports whether the folder path is absolute.

func (Folder) IsSet

func (f Folder) IsSet() bool

IsSet checks if the folder path is non-empty.

func (Folder) Join

func (f Folder) Join(paths ...string) Folder

Join combines this path with additional components. Returns a new Folder with the combined path.

func (Folder) List

func (f Folder) List() ([]fs.DirEntry, error)

List returns all entries (files and directories) in the folder.

func (Folder) ListFiles

func (f Folder) ListFiles() (files files.Files, err error)

ListFiles returns all immediate regular files in the folder. It excludes directories and other non-file entries.

func (Folder) ListFolders

func (f Folder) ListFolders() (folders []Folder, err error)

ListFolders returns all immediate subdirectories of the folder. It excludes files and other non-directory entries.

func (Folder) Path

func (f Folder) Path() string

Path returns the string representation of the folder path.

func (Folder) RelativeTo

func (f Folder) RelativeTo(base Folder) (Folder, error)

RelativeTo returns the path from base to this folder. Returns an error if it can't be computed.

func (Folder) Remove

func (f Folder) Remove() error

Remove recursively deletes the directory and its contents.

func (Folder) Size added in v0.1.0

func (f Folder) Size() (int64, error)

Size returns the size of the folder in bytes.

func (Folder) String

func (f Folder) String() string

String returns the folder path as a string.

func (Folder) Walk

func (f Folder) Walk(fn func(path file.File, d fs.DirEntry, err error) error) error

Walk traverses the directory tree rooted at f, calling fn for each entry. The error parameter from filepath.WalkDir is passed through — return nil to skip the error, or return it to abort the walk. Return filepath.SkipDir to skip a directory.

func (Folder) WithFile

func (f Folder) WithFile(path string) file.File

WithFile creates a File path within this directory. Combines the directory path with the provided filename.

Jump to

Keyboard shortcuts

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