file

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 9 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.

Index

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

func ExtensionString(s string) (Extension, error)

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

func (i Extension) IsAExtension() bool

IsAExtension returns "true" if the value is listed in the enum definition. "false" otherwise

func (Extension) String

func (i Extension) String() string

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

func New(paths ...string) File

New creates a File from one or more path components. Joins the paths using filepath.Join and normalizes the result.

func (File) Absolute

func (f File) Absolute() File

Absolute returns the absolute path of the file.

func (File) Append

func (f File) Append(data []byte) error

Append adds binary data to the end of the file. Creates the file if it doesn't exist.

func (File) Base

func (f File) Base() string

Base returns the last component of the file path.

func (*File) Chmod

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

Chmod modifies the file's permission bits. Takes a fs.FileMode parameter specifying the new permissions.

func (File) Copy

func (f File) Copy(other File) error

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

func (f File) Create() error

Create creates a new empty file at this path. Returns an error if the file cannot be created.

func (File) Dir

func (f File) Dir() string

Dir returns the containing directory path. Returns the path itself if it's a directory, otherwise returns the parent directory path.

func (File) Exists

func (f File) Exists() bool

Exists checks if the path exists in the filesystem. Returns true if the path exists, false otherwise.

func (File) Expanded

func (f File) Expanded() File

Expanded resolves home directory references. Replaces ~ with the user's home directory path.

func (File) Extension

func (f File) Extension() Extension

Extension returns the file's extension type. Maps the extension to a predefined constant (e.g., EXE, TAR, ZIP).

func (File) IsDir

func (f File) IsDir() bool

IsDir checks if the path is a directory. Returns false for regular files and non-existent paths.

func (File) IsExecutable

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

IsExecutable checks if the file has execute permissions. Returns true if any execute bit (user/group/other) is set.

func (File) IsFile

func (f File) IsFile() bool

IsFile checks if the path is a regular file. Returns false for directories, symlinks, and special files.

func (File) Join

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

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

func (File) Normalized

func (f File) Normalized() File

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

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. The user must close the file after use.

func (File) OpenForAppend

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

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

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. The user must close the file after use.

func (File) Path

func (f File) Path() string

Path returns the string representation of the file path.

func (File) Read

func (f File) Read() ([]byte, error)

Read retrieves the entire contents of the file. Returns the file contents as a byte slice.

func (File) RelativeTo

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

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

func (f File) Remove() error

Remove deletes the file from the filesystem. Returns an error if the file cannot be deleted.

func (File) String

func (f File) String() string

String returns the file path as a string.

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

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

func (f File) Unescape() File

Unescape decodes URL-escaped characters in the path. Returns the original path if unescaping fails.

func (File) WithoutExtension

func (f File) WithoutExtension() File

WithoutExtension returns the path without file extensions. Handles compound extensions (e.g., .tar.gz) and unknown extensions.

func (File) Write

func (f File) Write(data []byte) error

Write stores binary data in the file. Creates or truncates the file before writing.

func (File) WriteYAML

func (f File) WriteYAML(v any) error

WriteYAML serializes data to YAML format and writes it. Marshals the provided value to YAML and writes to the file.

Jump to

Keyboard shortcuts

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