folder

package
v0.0.12-beta Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package folder provides utilities for directory operations. It offers a Folder type that abstracts directory path handling and provides methods for common directory operations like creation, removal, path expansion, and existence checking. The package complements the file package by focusing on directory-specific operations and path manipulations.

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. Handles pathnormalization and expansion of special characters like ~.

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 parent. Use empty string for parent to create in system temp directory. Pattern is used as a prefix for the random directory name.

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.

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.

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) 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) Create

func (f Folder) Create() error

Create ensures the directory and its parents exist. Creates all necessary directories with 0755 permissions.

func (Folder) CreateIgnoreExisting

func (f Folder) CreateIgnoreExisting() error

CreateIgnoreExisting ensures the directory exists. Creates the directory and any missing parent directories. Returns nil if the directory already exists.

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 home directory references. Replaces ~ with the user's home directory path.

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. Returns ErrNotFound if no file matches all criteria.

func (Folder) IsParentOf

func (f Folder) IsParentOf(other Folder) bool

IsParentOf checks if this folder contains another folder. Returns true if the other folder's path starts with this folder's path.

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) ListFiles

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

ListFiles returns all immediate file entries. Returns a Files collection containing all regular files, excluding directories and other filesystem objects.

func (Folder) ListFolders

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

ListFolders returns all immediate subdirectories. Returns a slice of Folders for each directory entry, excluding regular files and other filesystem objects.

func (Folder) Normalized

func (f Folder) Normalized() Folder

Normalized returns the path with forward slashes. Converts backslashes to forward slashes for consistency.

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 computes the relative path from this folder to a base path. Returns an error if the relative path cannot be computed.

func (Folder) Remove

func (f Folder) Remove() error

Remove recursively deletes the directory and its contents.

func (Folder) String

func (f Folder) String() string

String returns the folder path as a string.

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