fileutil

package
v1.20.7 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyDir

func CopyDir(src string, dst string) (r result.VoidResult)

CopyDir copies a whole directory recursively and returns a VoidResult.

func CopyFile

func CopyFile(src, dst string) result.VoidResult

CopyFile copies a single file from src to dst and returns a VoidResult.

func FileExist

func FileExist(name string) (existed bool, isDir bool)

FileExist reports whether the named file or directory exists. It returns two values: whether the file exists, and whether it's a directory.

func FileExists

func FileExists(name string) bool

FileExists reports whether the named file or directory exists. It returns true if the file exists, false otherwise.

func FilepathAbsolute

func FilepathAbsolute(paths []string) (r result.Result[[]string])

FilepathAbsolute converts all paths to absolute paths and returns a Result.

func FilepathAbsoluteMap

func FilepathAbsoluteMap(paths []string) (r result.Result[map[string]string])

FilepathAbsoluteMap converts all paths to absolute paths and returns a Result.

func FilepathContains

func FilepathContains(basepath string, subpaths []string) (r result.VoidResult)

FilepathContains checks if the basepath contains all the subpaths and returns a VoidResult.

func FilepathDistinct

func FilepathDistinct(paths []string, toAbs bool) (r result.Result[[]string])

FilepathDistinct removes duplicate paths and returns a Result. If toAbs is true, returns absolute paths; otherwise returns original paths.

func FilepathRelative

func FilepathRelative(basepath string, targpaths []string) (r result.Result[[]string])

FilepathRelative converts all target paths to relative paths from the base path and returns a Result.

func FilepathRelativeMap

func FilepathRelativeMap(basepath string, targpaths []string) (r result.Result[map[string]string])

FilepathRelativeMap converts all target paths to relative paths from the base path and returns a Result.

func FilepathSame

func FilepathSame(path1, path2 string) (r result.Result[bool])

FilepathSame checks if the two paths refer to the same file or directory and returns a Result.

func FilepathSlashInsensitive

func FilepathSlashInsensitive(path string) string

FilepathSlashInsensitive ignores the difference between the slash and the backslash, and converts to the same as the current system.

func FilepathSplitExt

func FilepathSplitExt(filename string, slashInsensitive ...bool) (root, ext string)

FilepathSplitExt splits the filename into a pair (root, ext) such that root + ext == filename, and ext is empty or begins with a period and contains at most one period. Leading periods on the basename are ignored; splitext('.cshrc') returns ("", '.cshrc'). If slashInsensitive is true, it ignores the difference between slash and backslash.

func FilepathStem

func FilepathStem(filename string, slashInsensitive ...bool) string

FilepathStem returns the stem of filename. Example:

FilepathStem("/root/dir/sub/file.ext") // output "file"

If slashInsensitive is true, it ignores the difference between slash and backslash.

func GrepFile

func GrepFile(pattern string, filename string) result.Result[[]string]

GrepFile searches for lines matching a pattern in a file and returns a Result. Newlines are stripped while reading.

func MkdirAll

func MkdirAll(path string, perm ...os.FileMode) result.VoidResult

MkdirAll creates a directory named path, along with any necessary parents, and returns a VoidResult. The permission bits perm (before umask) are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns Ok. If perm is empty, default use 0755.

func ReplaceFile

func ReplaceFile(filename string, start, end int, newContent string) result.VoidResult

ReplaceFile replaces the bytes selected by [start, end] with the new content and returns a VoidResult.

func RewriteFile

func RewriteFile(filename string, fn func(content []byte) result.Result[[]byte]) result.VoidResult

RewriteFile rewrites the file content using the provided function and returns a VoidResult.

func RewriteToFile

func RewriteToFile(filename, newFilename string, fn func(content []byte) result.Result[[]byte]) result.VoidResult

RewriteToFile rewrites the file to a new filename using the provided function and returns a VoidResult. If newFilename already exists and is not a directory, replaces it.

func SearchFile

func SearchFile(filename string, paths ...string) result.Result[string]

SearchFile searches for a file in the given paths and returns a Result. This is often used to search for config files in /etc, ~/, etc.

func TarGz

func TarGz(src, dst string, includePrefix bool, logOutput func(string, ...interface{}), ignoreElem ...string) result.VoidResult

TarGz compresses and archives files into a tar.gz file and returns a VoidResult. src is the source file or directory to archive. dst is the destination tar.gz file path. includePrefix determines whether to include the parent directory in the archive. logOutput is an optional logging function that will be called for each file processed. ignoreElem is a list of file or directory names to ignore (e.g., ".git", ".DS_Store").

func TarGzTo

func TarGzTo(src string, dstWriter io.Writer, includePrefix bool, logOutput func(string, ...interface{}), ignoreElem ...string) result.VoidResult

TarGzTo compresses and archives files to the given writer and returns a VoidResult. src is the source file or directory to archive. dstWriter is the destination writer. includePrefix determines whether to include the parent directory in the archive. logOutput is an optional logging function that will be called for each file processed. ignoreElem is a list of file or directory names to ignore (e.g., ".git", ".DS_Store").

func WriteFile

func WriteFile(filename string, data []byte, perm ...os.FileMode) result.VoidResult

WriteFile writes data to a file, and automatically creates the directory if necessary. If perm is empty, automatically determines the file permissions based on extension.

Types

This section is empty.

Jump to

Keyboard shortcuts

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