fsutil

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ExDotFile exDotFlag = 1
	ExDotDir  exDotFlag = 2
)
View Source
const (
	// MimeSniffLen sniff Length, use for detect file mime type
	MimeSniffLen = 512
)

Variables

View Source
var (
	// perm and flags for create log file
	DefaultDirPerm  os.FileMode = 0775
	DefaultFilePerm os.FileMode = 0665

	DefaultFileFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND
)
View Source
var (
	DirExist  = IsDir
	FileExist = IsFile
	PathExist = PathExists
)

alias methods

View Source
var (
	MustRm  = MustRemove
	QuietRm = QuietRemove
)

alias methods

View Source
var ImageMimeTypes = map[string]string{
	"bmp": "image/bmp",
	"gif": "image/gif",
	"ief": "image/ief",
	"jpg": "image/jpeg",

	"jpeg": "image/jpeg",
	"png":  "image/png",
	"svg":  "image/svg+xml",
	"ico":  "image/x-icon",
	"webp": "image/webp",
}

ImageMimeTypes refer net/http package

Functions

func CopyFile added in v0.4.1

func CopyFile(src string, dst string) error

CopyFile copy file to another path.

func CreateFile added in v0.4.1

func CreateFile(fpath string, filePerm, dirPerm os.FileMode) (*os.File, error)

CreateFile create file if not exists Usage:

CreateFile("path/to/file.txt", 0664, 0666)

func DeleteIfExist added in v0.4.1

func DeleteIfExist(fpath string) error

DeleteIfExist removes the named file or (empty) directory on exists.

func DeleteIfFileExist added in v0.4.1

func DeleteIfFileExist(fpath string) error

DeleteIfFileExist removes the named file on exists.

func Dir added in v0.4.1

func Dir(fpath string) string

Dir get dir path, without last name.

func ExpandPath added in v0.4.1

func ExpandPath(path string) string

ExpandPath will parse `~` as user home dir path.

func FileExists

func FileExists(path string) bool

FileExists reports whether the named file or directory exists.

func FileExt added in v0.4.1

func FileExt(fpath string) string

FileExt get filename ext. alias of path.Ext()

func IsAbsPath

func IsAbsPath(aPath string) bool

IsAbsPath is abs path.

func IsDir added in v0.4.1

func IsDir(path string) bool

IsDir reports whether the named directory exists.

func IsFile added in v0.4.1

func IsFile(path string) bool

IsFile reports whether the named file or directory exists.

func IsImageFile added in v0.4.1

func IsImageFile(path string) bool

IsImageFile check file is image file.

func IsZipFile

func IsZipFile(filepath string) bool

IsZipFile check is zip file. from https://blog.csdn.net/wangshubo1989/article/details/71743374

func MimeType added in v0.4.1

func MimeType(path string) (mime string)

MimeType get File Mime Type name. eg "image/png"

func MkParentDir added in v0.4.1

func MkParentDir(fpath string) error

MkParentDir quick create parent dir

func Mkdir added in v0.4.1

func Mkdir(dirPath string, perm os.FileMode) error

Mkdir alias of os.MkdirAll()

func MustCopyFile added in v0.4.1

func MustCopyFile(src string, dst string)

MustCopyFile copy file to another path.

func MustCreateFile added in v0.4.1

func MustCreateFile(filePath string, filePerm, dirPerm os.FileMode) *os.File

MustCreateFile create file, will panic on error

func MustReadFile added in v0.4.1

func MustReadFile(filePath string) []byte

MustReadFile read file contents, will panic on error

func MustRemove added in v0.4.1

func MustRemove(fpath string)

MustRemove removes the named file or (empty) directory. NOTICE: if error will panic

func Name added in v0.4.1

func Name(fpath string) string

Name get file/dir name

func OSTempDir added in v0.4.1

func OSTempDir(pattern string) (string, error)

OSTempDir creates a new temp dir on os.TempDir and return the temp dir path Usage:

fsutil.OSTempDir("example.*.txt")

func OSTempFile added in v0.4.1

func OSTempFile(pattern string) (*os.File, error)

OSTempFile create an temp file on os.TempDir() Usage:

fsutil.OSTempFile("example.*.txt")

func OpenFile added in v0.4.1

func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error)

OpenFile like os.OpenFile, but will auto create dir.

func PathExists added in v0.4.1

func PathExists(path string) bool

PathExists reports whether the named file or directory exists.

func QuickOpenFile added in v0.4.1

func QuickOpenFile(filepath string) (*os.File, error)

QuickOpenFile like os.OpenFile

func QuietRemove added in v0.4.1

func QuietRemove(fpath string)

QuietRemove removes the named file or (empty) directory. NOTICE: will ignore error

func ReadExistFile added in v0.4.1

func ReadExistFile(filePath string) []byte

ReadExistFile read file contents if exist, will panic on error

func ReadString added in v0.4.1

func ReadString(filename string) (string, error)

func ReaderMimeType added in v0.4.1

