file

package
v0.1.0-beta Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2025 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

type CriteriaFunc func(File) (bool, error)

CriteriaFunc defines a function type for filtering files during search operations.

type Extension

type Extension int

Extension represents a file extension type.

const (
	// None represents no file extension.
	None Extension = iota
	// EXE represents the ".exe" file extension.
	EXE
	// GZ represents the ".gz" file extension.
	GZ
	// ZIP represents the ".zip" file extension.
	ZIP
	// Other represents any other, unrecognized file extension.
	Other
)

type File

type File string

File represents a file path as a string, providing methods for file operations.

func NewFile

func NewFile(paths ...string) File

NewFile creates a new File by joining the provided paths.

func (*File) Chmod

func (f *File) Chmod(mode fs.FileMode) error

Chmod changes the file permissions to the specified fs.FileMode.

func (File) Copy

func (f File) Copy(other File) error

Copy copies the contents of the current file to the specified destination File. It returns an error if the operation fails.

func (File) Create

func (f File) Create() error

Create creates a new file.

func (File) Dir

func (f File) Dir() Folder

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

func (f File) Exists() bool

Exists checks if the file exists in the file system.

func (File) Extension

func (f File) Extension() Extension

Extension returns the file extension of the File, mapped to a predefined Extension constant.

func (File) IsDir

func (f File) IsDir() bool

IsDir checks if the path represents a directory.

func (File) IsExecutable

func (f File) IsExecutable() (bool, error)

IsExecutable checks if the file has executable permissions.

func (File) IsFile

func (f File) IsFile() bool

IsFile checks if the path is a regular file (not a directory or special file).

func (File) Name

func (f File) Name() string

Name returns the name (string representation) of the File.

func (*File) Normalize

func (f *File) Normalize()

Normalize converts the file path to use forward slashes.

func (File) Normalized

func (f File) Normalized() File

Normalized converts the file path to use forward slashes.

func (File) Open

func (f File) Open() (*os.File, error)

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

func (f File) OpenForWriting() (*os.File, error)

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) Path added in v0.0.1

func (f File) Path() string

Path returns the path of the File.

func (File) Remove

func (f File) Remove() error

Remove deletes the file from the file system.

func (File) String

func (f File) String() string

String returns the string representation of the File.

func (f File) Symlink(symlinks ...File) error

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

func NewFiles(dir string, paths ...string) (fs Files)

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

func NewFilesFromFile(files ...File) (fs Files)

NewFilesFromFile creates a new Files collection from the provided list of File objects.

func (Files) Paths

func (es Files) Paths() (paths []string)

Paths returns a list of string paths representing all Files in the collection.

func (Files) SymlinksFor

func (es Files) SymlinksFor(file File) error

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

func NewFolder(paths ...string) Folder

NewFolder creates a new Folder object from the provided path segments by joining them.

func (Folder) Create

func (f Folder) Create() error

Create creates the Folder and all necessary parent directories with 0755 permissions.

func (*Folder) CreateInTempDir

func (f *Folder) CreateInTempDir() error

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

func (f *Folder) CreateRandomInDir(dir string) error

CreateRandomInDir creates a new random directory inside the given directory and assigns the generated path to the Folder.

func (*Folder) CreateRandomInTempDir

func (f *Folder) CreateRandomInTempDir() error

CreateRandomInTempDir creates a new random directory inside the system's temporary directory and assigns the generated path to the Folder.

func (Folder) Exists

func (f Folder) Exists() bool

Exists checks if the Folder exists in the file system.

func (*Folder) Expand

func (f *Folder) Expand() error

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

func (f *Folder) IsParentOf(other Folder) bool

IsParentOf determines if the Folder is a parent directory of the given 'other' Folder.

func (Folder) IsSet

func (f Folder) IsSet() bool

IsSet checks whether the Folder has been set to a non-empty value.

func (Folder) ListFiles

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

ListFiles returns a slice of Files representing all files within the current Folder.

func (Folder) ListFolders

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

ListFolders returns a slice of Folders representing all subdirectories within the current Folder.

func (Folder) Name

func (f Folder) Name() string

Name returns the base name (last element) of the Folder's path.

func (Folder) Path

func (f Folder) Path() string

Path returns the Folder path as a string.

func (Folder) Remove

func (f Folder) Remove() error

Remove deletes the Folder and all of its contents.

func (*Folder) Set

func (f *Folder) Set(path string)

Set assigns a new path to the Folder.

func (Folder) String

func (f Folder) String() string

String returns the Folder as a string.

Jump to

Keyboard shortcuts

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