Documentation
¶
Overview ¶
Package dir provides a series of directory/file operations
Index ¶
- 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 NormalizeDir(s string) string
- func NormalizePath(pathname string) string
- func PushDir(dirname string) (closer func())
- func PushDirEx(dirname string) (closer func(), err error)
- func RemoveDirRecursive(d string) (err error)
- func ToBool(val interface{}, defaultVal ...bool) (ret bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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 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 RemoveDirRecursive ¶
RemoveDirRecursive removes a directory and any children it contains.
Types ¶
This section is empty.