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.
Index ¶
- func ExtensionStrings() []string
- type Extension
- type File
- func (f File) Absolute() File
- func (f File) Append(data []byte) error
- func (f File) Base() string
- func (f *File) Chmod(mode fs.FileMode) error
- func (f File) Copy(other File) error
- func (f File) Create() error
- func (f File) Dir() string
- func (f File) Exists() bool
- func (f File) Expanded() File
- func (f File) Extension() Extension
- func (f File) IsDir() bool
- func (f File) IsExecutable() (bool, error)
- func (f File) IsFile() bool
- func (f File) Join(paths ...string) File
- func (f File) Normalized() File
- func (f File) Open() (*os.File, error)
- func (f File) OpenForAppend() (*os.File, error)
- func (f File) OpenForWriting() (*os.File, error)
- func (f File) Path() string
- func (f File) Read() ([]byte, error)
- func (f File) RelativeTo(base File) (File, error)
- func (f File) Remove() error
- func (f File) String() string
- func (f File) Symlink(symlinks ...File) error
- func (f File) Unescape() File
- func (f File) WithoutExtension() File
- func (f File) Write(data []byte) error
- func (f File) WriteYAML(v any) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtensionStrings ¶
func ExtensionStrings() []string
ExtensionStrings returns a slice of all String values of the enum
Types ¶
type Extension ¶
type Extension int
Extension represents common file extension types. Used to categorize files based on their extensions and handle platform-specific behaviors.
const ( // None indicates a file has no extension. None Extension = iota // EXE represents Windows executable files (.exe). EXE // GZ represents gzip compressed files (.gz). GZ // ZIP represents ZIP archive files (.zip). ZIP // TAR represents tape archive files (.tar). TAR // Other represents any unrecognized extension. Other )
func ExtensionString ¶
ExtensionString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
func ExtensionValues ¶
func ExtensionValues() []Extension
ExtensionValues returns all values of the enum
func (Extension) IsAExtension ¶
IsAExtension returns "true" if the value is listed in the enum definition. "false" otherwise
type File ¶
type File string
File represents a filesystem path with associated operations. Provides methods for file manipulation, path operations, and common file system tasks.
func New ¶
New creates a File from one or more path components. Joins the paths using filepath.Join and normalizes the result.
func (File) Append ¶
Append adds binary data to the end of the file. Creates the file if it doesn't exist.
func (*File) Chmod ¶
Chmod modifies the file's permission bits. Takes a fs.FileMode parameter specifying the new permissions.
func (File) Copy ¶
Copy duplicates the file to a new location. Creates the destination file with execute permissions (0755) and copies all content from the source file.
func (File) Create ¶
Create creates a new empty file at this path. Returns an error if the file cannot be created.
func (File) Dir ¶
Dir returns the containing directory path. Returns the path itself if it's a directory, otherwise returns the parent directory path.
func (File) Exists ¶
Exists checks if the path exists in the filesystem. Returns true if the path exists, false otherwise.
func (File) Expanded ¶
Expanded resolves home directory references. Replaces ~ with the user's home directory path.
func (File) Extension ¶
Extension returns the file's extension type. Maps the extension to a predefined constant (e.g., EXE, TAR, ZIP).
func (File) IsDir ¶
IsDir checks if the path is a directory. Returns false for regular files and non-existent paths.
func (File) IsExecutable ¶
IsExecutable checks if the file has execute permissions. Returns true if any execute bit (user/group/other) is set.
func (File) IsFile ¶
IsFile checks if the path is a regular file. Returns false for directories, symlinks, and special files.
func (File) Join ¶
Join combines this path with additional components. Returns a new File with the combined path.
func (File) Normalized ¶
Normalized returns the path with forward slashes. Converts backslashes to forward slashes for consistency.
func (File) Open ¶
Open opens the file for reading and returns a pointer to the os.File object, or an error. The user must close the file after use.
func (File) OpenForAppend ¶
OpenForAppend opens the file for appending and returns a pointer to the os.File object. If the file doesn't exist, it will be created. The user must close the file after use.
func (File) OpenForWriting ¶
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. The user must close the file after use.
func (File) Read ¶
Read retrieves the entire contents of the file. Returns the file contents as a byte slice.
func (File) RelativeTo ¶
RelativeTo computes the relative path from this file to a base path. Returns an error if the relative path cannot be computed.
func (File) Remove ¶
Remove deletes the file from the filesystem. Returns an error if the file cannot be deleted.
func (File) Symlink ¶
Symlink creates symbolic links to this file on Unix-like systems. Takes multiple target paths and creates a symlink at each location. Skips existing symlinks without error, but returns an error if symlink creation fails for any other reason. Not available on Windows.
func (File) Unescape ¶
Unescape decodes URL-escaped characters in the path. Returns the original path if unescaping fails.
func (File) WithoutExtension ¶
WithoutExtension returns the path without file extensions. Handles compound extensions (e.g., .tar.gz) and unknown extensions.