fsutil

package
v0.0.0-...-f5003fd Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: MIT Imports: 18 Imported by: 0

README

FileSystem Util

fsutil Provide some commonly file system util functions.

Install

go get github.com/Joaolfc0/goutil/fsutil

Go docs

Find files

// find all files in dir
fsutil.FindInDir("./", func(filePath string, de fs.DirEntry) error {
    fmt.Println(filePath)
    return nil
})

// find files with filters
fsutil.FindInDir("./", func(filePath string, de fs.DirEntry) error {
    fmt.Println(filePath)
    return nil
}, fsutil.ExcludeDotFile)

Functions API

Note: doc by run go doc ./fsutil

func ApplyFilters(fPath string, ent fs.DirEntry, filters []FilterFunc) bool
func CopyFile(srcPath, dstPath string) error
func CreateFile(fpath string, filePerm, dirPerm os.FileMode, fileFlag ...int) (*os.File, error)
func DeleteIfExist(fPath string) error
func DeleteIfFileExist(fPath string) error
func Dir(fpath string) string
func DiscardReader(src io.Reader)
func ExcludeDotFile(_ string, ent fs.DirEntry) bool
func Expand(pathStr string) string
func ExpandPath(pathStr string) string
func Extname(fpath string) string
func FileExists(path string) bool
func FileExt(fpath string) string
func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)
func GetContents(in any) []byte
func GlobWithFunc(pattern string, fn func(filePath string) error) (err error)
func IsAbsPath(aPath string) bool
func IsDir(path string) bool
func IsFile(path string) bool
func IsImageFile(path string) bool
func IsZipFile(filepath string) bool
func JoinPaths(elem ...string) string
func JoinSubPaths(basePath string, elem ...string) string
func LineScanner(in any) *bufio.Scanner
func MimeType(path string) (mime string)
func MkDirs(perm os.FileMode, dirPaths ...string) error
func MkParentDir(fpath string) error
func MkSubDirs(perm os.FileMode, parentDir string, subDirs ...string) error
func Mkdir(dirPath string, perm os.FileMode) error
func MustCopyFile(srcPath, dstPath string)
func MustCreateFile(filePath string, filePerm, dirPerm os.FileMode) *os.File
func MustReadFile(filePath string) []byte
func MustReadReader(r io.Reader) []byte
func MustRemove(fPath string)
func Name(fpath string) string
func NewIOReader(in any) (r io.Reader, err error)
func OSTempDir(pattern string) (string, error)
func OSTempFile(pattern string) (*os.File, error)
func OnlyFindDir(_ string, ent fs.DirEntry) bool
func OnlyFindFile(_ string, ent fs.DirEntry) bool
func OpenAppendFile(filepath string) (*os.File, error)
func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error)
func OpenReadFile(filepath string) (*os.File, error)
func OpenTruncFile(filepath string) (*os.File, error)
func PathExists(path string) bool
func PathMatch(pattern, s string) bool
func PathName(fpath string) string
func PutContents(filePath string, data any, fileFlag ...int) (int, error)
func QuickOpenFile(filepath string, fileFlag ...int) (*os.File, error)
func QuietRemove(fPath string)
func ReadAll(in any) []byte
func ReadExistFile(filePath string) []byte
func ReadFile(filePath string) []byte
func ReadOrErr(in any) ([]byte, error)
func ReadReader(r io.Reader) []byte
func ReadString(in any) string
func ReadStringOrErr(in any) (string, error)
func ReaderMimeType(r io.Reader) (mime string)
func Realpath(pathStr string) string
func Remove(fPath string) error
func ResolvePath(pathStr string) string
func RmFileIfExist(fPath string) error
func RmIfExist(fPath string) error
func SearchNameUp(dirPath, name string) string
func SearchNameUpx(dirPath, name string) (string, bool)
func SlashPath(path string) string
func SplitPath(pathStr string) (dir, name string)
func Suffix(fpath string) string
func TempDir(dir, pattern string) (string, error)
func TempFile(dir, pattern string) (*os.File, error)
func TextScanner(in any) *scanner.Scanner
func ToAbsPath(p string) string
func UnixPath(path string) string
func Unzip(archive, targetDir string) (err error)
func WalkDir(dir string, fn fs.WalkDirFunc) error
func WriteFile(filePath string, data any, perm os.FileMode, fileFlag ...int) error
func WriteOSFile(f *os.File, data any) (n int, err error)
type FilterFunc func(fPath string, ent fs.DirEntry) bool
func ExcludeSuffix(ss ...string) FilterFunc
func IncludeSuffix(ss ...string) FilterFunc
type HandleFunc func(fPath string, ent fs.DirEntry) error

