fsutil

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

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
	FsRFlags   = os.O_RDONLY                             // read-only
)

some flag consts for open file

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

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 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 Dir

func Dir(fpath string) string

Dir get dir path, without last name.

func DiscardReader

func DiscardReader(src io.Reader)

DiscardReader anything from the reader

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 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 path.Ext()

func FindInDir

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

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

filters: return false will skip the file.

func GetContents

func GetContents(in any) []byte

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

func GlobWithFunc

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

GlobWithFunc handle matched file

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 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 LineScanner

func LineScanner(in any) *bufio.Scanner

LineScanner create from filepath or io.Reader

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

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

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 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 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 Name

func Name(fpath string) string

Name get file/dir name from full path

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 OpenFile

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

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

func OpenReadFile

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

OpenReadFile like os.OpenFile, open file for read contents

func PathExists

func PathExists(path string) bool

PathExists reports whether the named file or directory exists.

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

Tip: file flag default is FsCWAFlags

Usage:

fsutil.PutContents(filePath, contents, fsutil.FsCWTFlags)

func QuickOpenFile

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

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

Tip: file flag default is FsCWAFlags

func QuietRemove

func QuietRemove(fPath string)

QuietRemove removes the named file or (empty) directory.

NOTICE: will ignore error

func ReadExistFile

func ReadExistFile(filePath string) []byte

ReadExistFile read file contents if existed, 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 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.

func Remove

func Remove(fPath string) error

Remove removes the named file or (empty) directory.

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 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 path.Ext()

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:

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

func TextScanner

func TextScanner(in any) *scanner.Scanner

TextScanner from filepath or io.Reader, will panic on in type error

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 Unzip

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

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

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

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 FilterFunc

type FilterFunc func(fPath string, fi os.FileInfo) bool

FilterFunc type for FindInDir

type HandleFunc

type HandleFunc func(fPath string, fi os.FileInfo) error

HandleFunc type for FindInDir

Directories

Path Synopsis
Package finder provide a finder tool for find files
Package finder provide a finder tool for find files

Jump to

Keyboard shortcuts

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