func ReaderMimeType(r io.Reader) (mime string)

ReaderMimeType get the io.Reader mimeType Usage:

file, err := os.Open(filepath)
if err != nil {
	return
}
mime := ReaderMimeType(file)

func Realpath added in v0.4.1

func Realpath(path string) string

Realpath parse and get

func Suffix added in v0.4.1

func Suffix(fpath string) string

Suffix get filename ext. alias of path.Ext()

func TempDir added in v0.4.1

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

TempDir creates a new temp dir and return the temp dir path Usage:

fsutil.TempDir("", "example.*.txt")

func TempFile added in v0.4.1

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

TempFile is alias of ioutil.TempFile Usage:

fsutil.TempFile("", "example.*.txt")

func Unzip

func Unzip(archive, targetDir string) (err error)

Unzip a zip archive from https://blog.csdn.net/wangshubo1989/article/details/71743374

Types

type DirFilter added in v0.4.1

type DirFilter interface {
	FilterDir(dirPath, dirName string) bool
}

DirFilter for filter dir path.

type DirFilterFunc added in v0.4.1

type DirFilterFunc func(dirPath, dirName string) bool

DirFilterFunc for filter file path.

func DirNameFilterFunc added in v0.4.1

func DirNameFilterFunc(names []string, include bool) DirFilterFunc

DirNameFilterFunc filter filepath by given dir names.

func DotDirFilterFunc added in v0.4.1

func DotDirFilterFunc(include bool) DirFilterFunc

DotDirFilterFunc filter dot dirname. eg: ".idea"

func (DirFilterFunc) FilterDir added in v0.4.1

func (fn DirFilterFunc) FilterDir(dirPath, dirName string) bool

FilterDir Filter for filter file path.

type FileFilter added in v0.4.1

type FileFilter interface {
	FilterFile(filePath, filename string) bool
}

FileFilter for filter file path.

type FileFilterFunc added in v0.4.1

type FileFilterFunc func(filePath, filename string) bool

FileFilterFunc for filter file path.

func DotFileFilterFunc added in v0.4.1

func DotFileFilterFunc(include bool) FileFilterFunc

DotFileFilterFunc filter dot filename. eg: ".gitignore"

func ExtFilterFunc added in v0.4.1

func ExtFilterFunc(exts []string, include bool) FileFilterFunc

ExtFilterFunc filter filepath by given file ext.

Usage:

f := EmptyFiler()
f.AddFilter(ExtFilterFunc([]string{".go", ".md"}, true))
f.AddFilter(ExtFilterFunc([]string{".log", ".tmp"}, false))

func GlobFilterFunc added in v0.4.1

func GlobFilterFunc(patterns []string, include bool) FileFilterFunc

GlobFilterFunc filter filepath by given patterns.

Usage:

f := EmptyFiler()
f.AddFilter(GlobFilterFunc([]string{"*_test.go"}, true))

func ModTimeFilterFunc added in v0.4.1

func ModTimeFilterFunc(limitSec int, op rune, include bool) FileFilterFunc

ModTimeFilterFunc filter file by modify time.

func PathNameFilterFunc added in v0.4.1

func PathNameFilterFunc(names []string, include bool) FileFilterFunc

PathNameFilterFunc filter filepath by given path names.

func RegexFilterFunc added in v0.4.1

func RegexFilterFunc(pattern string, include bool) FileFilterFunc

RegexFilterFunc filter filepath by given regex pattern

Usage:

f := EmptyFiler()
f.AddFilter(RegexFilterFunc(`[A-Z]\w+`, true))

func SuffixFilterFunc added in v0.4.1

func SuffixFilterFunc(suffixes []string, include bool) FileFilterFunc

SuffixFilterFunc filter filepath by given file ext.

Usage:

f := EmptyFiler()
f.AddFilter(SuffixFilterFunc([]string{"util.go", ".md"}, true))
f.AddFilter(SuffixFilterFunc([]string{"_test.go", ".log"}, false))

func (FileFilterFunc) FilterFile added in v0.4.1

func (fn FileFilterFunc) FilterFile(filePath, filename string) bool

FilterFile Filter for filter file path.

type FileFinder added in v0.4.1

type FileFinder struct {
	// contains filtered or unexported fields
}

FileFinder struct

func EmptyFinder added in v0.4.1

func EmptyFinder() *FileFinder

EmptyFinder new empty FileFinder instance

func NewFinder added in v0.4.1

func NewFinder(dirPaths []string, filePaths ...string) *FileFinder

NewFinder new instance with source dir paths.

func (*FileFinder) AddDir added in v0.4.1

func (f *FileFinder) AddDir(dirPaths ...string) *FileFinder

AddDir add source dir for find. alias of AddDirPath()

func (*FileFinder) AddDirFilter added in v0.4.1

func (f *FileFinder) AddDirFilter(filterFuncs ...DirFilter) *FileFinder

AddDirFilter for filter file contents

func (*FileFinder) AddDirPath added in v0.4.1