Code Check & Testing

gofmt -w -l ./
golint ./...

Testing:

go test -v ./fsutil/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./fsutil/...

Documentation

Overview

Package fsutil Filesystem util functions, quick create, read and write file. eg: file and dir check, operate

Index

Constants

View Source
const (
	FsCWAFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND // create, append write-only
	FsCWTFlags = os.O_CREATE | os.O_WRONLY | os.O_TRUNC  // create, override write-only
	FsCWFlags  = os.O_CREATE | os.O_WRONLY               // create, write-only
	FsRWFlags  = os.O_RDWR                               // read-write, dont create.
	FsRFlags   = os.O_RDONLY                             // read-only
)

some commonly flag consts for open file.

View Source
const (
	// MimeSniffLen sniff Length, use for detect file mime type
	MimeSniffLen = 512
)
View Source
const PathSep = os.PathSeparator

PathSep alias of os.PathSeparator

Variables

View Source
var (
	DefaultDirPerm   os.FileMode = 0775
	DefaultFilePerm  os.FileMode = 0665
	OnlyReadFilePerm os.FileMode = 0444
)

perm for create dir or file

View Source
var (
	// DefaultFileFlags for create and write
	DefaultFileFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND
	// OnlyReadFileFlags open file for read
	OnlyReadFileFlags = os.O_RDONLY
)
View Source
var (
	DirExist  = IsDir
	FileExist = IsFile
	PathExist = PathExists
)

alias methods

View Source
var (
	// MustRm removes the named file or (empty) directory.
	MustRm = MustRemove
	// QuietRm removes the named file or (empty) directory.
	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 ApplyFilters

func ApplyFilters(fPath string, ent fs.DirEntry, filters []FilterFunc) bool

ApplyFilters handle

func CopyFile

func CopyFile(srcPath, dstPath string) error

CopyFile copy a file to another file path.

func CreateFile

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

CreateFile create file if not exists

Usage:

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

func DeleteIfExist

func DeleteIfExist(fPath string) error

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

func DeleteIfFileExist

func DeleteIfFileExist(fPath string) error

DeleteIfFileExist removes the named file on exists.

func DetectMime

func DetectMime(path string) string

DetectMime detect file mime type. alias of MimeType()

func Dir

func Dir(fpath string) string

Dir get dir path from filepath, without last name.

func DirPath

func DirPath(fpath string) string

DirPath get dir path from filepath, without last name.

func DiscardReader

func DiscardReader(src io.Reader)

DiscardReader anything from the reader

func ExcludeDotFile

func ExcludeDotFile(_ string, ent fs.DirEntry) bool

ExcludeDotFile on find

func Expand

func Expand(pathStr string) string

Expand will parse first `~` as user home dir path.

func ExpandPath

func ExpandPath(pathStr string) string

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

func Extname

func Extname(fpath string) string

Extname get filename ext. alias of filepath.Ext()

eg: path/to/main.go => "go"

func FileExists

func FileExists(path string) bool

FileExists reports whether the named file or directory exists.

func FileExt

func FileExt(fpath string) string

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

eg: path/to/main.go => ".go"

func FilePathInDirs

func FilePathInDirs(file string, dirs ...string) string

FilePathInDirs get full file path in dirs.

Params:

  • file: can be relative path, file name, full path.
  • dirs: dir paths

func FindInDir

func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)

FindInDir code refer the go pkg: path/filepath.glob()

- TIP: will be not find in subdir.

filters: return false will skip the file.

func FirstExists

func FirstExists(paths ...string) string

FirstExists check multi paths and return first exists path.

func FirstExistsDir

func FirstExistsDir(paths ...string) string

FirstExistsDir check multi paths and return first exists dir.

func FirstExistsFile

func FirstExistsFile(paths ...string) string

FirstExistsFile check multi paths and return first exists file.

func GetContents

func GetContents(in any) []byte

GetContents read contents from path or io.Reader, will panic on in type error

func Glob

func Glob(pattern string, fls ...NameMatchFunc) []string

Glob find files by glob path pattern. alias of filepath.Glob() and support filter matched files by name.

Usage:

files := fsutil.Glob("/path/to/dir/*.go")

func GlobWithFunc

func GlobWithFunc(pattern string, fn func(filePath string) error) (err error)

