Documentation
¶
Overview ¶
Package fsutil provides a collection of utility functions that extend the capabilities of the Go standard filesystem (fs) package. It offers a variety of tools for working with files and directories, making it easier to perform common filesystem operations.
This package is designed to enhance the functionality of the standard library by providing additional features and abstractions. It is particularly useful for developers looking to simplify their filesystem interactions in Go applications.
Index ¶
- Constants
- func AssertDirExists(path string)
- func AssertFileExists(path string)
- func AssertPathExists(path string)
- func CopyFile(src, dst string) error
- func CreateAll(file string) (*os.File, error)
- func DirExists(path string) bool
- func FileExists(path string) bool
- func FirstExistingDir(paths ...string) (string, bool)
- func FirstExistingFile(paths ...string) (string, bool)
- func FirstExistingPath(paths ...string) (string, bool)
- func IsBinary(data []byte) bool
- func IsBinaryFast(data []byte) bool
- func IsBinarySlow(data []byte) bool
- func Must[T any](v T, err error) T
- func PathExists(path string) bool
- func ReadLines(path string) (lines []string, err error)
- func ReadString(path string) (string, error)
- func RemoveAllIfExists(path string) error
- func ReplaceForbiddenChars(name string) string
- func ReplaceForbiddenCharsWith(name string, newchar string) string
- func WriteAll(path string, data []byte, perm ...fs.FileMode) error
- func WriteLines(path string, content []string, perm ...fs.FileMode) error
- func WriteLinesWithNewlineChar(path string, content []string, newline string, perm ...fs.FileMode) error
- func WriteString(path, content string, perm ...fs.FileMode) error
Constants ¶
const ( CR = "\r" // Caret return. LF = "\n" // Line break. )
const NewLine = LF // Platform-specific newline character.
Variables ¶
This section is empty.
Functions ¶
func AssertDirExists ¶
func AssertDirExists(path string)
AssertDirExists panics if path does not exist or is not a directory.
func AssertFileExists ¶
func AssertFileExists(path string)
AssertFileExists panics if path does not exist or is not a file.
func AssertPathExists ¶
func AssertPathExists(path string)
AssertPathExists panics if path does not exist.
func CreateAll ¶
CreateAll creates file and all its parent directories. If the file exists, it is truncated.
func FileExists ¶
FileExists reports whether path exists and is a file.
func FirstExistingDir ¶ added in v0.4.0
FirstExistingDir returns the first path that exists and is a directory. The second value returned reports whether at least one path exists.
func FirstExistingFile ¶ added in v0.4.0
FirstExistingFile returns the first path that exists and is a regular file. The second value returned reports whether at least one path exists.
func FirstExistingPath ¶ added in v0.4.0
FirstExistingPath returns the first path that exists. The second value returned reports whether at least one path exists.
func IsBinary ¶ added in v0.2.0
IsBinary reports whether data is binary (non human-readable text). It checks the first and every 2^n line of the provided byte sequence (i.e. 0, 1, 2, 4, 8, ...), hence IsBinary may fail in rare cases.
If data is an empty slice or nil, false is returned.
For the most accurate determination, IsBinarySlow is recommended to be used. For the fastest determination, IsBinaryFast should be used.
func IsBinaryFast ¶ added in v0.2.0
IsBinaryFast reports whether data is binary (non human-readable text). It checks only first line of the file, hence the determination performance is the highest at a cost of accuracy.
If data is an empty slice or nil, false is returned.
For the most cases, IsBinary is recommended to be used. For the most accurate determination, IsBinarySlow is recommended to be used.
func IsBinarySlow ¶ added in v0.2.0
IsBinarySlow reports whether data is binary (non human-readable text). It checks the entire file, hence the determination accuracy is the highest at a cost of performance.
If data is an empty slice or nil, false is returned.
For the most cases, IsBinary is recommended to be used. For the fastest determination, IsBinaryFast is recommended be used.
func Must ¶
Must is a helper that wraps a call to a function returning an error and panics if the error is not nil. It is intended for use in variable initialization.
func ReadString ¶
ReadString reads path and returns its content as string.
func RemoveAllIfExists ¶
RemoveAllIfExists is like os.RemoveAll except that it returns an error if the path does not exist.
func ReplaceForbiddenChars ¶ added in v0.3.0
ReplaceForbiddenChars replaces characters forbidden in some filesystems with dash (-).
Characters replaced are:
- < (less than)
- > (greater than)
- : (colon)
- " (double quote)
- / (slash)
- \ (backslash)
- | (pipe)
- ? (question mark)
- * (asterisk)
In addition, all leading and trailing whitespace characters are trimmed.
func ReplaceForbiddenCharsWith ¶ added in v0.3.0
ReplaceForbiddenCharsWith replaces characters forbidden in some filesystems with the specified character.
Characters replaced are:
- < (less than)
- > (greater than)
- : (colon)
- " (double quote)
- / (slash)
- \ (backslash)
- | (pipe)
- ? (question mark)
- * (asterisk)
In addition, all leading and trailing whitespace characters are trimmed.
func WriteAll ¶
WriteAll writes data to path, creating all necessary parent directories. If parent directories already exist, WriteAll does nothing to them. If file already exists, it is overwritten with the given data.
Permission perm is optional and defaults to os.ModePerm.
func WriteLines ¶
WriteLines writes a slice of string as lines to a file.
WriteLines uses platform-specific linebreak character (LF on Unix, CRLF of Windows). Use WriteLinesWithNewlineChar if specific newline character is required.
Permission perm is optional and defaults to os.ModePerm.
func WriteLinesWithNewlineChar ¶
func WriteLinesWithNewlineChar(path string, content []string, newline string, perm ...fs.FileMode) error
WriteLinesWithNewlineChar is like WriteLines, but allows to set newline character used.
func WriteString ¶
WriteString writes a string content to a file.
Permission perm is optional and defaults to os.ModePerm.
Types ¶
This section is empty.