files

package
v0.1.31 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package files provides utilities for common file and directory operations.

Index

Constants

View Source
const (
	DirPerm  = 0o755 // Permissions for new directories.
	FilePerm = 0o644 // Permissions for new files.
)

Variables

View Source
var (
	ErrFileNotFound = errors.New("file not found")
	ErrPathNotFound = errors.New("path not found")
	ErrFileExists   = errors.New("file already exists")
	ErrPathEmpty    = errors.New("path is empty")
	ErrPathExists   = errors.New("path already exists")
)

Functions

func CloseAndClean

func CloseAndClean(f *os.File)

CloseAndClean 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(from, to string) error

Copy copies the contents of a source file to a destination file.

func CreateTemp

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

CreateTemp Creates a temporary file with the provided prefix.

func CreateTempFileWithData

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

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

func Empty

func Empty(s string) bool

Empty returns true if the file at path s has non-zero size.

func EnsureSuffix

func EnsureSuffix(s, suffix string) string

EnsureSuffix 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

func ExpandHomeDir

func ExpandHomeDir(s string) string

func Find

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

Find returns a list of files matching the provided pattern.

func FindByExtList

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

func IsFile

func IsFile(path string) bool

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

func JSONRead

func JSONRead[T any](p string, v *T) error

JSONRead unmarshals the JSON data from the specified file.

func JSONWrite

func JSONWrite[T any](p string, v *T, force bool) (bool, error)

JSONWrite writes the provided data as JSON to the specified file. It uses generics to accept any type `T`.

func List

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

List returns all files found in a given path.

func ListRecursive

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

func ListRootFolders

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

ListRootFolders returns a list of root folders.

func MkdirAll

func MkdirAll(s ...string) error

MkdirAll creates all the given paths.

func ModTime

func ModTime(s, format string) string

ModTime returns the formatted modification time of the specified file.

func PrioritizeFile

func PrioritizeFile(files []string, name string)

PrioritizeFile moves a file to the front of the list.

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 RemoveFilepath

func RemoveFilepath(fname string) error

RemoveFilepath removes the file and its parent directory if empty.

func Rename

func Rename(oldPath, newName string) error

func SizeBytes added in v0.1.25

func SizeBytes(s string) int64

SizeBytes returns the SizeBytes of a file.

func SizeFormatted added in v0.1.25

func SizeFormatted(f string) string

func StripSuffixes

func StripSuffixes(p string) string

StripSuffixes removes all suffixes from the path.

func Touch

func Touch(s string, existsOK bool) (*os.File, error)

Touch creates a file at this given path. If the file already exists, the function succeeds when exist_ok is true.

Types

This section is empty.

Jump to

Keyboard shortcuts

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