files

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 11 Imported by: 0

README

gofiles

Utilities for common file and directory operations.

Extracted from gomarks

Documentation

Overview

Package files provides utilities for common file and directory operations.

Index

Constants

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

View Source
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")
)
View Source
var DefaultManager = NewFileManager()

DefaultManager is the default FileManager instance for package-level file operations.

Functions

func CloseAndRemove

func CloseAndRemove(f *os.File)

CloseAndRemove closes the provided file and deletes the associated temporary file.

func CollapseHomeDir

func CollapseHomeDir(p string) string

CollapseHomeDir replaces the home directory with a tilde (~).

func Copy

func Copy(src, dst string) error

Copy copies a file from src to dst, overwriting if dst exists.

func EnsureExt

func EnsureExt(s, suffix string) string

EnsureExt appends the specified suffix to the filename.

func Exists

func Exists(s string) bool

Exists checks if a file exists.

func ExistsErr

func ExistsErr(p string) error

ExistsErr returns an error if a path does not exist or cannot be accessed.

func ExpandHomeDir

func ExpandHomeDir(s string) string

ExpandHomeDir expands a leading "~/" to the user's home directory.

func Find

func Find(root, pattern string) ([]string, error)

Find returns a list of files matching the provided pattern.

func FindByExtension

func FindByExtension(root string, ext ...string) ([]string, error)

FindByExtension finds files matching the given extensions under a root path.

func IsDir

func IsDir(path string) bool

IsDir checks if the given path exists and refers to a directory.

func IsEmpty

func IsEmpty(f string) bool

IsEmpty reports whether the file at path f has no content.

func IsEmptyPath

func IsEmptyPath(path string) (bool, error)

IsEmptyPath reports whether the directory at path is empty.

func IsFile

func IsFile(path string) bool

IsFile checks if the given path exists and refers to a regular file.

func List

func List(root, pattern string) ([]string, error)

List returns all files found in a given path.

func ListRootFolders

func ListRootFolders(root string, ignore ...string) ([]string, error)

ListRootFolders returns a list of root folders.

func ListWithExclude

func ListWithExclude(path, fileExt string, exclude ...string) ([]string, error)

ListWithExclude returns files matching an extension, excluding specified names.

func MkdirAll

func MkdirAll(s ...string) error

MkdirAll creates all the given paths.

func New

func New(path string, existsOK bool) (*os.File, error)

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

func NewJSON[T any](path string, v *T) error

NewJSON writes v to path as JSON, failing if the file already exists.

func NewTempFile

func NewTempFile(prefix, ext string) (*os.File, error)

NewTempFile creates a temporary file with the provided prefix.

func NewTempFileWithData

func NewTempFileWithData(prefix, extension string, d []byte) (*os.File, error)

NewTempFileWithData creates a temporary file and writes the provided data to it.

func NewWithData

func NewWithData(path string, data []byte, existsOK bool) (*os.File, error)

NewWithData creates a file and writes the provided data to it.

func NormalizePath

func NormalizePath(filename, def string) (string, error)

NormalizePath returns a valid path using def for missing or invalid components.

func PrioritizeFile

func PrioritizeFile(files []string, name string)

PrioritizeFile moves a file to the front of the list.

func Read

func Read(path string) ([]byte, error)

Read reads the entire contents of a file.

func ReadJSON added in v0.1.1

func ReadJSON[T any](s string, v *T) error

ReadJSON unmarshals the JSON data from the specified file.

func Remove

func Remove(s string) error

Remove removes the specified file if it exists.

func RemoveAll

func RemoveAll(s string) error

RemoveAll removes the specified file if it exists.

func RemoveEmptyDirs

func RemoveEmptyDirs(root string) error

RemoveEmptyDirs removes empty directories.

func RemovePruneEmpty

func RemovePruneEmpty(fname string) error

RemovePruneEmpty removes the file and prunes the parent directory if left empty.

func SizeBytes

func SizeBytes(s string) int64

SizeBytes returns the SizeBytes of a file.

func SizeFormatted

func SizeFormatted(f string) string

SizeFormatted returns the file size in a human-readable format (KB, MB, GB).

func SizeToHuman

func SizeToHuman(b int64) string

SizeToHuman converts bytes to KB, MB, or GB.

func StripExts

func StripExts(p string) string

StripExts removes all suffixes from the path.

func WriteJSON added in v0.1.1

func WriteJSON[T any](path string, v *T) error

WriteJSON writes v to path as JSON, overwriting the file if it exists.

func WriteJSONIfChanged added in v0.1.1

func WriteJSONIfChanged[T any](path string, v *T, force bool) (bool, error)

WriteJSONIfChanged writes v to path as JSON only if the content differs from what's already there (or force is true / the file doesn't exist).

Returns whether a write occurred.

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

func (fm *FileManager) New(path string, existsOK bool) (*os.File, error)

New creates a file at this given path.

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.

Jump to

Keyboard shortcuts

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