dir

package
v1.6.25 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2023 License: MIT Imports: 12 Imported by: 20

Documentation

Overview

Package dir provides a series of directory/file operations

Index

Constants

This section is empty.

Variables

View Source
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 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 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 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 ReadAll added in v1.5.53

func ReadAll(r io.Reader) ([]byte, error)

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

func ReadDir(dirname string) ([]os.DirEntry, error)

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

func ReadFile(filename string) ([]byte, error)

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

func RemoveDirRecursive(d string) (err error)

RemoveDirRecursive removes a directory and any children it contains.

func TempDir added in v1.5.53

func TempDir(dir, pattern string) (name string, err error)

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

func TempFile(dir, pattern string) (f *os.File, err error)

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 ToBool added in v1.5.3

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

ToBool translate a value to boolean

func ToBoolEx added in v1.5.55

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

ToBoolEx translate a value to boolean

func WriteFile added in v1.5.53

func WriteFile(filename string, data []byte, perm os.FileMode) error

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.

Jump to

Keyboard shortcuts

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