GlobWithFunc find files by glob path pattern, then handle matched file

- TIP: will be not find in subdir.

func IsAbsPath

func IsAbsPath(aPath string) bool

IsAbsPath is abs path.

func IsDir

func IsDir(path string) bool

IsDir reports whether the named directory exists.

func IsEmptyDir

func IsEmptyDir(dirPath string) bool

IsEmptyDir reports whether the named directory is empty.

func IsFile

func IsFile(path string) bool

IsFile reports whether the named file or directory exists.

func IsImageFile

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 JoinPaths

func JoinPaths(elem ...string) string

JoinPaths elements, alias of filepath.Join()

func JoinPaths3

func JoinPaths3(basePath, secPath string, elems ...string) string

JoinPaths3 elements, like the filepath.Join()

func JoinSubPaths

func JoinSubPaths(basePath string, elems ...string) string

JoinSubPaths elements, like the filepath.Join()

func LineScanner

func LineScanner(in any) *bufio.Scanner

LineScanner create from filepath or io.Reader, will panic on in type error. Will scan and parse text to lines.

s := fsutil.LineScanner("/path/to/file")
for s.Scan() {
	fmt.Println(s.Text())
}

func MatchFirst

func MatchFirst(paths []string, matcher PathMatchFunc, defaultPath string) string

MatchFirst filter paths by filter func and return first match path.

func MatchPaths

func MatchPaths(paths []string, matcher PathMatchFunc) []string

MatchPaths given paths by custom mather func.

func MimeType

func MimeType(path string) (mime string)

MimeType get file mime type name. eg "image/png"

func MkDirs

func MkDirs(perm os.FileMode, dirPaths ...string) error

MkDirs batch make multi dirs at once

func MkParentDir

func MkParentDir(fpath string) error

MkParentDir quick create parent dir for given path.

func MkSubDirs

func MkSubDirs(perm os.FileMode, parentDir string, subDirs ...string) error

MkSubDirs batch make multi sub-dirs at once

func Mkdir

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

Mkdir alias of os.MkdirAll()

func Must2

func Must2(_ any, err error)

Must2 ok for (any, error) result. if it has error, will panic

func MustCopyFile

func MustCopyFile(srcPath, dstPath string)

MustCopyFile copy file to another path.

func MustCreateFile

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

MustCreateFile create file, will panic on error

func MustOpenFile

func MustOpenFile(filePath string, flag int, perm os.FileMode) *os.File

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

Usage:

file := MustOpenFile("path/to/file.txt", FsCWFlags, 0666)

func MustRead

func MustRead(in any) []byte

MustRead read contents from path or io.Reader, will panic on in type error

func MustReadFile

func MustReadFile(filePath string) []byte

MustReadFile read file contents, will panic on error

func MustReadReader

func MustReadReader(r io.Reader) []byte

MustReadReader read contents from io.Reader, will panic on error

func MustRemove

func MustRemove(fPath string)

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

func MustSave

func MustSave(filePath string, data any, optFns ...OpenOptionFunc)

MustSave create file and write contents to file, panic on error.

  • data type allow: string, []byte, io.Reader

default option see NewOpenOption()

func Name

func Name(fpath string) string

Name get file/dir name from full path.

eg: path/to/main.go => main.go

func NewIOReader

func NewIOReader(in any) (r io.Reader, err error)

NewIOReader instance by input file path or io.Reader

func OSTempDir

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.*")

func OSTempFile

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

OSTempFile create a temp file on os.TempDir()

Usage:

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

func OnlyFindDir

func OnlyFindDir(_ string, ent fs.DirEntry) bool

OnlyFindDir on find

func OnlyFindFile

func OnlyFindFile(_ string, ent fs.DirEntry) bool

OnlyFindFile on find

func OpenAppendFile

func OpenAppendFile(filepath string, filePerm ...os.FileMode) (*os.File, error)

OpenAppendFile like os.OpenFile, open for append write. if not exists, will create it.

func OpenFile

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

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

Usage:

file, err := OpenFile("path/to/file.txt", FsCWFlags, 0666)

func OpenReadFile

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

OpenReadFile like os.OpenFile, open file for read contents

func OpenTruncFile

func OpenTruncFile(filepath string, filePerm ...os.FileMode) (*os.File, error)

OpenTruncFile like os.OpenFile, open for override write. if not exists, will create it.

func PathExists

func PathExists(path string) bool

PathExists reports whether the named file or directory exists.

func PathMatch

func PathMatch(pattern, s string) bool

