Documentation
¶
Overview ¶
Package dir provides a series of directory/file operations
Index ¶
- Variables
- func AbsPath(pathname string) string
- func CopyFile(src, dst string) (err error)
- func CopyFileByLinkFirst(src, dst string) (err error)
- func DeleteFile(dst string) (err error)
- func EnsureDir(d string) (err error)
- func EnsureDirEnh(d string) (err error)
- func FileAccessedTime(fileInfo os.FileInfo) (tm time.Time)
- func FileCreatedTime(fileInfo os.FileInfo) (tm time.Time)
- func FileExists(filepath string) bool
- func FileModeIs(filepath string, tester func(mode os.FileMode) bool) (ret bool)
- func FileModifiedTime(fileInfo os.FileInfo) (tm time.Time)
- func FollowSymLink(pathname string) string
- func ForDir(root string, ...) (err error)
- func ForDirMax(root string, initialDepth int, maxDepth int, ...) (err error)
- func ForFile(root string, ...) (err error)
- func ForFileMax(root string, initialDepth, maxDepth int, ...) (err error)
- func GetCurrentDir() string
- func GetExecutableDir() string
- func GetExecutablePath() string
- func IsDirectory(filepath string) (bool, error)
- func IsModeCharDevice(mode os.FileMode) bool
- func IsModeDevice(mode os.FileMode) bool
- func IsModeDirectory(mode os.FileMode) bool
- func IsModeExecAll(mode os.FileMode) bool
- func IsModeExecAny(mode os.FileMode) bool
- func IsModeExecGroup(mode os.FileMode) bool
- func IsModeExecOther(mode os.FileMode) bool
- func IsModeExecOwner(mode os.FileMode) bool
- func IsModeIrregular(mode os.FileMode) bool
- func IsModeNamedPipe(mode os.FileMode) bool
- func IsModeReadAll(mode os.FileMode) bool
- func IsModeReadAny(mode os.FileMode) bool
- func IsModeReadGroup(mode os.FileMode) bool
- func IsModeReadOther(mode os.FileMode) bool
- func IsModeReadOwner(mode os.FileMode) bool
- func IsModeRegular(mode os.FileMode) bool
- func IsModeSetgid(mode os.FileMode) bool
- func IsModeSetuid(mode os.FileMode) bool
- func IsModeSocket(mode os.FileMode) bool
- func IsModeSticky(mode os.FileMode) bool
- func IsModeSymbolicLink(mode os.FileMode) bool
- func IsModeWriteAll(mode os.FileMode) bool
- func IsModeWriteAny(mode os.FileMode) bool
- func IsModeWriteGroup(mode os.FileMode) bool
- func IsModeWriteOther(mode os.FileMode) bool
- func IsModeWriteOwner(mode os.FileMode) bool
- func IsRegularFile(filepath string) (bool, error)
- func IsWildMatch(s string, p string) bool
- func NopCloser(r io.Reader) io.ReadCloser
- func NormalizeDir(s string) string
- func NormalizePath(pathname string) string
- func PushDir(dirname string) (closer func())
- func PushDirEx(dirname string) (closer func(), err error)
- func ReadAll(r io.Reader) ([]byte, error)
- func ReadDir(dirname string) ([]os.DirEntry, error)
- func ReadFile(filename string) ([]byte, error)
- func RemoveDirRecursive(d string) (err error)
- func TempDir(dir, pattern string) (name string, err error)
- func TempFile(dir, pattern string) (f *os.File, err error)
- func ToBool(val interface{}, defaultVal ...bool) (ret bool)
- func ToBoolEx(val interface{}, defaultVal ...bool) (ret, parsed bool)
- func WriteFile(filename string, data []byte, perm os.FileMode) error
Constants ¶
This section is empty.
Variables ¶
var Discard io.Writer = io.Discard
Discard is an io.Writer on which all Write calls succeed without doing anything.
As of Go 1.16, this value is simply io.Discard.
Functions ¶
func CopyFileByLinkFirst ¶
CopyFileByLinkFirst copies a file from src to dst. If src and dst files exist, and are the same, then return success. Otherwise, attempt to create a hard link between the two files. If that fail, copy the file contents from src to dst.
func EnsureDirEnh ¶
EnsureDirEnh checks and creates the directory, via sudo if necessary.
func FileAccessedTime ¶ added in v0.3.15
FileAccessedTime return the creation time of a file
func FileCreatedTime ¶ added in v0.3.15
FileCreatedTime return the creation time of a file
func FileExists ¶
FileExists returns the existence of an directory or file
func FileModeIs ¶
FileModeIs tests the mode of 'filepath' with 'tester'. Examples:
var yes = exec.FileModeIs("/etc/passwd", exec.IsModeExecAny) var yes = exec.FileModeIs("/etc/passwd", exec.IsModeDirectory)
func FileModifiedTime ¶ added in v0.3.15
FileModifiedTime return the creation time of a file
func FollowSymLink ¶ added in v1.5.3
func ForDir ¶
func ForDir( root string, cb func(depth int, dirname string, fi os.FileInfo) (stop bool, err error), excludes ...string, ) (err error)
ForDir walks on `root` directory and its children
func ForDirMax ¶
func ForDirMax( root string, initialDepth int, maxDepth int, cb func(depth int, dirname string, fi os.FileInfo) (stop bool, err error), excludes ...string, ) (err error)
ForDirMax walks on `root` directory and its children with nested levels up to `maxLength`.
Example - discover folder just one level
_ = ForDirMax(dir, 0, 1, func(depth int, dirname string, fi os.FileInfo) (stop bool, err error) { if fi.IsDir() { return } // ... doing something for a file, return })
maxDepth = -1: no limit. initialDepth: 0 if no idea.
func ForFile ¶
func ForFile( root string, cb func(depth int, dirname string, fi os.FileInfo) (stop bool, err error), excludes ...string, ) (err error)
ForFile walks on `root` directory and its children
func ForFileMax ¶
func ForFileMax( root string, initialDepth, maxDepth int, cb func(depth int, dirname string, fi os.FileInfo) (stop bool, err error), excludes ...string, ) (err error)
ForFileMax walks on `root` directory and its children with nested levels up to `maxLength`.
Example - discover folder just one level
_ = ForFileMax(dir, 0, 1, func(depth int, dirname string, fi os.FileInfo) (stop bool, err error) { if fi.IsDir() { return } // ... doing something for a file, return })
maxDepth = -1: no limit. initialDepth: 0 if no idea.
Known issue: can't walk at ~/.local/share/NuGet/v3-cache/1ca707a4d90792ce8e42453d4e350886a0fdaa4d:_api.nuget.org_v3_index.json. workaround: use filepath.Walk
func GetCurrentDir ¶
func GetCurrentDir() string
GetCurrentDir returns the current workingFlag directory it should be equal with os.Getenv("PWD")
func GetExecutableDir ¶
func GetExecutableDir() string
GetExecutableDir returns the executable file directory
func GetExecutablePath ¶
func GetExecutablePath() string
GetExecutablePath returns the executable file path
func IsDirectory ¶
IsDirectory tests whether `path` is a directory or not
func IsModeCharDevice ¶
IsModeCharDevice give the result of whether a file is a character device
func IsModeDevice ¶
IsModeDevice give the result of whether a file is a device
func IsModeDirectory ¶
IsModeDirectory give the result of whether a file is a directory
func IsModeExecAll ¶
IsModeExecAll give the result of whether a file can be invoked by all users
func IsModeExecAny ¶
IsModeExecAny give the result of whether a file can be invoked by anyone
func IsModeExecGroup ¶
IsModeExecGroup give the result of whether a file can be invoked by its unix-group
func IsModeExecOther ¶
IsModeExecOther give the result of whether a file can be invoked by its unix-all
func IsModeExecOwner ¶
IsModeExecOwner give the result of whether a file can be invoked by its unix-owner
func IsModeIrregular ¶
IsModeIrregular give the result of whether a file is a non-regular file; nothing else is known about this file
func IsModeNamedPipe ¶
IsModeNamedPipe give the result of whether a file is a named pipe
func IsModeReadAll ¶
IsModeReadAll give the result of whether a file can be read by all users
func IsModeReadAny ¶
IsModeReadAny give the result of whether a file can be read by anyone
func IsModeReadGroup ¶
IsModeReadGroup give the result of whether a file can be read by its unix-group
func IsModeReadOther ¶
IsModeReadOther give the result of whether a file can be read by its unix-all
func IsModeReadOwner ¶
IsModeReadOwner give the result of whether a file can be read by its unix-owner
func IsModeRegular ¶
IsModeRegular give the result of whether a file is a regular file
func IsModeSetgid ¶
IsModeSetgid give the result of whether a file has the setgid bit
func IsModeSetuid ¶
IsModeSetuid give the result of whether a file has the setuid bit
func IsModeSocket ¶
IsModeSocket give the result of whether a file is a socket file
func IsModeSticky ¶
IsModeSticky give the result of whether a file is a sticky file
func IsModeSymbolicLink ¶
IsModeSymbolicLink give the result of whether a file is a symbolic link
func IsModeWriteAll ¶
IsModeWriteAll give the result of whether a file can be written by all users
func IsModeWriteAny ¶
IsModeWriteAny give the result of whether a file can be written by anyone
func IsModeWriteGroup ¶
IsModeWriteGroup give the result of whether a file can be written by its unix-group
func IsModeWriteOther ¶
IsModeWriteOther give the result of whether a file can be written by its unix-all
func IsModeWriteOwner ¶
IsModeWriteOwner give the result of whether a file can be written by its unix-owner
func IsRegularFile ¶
IsRegularFile tests whether `path` is a normal regular file or not
func IsWildMatch ¶ added in v1.5.3
IsWildMatch provides a wild-matching ('*' and '?') test.
For examples:
output := IsWildMatch("aa", "aa") expectTrue(t, output) output = IsWildMatch("aaaa", "*") expectTrue(t, output) output = IsWildMatch("ab", "a?") expectTrue(t, output) output = IsWildMatch("adceb", "*a*b") expectTrue(t, output) output = IsWildMatch("aa", "a") expectFalse(t, output) output = IsWildMatch("mississippi", "m??*ss*?i*pi") expectFalse(t, output) output = IsWildMatch("acdcb", "a*c?b") expectFalse(t, output)
func NopCloser ¶ added in v1.5.53
func NopCloser(r io.Reader) io.ReadCloser
NopCloser returns a ReadCloser with a no-op Close method wrapping the provided Reader r.
As of Go 1.16, this function simply calls io.NopCloser.
func NormalizePath ¶
NormalizePath cleans up the given pathname
func PushDir ¶ added in v1.5.3
func PushDir(dirname string) (closer func())
PushDir provides a shortcut to enter a folder and restore at the end of your current function scope. PushDir returns a functor and assumes you will DEFER call it.
For example:
func TestSth() { defer dir.PushDir("/your/working/dir")() // do sth under '/your/working/dir' ... }
BEWARE DON'T miss the ending brakets for defer call. NOTE that current directory would not be changed if chdir(dirname) failed,
func PushDirEx ¶ added in v1.5.13
PushDirEx provides a shortcut to enter a folder and restore at the end of your current function scope.
func ReadAll ¶ added in v1.5.53
ReadAll reads from r until an error or EOF and returns the data it read. A successful call returns err == nil, not err == EOF. Because ReadAll is defined to read from src until EOF, it does not treat an EOF from Read as an error to be reported.
As of Go 1.16, this function simply calls io.ReadAll.
func ReadDir ¶ added in v1.5.53
ReadDir reads the directory named by dirname and returns a list of fs.FileInfo for the directory's contents, sorted by filename. If an error occurs reading the directory, ReadDir returns no directory entries along with the error.
As of Go 1.16, os.ReadDir is a more efficient and correct choice: it returns a list of fs.DirEntry instead of fs.FileInfo, and it returns partial results in the case of an error midway through reading a directory.
func ReadFile ¶ added in v1.5.53
ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.
As of Go 1.16, this function simply calls os.ReadFile.
func RemoveDirRecursive ¶
RemoveDirRecursive removes a directory and any children it contains.
func TempDir ¶ added in v1.5.53
TempDir creates a new temporary directory in the directory dir. The directory name is generated by taking pattern and applying a random string to the end. If pattern includes a "*", the random string replaces the last "*". TempDir returns the name of the new directory. If dir is the empty string, TempDir uses the default directory for temporary files (see os.TempDir). Multiple programs calling TempDir simultaneously will not choose the same directory. It is the caller's responsibility to remove the directory when no longer needed.
As of Go 1.17, this function simply calls os.MkdirTemp.
func TempFile ¶ added in v1.5.53
TempFile creates a new temporary file in the directory dir, opens the file for reading and writing, and returns the resulting *os.File. The filename is generated by taking pattern and adding a random string to the end. If pattern includes a "*", the random string replaces the last "*". If dir is the empty string, TempFile uses the default directory for temporary files (see os.TempDir). Multiple programs calling TempFile simultaneously will not choose the same file. The caller can use f.Name() to find the pathname of the file. It is the caller's responsibility to remove the file when no longer needed.
As of Go 1.17, this function simply calls os.CreateTemp.
func WriteFile ¶ added in v1.5.53
WriteFile writes data to a file named by filename. If the file does not exist, WriteFile creates it with permissions perm (before umask); otherwise WriteFile truncates it before writing, without changing permissions.
As of Go 1.16, this function simply calls os.WriteFile.
Types ¶
This section is empty.