dir

package
v1.5.50 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2022 License: MIT Imports: 13 Imported by: 20

Documentation

Overview

Package dir provides a series of directory/file operations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsPath

func AbsPath(pathname string) string

AbsPath returns a clean, normalized and absolute path string for the given pathname.

func CopyFile

func CopyFile(src, dst string) (err error)

CopyFile will make a content clone of src.

func CopyFileByLinkFirst

func CopyFileByLinkFirst(src, dst string) (err error)

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 DeleteFile

func DeleteFile(dst string) (err error)

DeleteFile deletes a file if exists

func EnsureDir

func EnsureDir(d string) (err error)

EnsureDir checks and creates the directory.

func EnsureDirEnh

func EnsureDirEnh(d string) (err error)

EnsureDirEnh checks and creates the directory, via sudo if necessary.

func FileAccessedTime added in v0.3.15

func FileAccessedTime(fileInfo os.FileInfo) (tm time.Time)

FileAccessedTime return the creation time of a file

func FileCreatedTime added in v0.3.15

func FileCreatedTime(fileInfo os.FileInfo) (tm time.Time)

FileCreatedTime return the creation time of a file

func FileExists

func FileExists(filepath string) bool

FileExists returns the existence of an directory or file

func FileModeIs

func FileModeIs(filepath string, tester func(mode os.FileMode) bool) (ret bool)

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

func FileModifiedTime(fileInfo os.FileInfo) (tm time.Time)

FileModifiedTime return the creation time of a file

func FollowSymLink(pathname string) string

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

func IsDirectory(filepath string) (bool, error)

IsDirectory tests whether `path` is a directory or not

func IsModeCharDevice

func IsModeCharDevice(mode os.FileMode) bool

IsModeCharDevice give the result of whether a file is a character device

func IsModeDevice

func IsModeDevice(mode os.FileMode) bool

IsModeDevice give the result of whether a file is a device

func IsModeDirectory

func IsModeDirectory(mode os.FileMode) bool

IsModeDirectory give the result of whether a file is a directory

func IsModeExecAll

func IsModeExecAll(mode os.FileMode) bool

IsModeExecAll give the result of whether a file can be invoked by all users

func IsModeExecAny

func IsModeExecAny(mode os.FileMode) bool

IsModeExecAny give the result of whether a file can be invoked by anyone

func IsModeExecGroup

func IsModeExecGroup(mode os.FileMode) bool

IsModeExecGroup give the result of whether a file can be invoked by its unix-group

func IsModeExecOther

func IsModeExecOther(mode os.FileMode) bool

IsModeExecOther give the result of whether a file can be invoked by its unix-all

func IsModeExecOwner

func IsModeExecOwner(mode os.FileMode) bool

IsModeExecOwner give the result of whether a file can be invoked by its unix-owner

func IsModeIrregular

func IsModeIrregular(mode os.FileMode) bool

IsModeIrregular give the result of whether a file is a non-regular file; nothing else is known about this file

func IsModeNamedPipe

func IsModeNamedPipe(mode os.FileMode) bool

IsModeNamedPipe give the result of whether a file is a named pipe

func IsModeReadAll

func IsModeReadAll(mode os.FileMode) bool

IsModeReadAll give the result of whether a file can be read by all users

func IsModeReadAny

func IsModeReadAny(mode os.FileMode) bool

IsModeReadAny give the result of whether a file can be read by anyone

func IsModeReadGroup

func IsModeReadGroup(mode os.FileMode) bool

IsModeReadGroup give the result of whether a file can be read by its unix-group

func IsModeReadOther

func IsModeReadOther(mode os.FileMode) bool

IsModeReadOther give the result of whether a file can be read by its unix-all

func IsModeReadOwner

func IsModeReadOwner(mode os.FileMode) bool

IsModeReadOwner give the result of whether a file can be read by its unix-owner

func IsModeRegular

func IsModeRegular(mode os.FileMode) bool

IsModeRegular give the result of whether a file is a regular file

func IsModeSetgid

func IsModeSetgid(mode os.FileMode) bool

IsModeSetgid give the result of whether a file has the setgid bit

func IsModeSetuid

func IsModeSetuid(mode os.FileMode) bool

IsModeSetuid give the result of whether a file has the setuid bit

func IsModeSocket

func IsModeSocket(mode os.FileMode) bool

IsModeSocket give the result of whether a file is a socket file

func IsModeSticky

func IsModeSticky(mode os.FileMode) bool

IsModeSticky give the result of whether a file is a sticky file

func IsModeSymbolicLink(mode os.FileMode) bool

IsModeSymbolicLink give the result of whether a file is a symbolic link

func IsModeWriteAll

func IsModeWriteAll(mode os.FileMode) bool

IsModeWriteAll give the result of whether a file can be written by all users

func IsModeWriteAny

func IsModeWriteAny(mode os.FileMode) bool

IsModeWriteAny give the result of whether a file can be written by anyone

func IsModeWriteGroup

func IsModeWriteGroup(mode os.FileMode) bool

IsModeWriteGroup give the result of whether a file can be written by its unix-group

func IsModeWriteOther

func IsModeWriteOther(mode os.FileMode) bool

IsModeWriteOther give the result of whether a file can be written by its unix-all

func IsModeWriteOwner

func IsModeWriteOwner(mode os.FileMode) bool

IsModeWriteOwner give the result of whether a file can be written by its unix-owner

func IsRegularFile

func IsRegularFile(filepath string) (bool, error)

IsRegularFile tests whether `path` is a normal regular file or not

func IsWildMatch added in v1.5.3

func IsWildMatch(s string, p string) bool

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 NormalizeDir

func NormalizeDir(s string) string

NormalizeDir make dir name normalized

func NormalizePath

func NormalizePath(pathname string) string

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

func PushDirEx(dirname string) (closer func(), err error)

PushDirEx provides a shortcut to enter a folder and restore at the end of your current function scope.

func RemoveDirRecursive

func RemoveDirRecursive(d string) (err error)

RemoveDirRecursive removes a directory and any children it contains.

func ToBool added in v1.5.3

func ToBool(val interface{}, defaultVal ...bool) (ret bool)

ToBool translate a value to boolean

Types

This section is empty.

Jump to

Keyboard shortcuts

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