Documentation
¶
Overview ¶
Package fileutils is a collection of filename manipulation and filesystem utilities including directory traversal with symlinks support, finding file and folders with extended glob pattern, and atomic file operations.
To help support non-unix platforms, it also includes ad set of functions that are similar to those found in package "path/filepath", but but using '/' as path separator, and preserving trailing separator for directory filenames.
Index ¶
- Variables
- func Abs(path string) (string, error)
- func Base(path string) string
- func Clean(input string) string
- func Dir(path string) string
- func EvalSymlinks(path string) (string, error)
- func Exists(path string) bool
- func ExpandPath(input string) (output string, err error)
- func ExpandPathRelative(input, basepath string) (output string, err error)
- func Ext(path string) string
- func IsAbs(path string) bool
- func IsDir(path string) bool
- func IsDirectoryName(path string) bool
- func IsFile(path string) bool
- func IsPathSeparator(c uint8) bool
- func Join(elem ...string) string
- func OpenTemp(filename, suffix string) (f *os.File, err error)
- func ReadFile(filename string, reader func(r io.Reader) error) error
- func Rel(basepath, targetpath string) (string, error)
- func RewriteFilename(input string, opts *RewriteOpts) string
- func Split(path string) (dir, base string)
- func ToNative(path string) string
- func ToSlash(path string) string
- func Touch(filenames ...string) (err error)
- func VolumeName(path string) string
- func WriteFile(filename string, writer func(w io.Writer) error) error
- type ReaderFunc
- type RewriteOpts
- type WalkFunc
- type WriterFunc
Constants ¶
This section is empty.
Variables ¶
var OSSeparator = os.PathSeparator
OSSeparator is the os specific path separator, usually either '/' on unix latforms or '\\` on windows.
var Separator = '/'
Separator is a fixed default path separator and is always '/'
Functions ¶
func Abs ¶ added in v0.6.1
Abs is equivalent to `filepath.Abs()`, but preserves any trailing path separator.
func Clean ¶
Clean returns a lexically equivalent path, using '/' as separator, removing any discardable '/' or "./", and collapsing any intermediate "../". It preserves a trailing separator for directory names.
func EvalSymlinks ¶ added in v0.6.1
EvalSymlinks is equivalent to `filepath.EvalSymlinks()`, but preserves any trailing path separator.
func ExpandPath ¶
ExpandPath is similar to ExpandPathRelative with an empty `basepath`; relative paths are expanded relative to `$(pwd)`.
func ExpandPathRelative ¶
ExpandPathRelative returns the absolute path for the given input, expanding environment variable and handling the special case `~/` referring to the current user home directory. If the resulting path after variable expansion is relative, it is expanded relative to `basepath`. If the resulting path is still relative, it is expanded relative to `$(pwd)`. The function returns and error if one of the underlying calls fails (getting user home or process working directory path).
func IsDirectoryName ¶ added in v0.6.1
IsDirectoryName returns true is the input can be inferred to be a directory based on its name only, without having to access the filesystem to check. This include paths with trailing separator, an empty path that resolved to "./", or paths the end with either a "." or ".." path fragment that are always directories.
func IsPathSeparator ¶ added in v0.6.1
IsPathSeparator returns true for runes that are either the default path separator or the os native path separator.
func Join ¶
Join joins multiple path fragments into a single path, preserving a trailing separator if any, and handling any intermediate absolute path as the new root of the resulting path.
func OpenTemp ¶
OpenTemp returns the name and handle to a newly created temporary file, guarantied to not previously exist, located in the same directory as the specified file.
func ReadFile ¶
ReadFile opens a file for reading ans passes if to the provided reader for loading. The file is closed when the function returns
func RewriteFilename ¶
func RewriteFilename(input string, opts *RewriteOpts) string
RewriteFilename transforms a filename according the the specified options and can change dirname, basename, extension or append / prepend a fragment to the basename.
func Split ¶ added in v0.6.1
Split splits the last path fragment of `path` from everything that precedes, so that path = dir+file.
func ToSlash ¶ added in v0.6.1
ToSlash converts path into a path using '/' as path separator, consistent with other functions in this package.
func Touch ¶
Touch create a new files or update files mtime at the specified locations. If the destination directory does not exist, it is created as well. An errors is returned if any destination directory does not exist and cannot be created, or any of the specified files cannot be opened.
func VolumeName ¶ added in v0.6.1
VolumeName returns the name of the volume if specified in path. The result is always empty on unix platforms.
Types ¶
type ReaderFunc ¶
ReaderFunc is a function type that implements io.Reader
type RewriteOpts ¶
type RewriteOpts struct { Dirname string // Replace the path with the specified dirname Extname string // Replace the file extension with the specified extname Prefix string // Prefix to prepend on the basename Suffix string // Suffix to append on the basename }
RewriteOpts contains the options to apply to RewriteFilename to transform the input filename
type WalkFunc ¶ added in v0.6.1
type WalkFunc = fs.WalkDirFunc
WalkFunc is the type of the function called by Walk to visit each file or directory. Since all walk functions in this package are based on go 1.16 WalkDir(), it uses `fs.DirEntry` as second argument to capture the file information.