func (f *FileFinder) AddDirPath(dirPaths ...string) *FileFinder

AddDirPath add source dir for find

func (*FileFinder) AddFile added in v0.4.1

func (f *FileFinder) AddFile(filePaths ...string) *FileFinder

AddFile add source file. alias of AddFilePath()

func (*FileFinder) AddFileFilter added in v0.4.1

func (f *FileFinder) AddFileFilter(filterFuncs ...FileFilter) *FileFinder

AddFileFilter for filter filepath

func (*FileFinder) AddFilePath added in v0.4.1

func (f *FileFinder) AddFilePath(filePaths ...string) *FileFinder

AddFilePath add source file

func (*FileFinder) AddFilePaths added in v0.4.1

func (f *FileFinder) AddFilePaths(filePaths []string)

AddFilePaths set founded files

func (*FileFinder) AddFilter added in v0.4.1

func (f *FileFinder) AddFilter(filterFuncs ...interface{}) *FileFinder

AddFilter for filter filepath or dirpath

func (*FileFinder) Each added in v0.4.1

func (f *FileFinder) Each(fn func(filePath string))

Each each file paths.

func (*FileFinder) EachContents added in v0.4.1

func (f *FileFinder) EachContents(fn func(contents, filePath string))

EachContents each file contents

func (*FileFinder) EachFile added in v0.4.1

func (f *FileFinder) EachFile(fn func(file *os.File))

EachFile each file os.File

func (*FileFinder) EachStat added in v0.4.1

func (f *FileFinder) EachStat(fn func(fi os.FileInfo, filePath string))

EachStat each file os.FileInfo

func (*FileFinder) ExcludeDir added in v0.4.1

func (f *FileFinder) ExcludeDir(dirs ...string) *FileFinder

ExcludeDir exclude dir names.

func (*FileFinder) ExcludeDotDir added in v0.4.1

func (f *FileFinder) ExcludeDotDir(exclude ...bool) *FileFinder

ExcludeDotDir exclude dot dir names. eg: ".idea"

func (*FileFinder) ExcludeDotFile added in v0.4.1

func (f *FileFinder) ExcludeDotFile(exclude ...bool) *FileFinder

ExcludeDotFile exclude dot dir names. eg: ".gitignore"

func (*FileFinder) ExcludeName added in v0.4.1

func (f *FileFinder) ExcludeName(files ...string) *FileFinder

ExcludeName exclude file names.

func (*FileFinder) Find added in v0.4.1

func (f *FileFinder) Find() *FileFinder

Find find file paths.

func (*FileFinder) FindAll added in v0.4.1

func (f *FileFinder) FindAll() []string

FindAll find and return founded file paths.

func (*FileFinder) NoDotDir added in v0.4.1

func (f *FileFinder) NoDotDir(exclude ...bool) *FileFinder

NoDotDir exclude dot dir names. alias of ExcludeDotDir().

func (*FileFinder) NoDotFile added in v0.4.1

func (f *FileFinder) NoDotFile(exclude ...bool) *FileFinder

NoDotFile exclude dot dir names. alias of ExcludeDotFile().

func (*FileFinder) Reset added in v0.4.1

func (f *FileFinder) Reset()

Reset data setting.

func (*FileFinder) String added in v0.4.1

func (f *FileFinder) String() string

String all file paths

func (*FileFinder) WithDirFilter added in v0.4.1

func (f *FileFinder) WithDirFilter(filterFuncs ...DirFilter) *FileFinder

WithDirFilter for filter func for filtering file contents

func (*FileFinder) WithFileFilter added in v0.4.1

func (f *FileFinder) WithFileFilter(filterFuncs ...FileFilter) *FileFinder

WithFileFilter for filter func for filtering filepath

func (*FileFinder) WithFilter added in v0.4.1

func (f *FileFinder) WithFilter(filterFuncs ...interface{}) *FileFinder

WithFilter add filter func for filtering filepath or dirpath

type FileMeta added in v0.4.1

type FileMeta struct {
	// contains filtered or unexported fields
}

FileMeta struct

type FilterFunc added in v0.4.1

type FilterFunc func(filePath, filename string) bool

FilterFunc for filter file path.

func (FilterFunc) Filter added in v0.4.1

func (fn FilterFunc) Filter(filePath, filename string) bool

Filter for filter file path.

type FindResults added in v0.4.1

type FindResults struct {
	// contains filtered or unexported fields
}

FindResults struct

func (*FindResults) AddFilters added in v0.4.1

func (r *FindResults) AddFilters(filterFuncs ...FileFilter) *FindResults

AddFilters Result get find paths

func (*FindResults) Each added in v0.4.1

func (r *FindResults) Each() *FindResults

Each Result get find paths

func (*FindResults) Filter added in v0.4.1

func (r *FindResults) Filter() *FindResults

Filter Result get find paths

func (*FindResults) Result added in v0.4.1

func (r *FindResults) Result() []string

Result get find paths

Jump to

Keyboard shortcuts

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