Documentation
¶
Overview ¶
Package file provides utilities for handling folder and file operations.
The `File` type is a string-based abstraction over file paths that simplifies common file operations. Additionally, the package supports searching for files that meet specific criteria, handling file extensions, and working with collections of files through the `Files` type.
The `Folder` type is a string-based abstraction over a directory paths. The package includes methods for creating, removing, expanding, and checking the existence of directories, as well as manipulating paths.
Index ¶
- Variables
- type CriteriaFunc
- type Extension
- type File
- func (f *File) Chmod(mode fs.FileMode) error
- func (f File) Copy(other File) error
- func (f File) Create() error
- func (f File) Dir() Folder
- func (f File) Exists() bool
- func (f File) Extension() Extension
- func (f File) IsDir() bool
- func (f File) IsExecutable() (bool, error)
- func (f File) IsFile() bool
- func (f File) Name() string
- func (f *File) Normalize()
- func (f File) Normalized() File
- func (f File) Open() (*os.File, error)
- func (f File) OpenForWriting() (*os.File, error)
- func (f File) Path() string
- func (f File) Remove() error
- func (f File) String() string
- func (f File) Symlink(symlinks ...File) error
- type Files
- type Folder
- func (f Folder) Create() error
- func (f *Folder) CreateInTempDir() error
- func (f *Folder) CreateRandomInDir(dir string) error
- func (f *Folder) CreateRandomInTempDir() error
- func (f Folder) Exists() bool
- func (f *Folder) Expand() error
- func (f Folder) FindFile(criteria ...CriteriaFunc) (File, error)
- func (f *Folder) IsParentOf(other Folder) bool
- func (f Folder) IsSet() bool
- func (f Folder) ListFiles() (Files, error)
- func (f Folder) ListFolders() ([]Folder, error)
- func (f Folder) Name() string
- func (f Folder) Path() string
- func (f Folder) Remove() error
- func (f *Folder) Set(path string)
- func (f Folder) String() string
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("file not found")
ErrNotFound is returned when a file is not found during a search operation.
Functions ¶
This section is empty.
Types ¶
type CriteriaFunc ¶
CriteriaFunc defines a function type for filtering files during search operations.
type File ¶
type File string
File represents a file path as a string, providing methods for file operations.
func (File) Copy ¶
Copy copies the contents of the current file to the specified destination File. It returns an error if the operation fails.
func (File) Dir ¶
Dir returns the file.Folder object representing the directory of the file. If it is actually a folder, it returns itself as a Folder object.
func (File) Extension ¶
Extension returns the file extension of the File, mapped to a predefined Extension constant.
func (File) IsExecutable ¶
IsExecutable checks if the file has executable permissions.
func (*File) Normalize ¶
func (f *File) Normalize()
Normalize converts the file path to use forward slashes.
func (File) Normalized ¶
Normalized converts the file path to use forward slashes.
func (File) Open ¶
Open opens the file for reading and returns a pointer to the os.File object, or an error.
func (File) OpenForWriting ¶ added in v0.0.1
OpenForWriting opens the file for writing and returns a pointer to the os.File object. If the file doesn't exist, it will be created. If it exists, it will be truncated.
func (File) Symlink ¶
Symlink creates symbolic links for the File to each of the provided symlink Files on Linux or Darwin systems. If a symlink already exists, it will skip that symlink and continue without returning an error. Returns an error if any symlink creation fails (excluding existing symlinks).
type Files ¶
type Files []File
Files represents a collection of File objects.
func NewFiles ¶
NewFiles creates a new Files collection from the provided list of paths. The paths are joined with the provided directory to create the full file paths. Pass `dir` as an empty string to use the paths as-is.
func NewFilesFromFile ¶
NewFilesFromFile creates a new Files collection from the provided list of File objects.
func (Files) SymlinksFor ¶
SymlinksFor creates symbolic links for all Files in the collection, linking them to the specified target File. It returns an error if the operation fails.
type Folder ¶
type Folder string
Folder represents a file system directory as a string. It provides methods for working with directories, such as creating, removing, expanding paths, and checking existence.
func NewFolder ¶
NewFolder creates a new Folder object from the provided path segments by joining them.
func (Folder) Create ¶
Create creates the Folder and all necessary parent directories with 0755 permissions.
func (*Folder) CreateInTempDir ¶
CreateInTempDir creates a directory inside the system's temporary directory using the Folder's name and assigns the path to the Folder.
func (*Folder) CreateRandomInDir ¶
CreateRandomInDir creates a new random directory inside the given directory and assigns the generated path to the Folder.
func (*Folder) CreateRandomInTempDir ¶
CreateRandomInTempDir creates a new random directory inside the system's temporary directory and assigns the generated path to the Folder.
func (*Folder) Expand ¶
Expand expands a Folder path that begins with "~" to the user's home directory.
func (Folder) FindFile ¶
func (f Folder) FindFile(criteria ...CriteriaFunc) (File, error)
FindFile searches for a file in the Folder that matches the provided criteria. It returns the first file found or an error if none are found.
func (*Folder) IsParentOf ¶
IsParentOf determines if the Folder is a parent directory of the given 'other' Folder.
func (Folder) ListFiles ¶
ListFiles returns a slice of Files representing all files within the current Folder.
func (Folder) ListFolders ¶
ListFolders returns a slice of Folders representing all subdirectories within the current Folder.