Documentation
¶
Overview ¶
Package files provides utilities for common file and directory operations.
Index ¶
- Constants
- Variables
- func CloseAndRemove(f *os.File)
- func CollapseHomeDir(p string) string
- func Copy(src, dst string) error
- func EnsureExt(s, suffix string) string
- func Exists(s string) bool
- func ExistsErr(p string) error
- func ExpandHomeDir(s string) string
- func Find(root, pattern string) ([]string, error)
- func FindByExtension(root string, ext ...string) ([]string, error)
- func IsDir(path string) bool
- func IsEmpty(f string) bool
- func IsEmptyPath(path string) (bool, error)
- func IsFile(path string) bool
- func List(root, pattern string) ([]string, error)
- func ListRootFolders(root string, ignore ...string) ([]string, error)
- func ListWithExclude(path, fileExt string, exclude ...string) ([]string, error)
- func MkdirAll(s ...string) error
- func New(path string, existsOK bool) (*os.File, error)
- func NewJSON[T any](path string, v *T) error
- func NewTempFile(prefix, ext string) (*os.File, error)
- func NewTempFileWithData(prefix, extension string, d []byte) (*os.File, error)
- func NewWithData(path string, data []byte, existsOK bool) (*os.File, error)
- func NormalizePath(filename, def string) (string, error)
- func PrioritizeFile(files []string, name string)
- func Read(path string) ([]byte, error)
- func ReadJSON[T any](s string, v *T) error
- func Remove(s string) error
- func RemoveAll(s string) error
- func RemoveEmptyDirs(root string) error
- func RemovePruneEmpty(fname string) error
- func SizeBytes(s string) int64
- func SizeFormatted(f string) string
- func SizeToHuman(b int64) string
- func StripExts(p string) string
- func WriteJSON[T any](path string, v *T) error
- func WriteJSONIfChanged[T any](path string, v *T, force bool) (bool, error)
- type FileManager
- func (fm *FileManager) Abs(path string) (string, error)
- func (fm *FileManager) Base(path string) string
- func (fm *FileManager) Dir(path string) string
- func (fm *FileManager) Exists(path string) bool
- func (fm *FileManager) ExistsErr(path string) error
- func (fm *FileManager) Ext(path string) string
- func (fm *FileManager) Find(root, pattern string) ([]string, error)
- func (fm *FileManager) FindWithExclude(path, fileExt string, exclude ...string) ([]string, error)
- func (fm *FileManager) IsDir(path string) bool
- func (fm *FileManager) IsEmpty(path string) bool
- func (fm *FileManager) IsEmptyPath(path string) (bool, error)
- func (fm *FileManager) IsFile(path string) bool
- func (fm *FileManager) Join(elem ...string) string
- func (fm *FileManager) Mkdir(path string) error
- func (fm *FileManager) MkdirAll(s ...string) error
- func (fm *FileManager) Move(src, dst string) error
- func (fm *FileManager) New(path string, existsOK bool) (*os.File, error)
- func (fm *FileManager) NewTempFile(prefix, ext string) (*os.File, error)
- func (fm *FileManager) Read(path string) ([]byte, error)
- func (fm *FileManager) Rm(path string) error
- func (fm *FileManager) RmAll(path string) error
- func (fm *FileManager) RmEmptyDirs(root string) error
- func (fm *FileManager) Size(path string) int64
Constants ¶
const ( // DirPerm defines default private directory permissions. DirPerm = 0o700 // FilePerm defines default private file permissions. FilePerm = 0o600 // DirPermPublic defines default public directory permissions. DirPermPublic = 0o755 // FilePermPublic defines default public file permissions. FilePermPublic = 0o644 )
Variables ¶
var ( // ErrPathNotFound indicates that the specified path does not exist. ErrPathNotFound = errors.New("path not found") // ErrPathEmpty indicates that the path is empty. ErrPathEmpty = errors.New("path is empty") // ErrInvalidFilename indicates that the filename is invalid. ErrInvalidFilename = errors.New("invalid filename") // ErrInvalidFilepath indicates that the filepath is invalid. ErrInvalidFilepath = errors.New("invalid filepath") )
var DefaultManager = NewFileManager()
DefaultManager is the default FileManager instance for package-level file operations.
Functions ¶
func CloseAndRemove ¶
CloseAndRemove closes the provided file and deletes the associated temporary file.
func CollapseHomeDir ¶
CollapseHomeDir replaces the home directory with a tilde (~).
func ExpandHomeDir ¶
ExpandHomeDir expands a leading "~/" to the user's home directory.
func FindByExtension ¶
FindByExtension finds files matching the given extensions under a root path.
func IsEmptyPath ¶
IsEmptyPath reports whether the directory at path is empty.
func ListRootFolders ¶
ListRootFolders returns a list of root folders.
func ListWithExclude ¶
ListWithExclude returns files matching an extension, excluding specified names.
func New ¶
New creates a file at this given path. If the file already exists, the function succeeds when existsOK is true.
func NewJSON ¶ added in v0.1.1
NewJSON writes v to path as JSON, failing if the file already exists.
func NewTempFile ¶
NewTempFile creates a temporary file with the provided prefix.
func NewTempFileWithData ¶
NewTempFileWithData creates a temporary file and writes the provided data to it.
func NewWithData ¶
NewWithData creates a file and writes the provided data to it.
func NormalizePath ¶
NormalizePath returns a valid path using def for missing or invalid components.
func PrioritizeFile ¶
PrioritizeFile moves a file to the front of the list.
func RemoveEmptyDirs ¶
RemoveEmptyDirs removes empty directories.
func RemovePruneEmpty ¶
RemovePruneEmpty removes the file and prunes the parent directory if left empty.
func SizeFormatted ¶
SizeFormatted returns the file size in a human-readable format (KB, MB, GB).
Types ¶
type FileManager ¶
type FileManager struct{}
FileManager provides file system operations.
func NewFileManager ¶
func NewFileManager() *FileManager
NewFileManager returns a new FileManager instance.
func (*FileManager) Abs ¶
func (fm *FileManager) Abs(path string) (string, error)
Abs returns the absolute path of a path.
func (*FileManager) Base ¶
func (fm *FileManager) Base(path string) string
Base returns the last element of path.
func (*FileManager) Dir ¶
func (fm *FileManager) Dir(path string) string
Dir returns the directory portion of a path.
func (*FileManager) Exists ¶
func (fm *FileManager) Exists(path string) bool
Exists checks if a file exists.
func (*FileManager) ExistsErr ¶
func (fm *FileManager) ExistsErr(path string) error
ExistsErr returns an error if a path does not exist or cannot be accessed.
func (*FileManager) Ext ¶
func (fm *FileManager) Ext(path string) string
Ext returns the file extension of a path.
func (*FileManager) Find ¶
func (fm *FileManager) Find(root, pattern string) ([]string, error)
Find returns a list of files matching the provided pattern.
func (*FileManager) FindWithExclude ¶
func (fm *FileManager) FindWithExclude(path, fileExt string, exclude ...string) ([]string, error)
FindWithExclude returns files matching an extension, excluding specified names.
func (*FileManager) IsDir ¶
func (fm *FileManager) IsDir(path string) bool
IsDir checks if the given path exists and refers to a directory.
func (*FileManager) IsEmpty ¶
func (fm *FileManager) IsEmpty(path string) bool
IsEmpty reports whether the file at path f has no content.
func (*FileManager) IsEmptyPath ¶
func (fm *FileManager) IsEmptyPath(path string) (bool, error)
IsEmptyPath reports whether the directory at path is empty.
func (*FileManager) IsFile ¶
func (fm *FileManager) IsFile(path string) bool
IsFile checks if the given path exists and refers to a regular file.
func (*FileManager) Join ¶
func (fm *FileManager) Join(elem ...string) string
Join joins path elements into a single path.
func (*FileManager) Mkdir ¶
func (fm *FileManager) Mkdir(path string) error
Mkdir creates a new directory at the specified path.
func (*FileManager) MkdirAll ¶
func (fm *FileManager) MkdirAll(s ...string) error
MkdirAll creates all the given paths.
func (*FileManager) Move ¶
func (fm *FileManager) Move(src, dst string) error
Move moves/renames a file from src to dst.
func (*FileManager) NewTempFile ¶
func (fm *FileManager) NewTempFile(prefix, ext string) (*os.File, error)
NewTempFile creates a temporary file with the provided prefix.
func (*FileManager) Read ¶
func (fm *FileManager) Read(path string) ([]byte, error)
Read reads the entire contents of a file.
func (*FileManager) Rm ¶
func (fm *FileManager) Rm(path string) error
Rm removes the specified file if it exists.
func (*FileManager) RmAll ¶
func (fm *FileManager) RmAll(path string) error
RmAll removes the specified file if it exists.
func (*FileManager) RmEmptyDirs ¶
func (fm *FileManager) RmEmptyDirs(root string) error
RmEmptyDirs removes empty directories.
func (*FileManager) Size ¶
func (fm *FileManager) Size(path string) int64
Size returns the size of a file in bytes.