PathMatch check for a string. alias of path.Match()

func PathName

func PathName(fpath string) string

PathName get file/dir name from full path

func PutContents

func PutContents(filePath string, data any, fileFlag ...int) (int, error)

PutContents create file and write contents to file at once.

data type allow: string, []byte, io.Reader. will auto create dir.

Tip: file flag default is FsCWTFlags (override write)

Usage:

fsutil.PutContents(filePath, contents, fsutil.FsCWAFlags) // append write
fsutil.Must2(fsutil.PutContents(filePath, contents)) // panic on error

func QuickOpenFile

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

QuickOpenFile like os.OpenFile, open for append write. if not exists, will create it.

Alias of OpenAppendFile()

func QuietRemove

func QuietRemove(fPath string)

QuietRemove removes the named file or (empty) directory.

NOTICE: will ignore error

func ReadAll

func ReadAll(in any) []byte

ReadAll read contents from path or io.Reader, will panic on in type error

func ReadExistFile

func ReadExistFile(filePath string) []byte

ReadExistFile read file contents if existed, will panic on error

func ReadFile

func ReadFile(filePath string) []byte

ReadFile read file contents, will panic on error

func ReadOrErr

func ReadOrErr(in any) ([]byte, error)

ReadOrErr read contents from path or io.Reader, will panic on in type error

func ReadReader

func ReadReader(r io.Reader) []byte

ReadReader read contents from io.Reader, will panic on error

func ReadString

func ReadString(in any) string

ReadString read contents from path or io.Reader, will panic on in type error

func ReadStringOrErr

func ReadStringOrErr(in any) (string, error)

ReadStringOrErr read contents from path or io.Reader, will panic on in type error

func ReaderMimeType

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

func Realpath(pathStr string) string

Realpath returns the shortest path name equivalent to path by purely lexical processing. Will expand ~ to home dir, and join with workdir if path is relative path.

func Remove

func Remove(fPath string) error

Remove removes the named file or (empty) directory.

func RemoveSub

func RemoveSub(dirPath string, fns ...FilterFunc) error

RemoveSub removes all sub files and dirs of dirPath, but not remove dirPath.

func ResolvePath

func ResolvePath(pathStr string) string

ResolvePath will parse `~` and env var in path

func RmFileIfExist

func RmFileIfExist(fPath string) error

RmFileIfExist removes the named file on exists.

func RmIfExist

func RmIfExist(fPath string) error

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

func SafeRemoveAll

func SafeRemoveAll(path string)

SafeRemoveAll removes path and any children it contains. will ignore error

func SaveFile

func SaveFile(filePath string, data any, optFns ...OpenOptionFunc) error

SaveFile create file and write contents to file. will auto create dir.

  • data type allow: string, []byte, io.Reader

default option see NewOpenOption()

func SearchNameUp

func SearchNameUp(dirPath, name string) string

SearchNameUp find file/dir name in dirPath or parent dirs, return the name of directory path

Usage:

repoDir := fsutil.SearchNameUp("/path/to/dir", ".git")

func SearchNameUpx

func SearchNameUpx(dirPath, name string) (string, bool)

SearchNameUpx find file/dir name in dirPath or parent dirs, return the name of directory path and dir is changed.

func SlashPath

func SlashPath(path string) string

SlashPath alias of filepath.ToSlash

func SplitPath

func SplitPath(pathStr string) (dir, name string)

SplitPath splits path immediately following the final Separator, separating it into a directory and file name component

func Suffix

func Suffix(fpath string) string

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

eg: path/to/main.go => ".go"

func TempDir

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

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

Usage:

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

func TempFile

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

TempFile is like os.CreateTemp, but can custom temp dir.

Usage:

// create temp file on os.TempDir()
fsutil.TempFile("", "example.*.txt")
// create temp file on "testdata" dir
fsutil.TempFile("testdata", "example.*.txt")

func TextScanner

func TextScanner(in any) *scanner.Scanner

TextScanner from filepath or io.Reader, will panic on in type error. Will scan parse text to tokens: Ident, Int, Float, Char, String, RawString, Comment, etc.

Usage:

s := fsutil.TextScanner("/path/to/file")
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
	fmt.Printf("%s: %s\n", s.Position, s.TokenText())
}

func ToAbsPath

func ToAbsPath(p string) string

ToAbsPath convert path to absolute path. Will expand home dir, if empty will return current work dir

TIP: will don't check path is really exists

func UnixPath

func UnixPath(path string) string

UnixPath like of filepath.ToSlash, but always replace

func Unzip

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

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

func UpdateContents

func UpdateContents(filePath string, handleFn func(bs []byte) []byte) error

UpdateContents read file contents, call handleFn(contents) handle, then write updated contents to file

func WalkDir

func WalkDir(dir string, fn fs.WalkDirFunc) error

WalkDir walks the file tree rooted at root, calling fn for each file or directory in the tree, including root.

TIP: will recursively find in sub dirs.

func WriteFile

func WriteFile(filePath string, data any, perm os.FileMode, fileFlag ...int) error

WriteFile create file and write contents to file, can set perm for file.

data type allow: string, []byte, io.Reader

Tip: file flag default is FsCWTFlags (override write)

Usage:

fsutil.WriteFile(filePath, contents, fsutil.DefaultFilePerm, fsutil.FsCWAFlags)

func WriteOSFile

func WriteOSFile(f *os.File, data any) (n int, err error)

WriteOSFile write data to give os.File, then close file.

data type allow: string, []byte, io.Reader

Types

type Entry

type Entry interface {
	fs.DirEntry
	// Path get file/dir full path. eg: "/path/to/file.go"
	Path() string
	// Info get file info. like fs.DirEntry.Info(), but will cache result.
	Info() (fs.FileInfo, error)
}

Entry extends fs.DirEntry, add some useful methods

func NewEntry

func NewEntry(fPath string, ent fs.DirEntry) Entry

NewEntry create a new Entry instance

type FileInfo

type FileInfo interface {
	fs.FileInfo
	// Path get file full path. eg: "/path/to/file.go"
	Path() string
}

FileInfo extends fs.FileInfo, add some useful methods

func NewFileInfo

func NewFileInfo(fPath string, info fs.FileInfo) FileInfo

NewFileInfo create a new FileInfo instance

type FileInfos

type FileInfos []FileInfo

FileInfos type for FileInfo slice

implements sort.Interface:

sorts by oldest time modified in the file info.
eg: [old_220211, old_220212, old_220213]

func (FileInfos) Len

func (fis FileInfos) Len() int

Len get length

func (FileInfos) Less

func (fis FileInfos) Less(i, j int) bool

Less check by mod time

func (FileInfos) Swap

func (fis FileInfos) Swap(i, j int)

Swap swap values

type FilterFunc

type FilterFunc func(fPath string, ent fs.DirEntry) bool

FilterFunc type for FindInDir

- return False will skip handle the file.

func ExcludeNames

func ExcludeNames(names ...string) FilterFunc

ExcludeNames on find

func ExcludeSuffix

func ExcludeSuffix(ss ...string) FilterFunc

ExcludeSuffix on find

func IncludeSuffix

func IncludeSuffix(ss ...string) FilterFunc

IncludeSuffix on find

type HandleFunc

type HandleFunc func(fPath string, ent fs.DirEntry) error

HandleFunc type for FindInDir

type NameMatchFunc

type NameMatchFunc = comdef.StringMatchFunc

NameMatchFunc name match func, alias of comdef.StringMatchFunc

type OpenOption

type OpenOption struct {
	// file open flag. see FsCWTFlags
	Flag int
	// file perm. see DefaultFilePerm
	Perm os.FileMode
}

OpenOption for open file

func NewOpenOption

func NewOpenOption(optFns ...OpenOptionFunc) *OpenOption

NewOpenOption create a new OpenOption instance

Defaults:

  • open flags: FsCWTFlags (override write)
  • file Perm: DefaultFilePerm

func OpenOptOrNew

func OpenOptOrNew(opt *OpenOption) *OpenOption

OpenOptOrNew create a new OpenOption instance if opt is nil

type OpenOptionFunc

type OpenOptionFunc func(*OpenOption)

OpenOptionFunc for open/write file

func WithFlag

func WithFlag(flag int) OpenOptionFunc

WithFlag set file open flag

func WithPerm

func WithPerm(perm os.FileMode) OpenOptionFunc

WithPerm set file perm

type PathMatchFunc

type PathMatchFunc = comdef.StringMatchFunc

PathMatchFunc path match func. alias of comdef.StringMatchFunc

Directories

Path Synopsis
Package finder Provides a simple and convenient filedir lookup function, supports filtering, excluding, matching, ignoring, etc.
Package finder Provides a simple and convenient filedir lookup function, supports filtering, excluding, matching, ignoring, etc.

Jump to

Keyboard shortcuts

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