filesystems

package
v2.9.10 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

FilesystemV4 用法

  1. 初始化DirFile

    package main
    
    import (
    	. "fmt"
    
    	"github.com/aid297/aid/v2/filesystems/filesystems"
    )
    
    func main() {
    	dir1 := filesystemV4.NewDir(filesystemV4.Rel("."))
    	Printf("init dir by rel: %s\n", dir1.GetFullPath()) // init dir by rel: /Users/yujizhou/development/projects/go/readme
    
    	dir2 := filesystemV4.NewDir(filesystemV4.Abs("/Users/yujizhou/development/projects/go/readme"))
    	Printf("init dir by abs: %s\n", dir2.GetFullPath()) // init dir by abs: /Users/yujizhou/development/projects/go/readme
    
    	file1 := filesystemV4.NewFile(filesystemV4.Rel("./main.go"))
    	Printf("init file by rel: %s\n", file1.GetFullPath()) // init file by rel: /Users/yujizhou/development/projects/go/readme/main.go
    
    	file2 := filesystemV4.NewFile(filesystemV4.Abs("/Users/yujizhou/development/projects/go/readme/main.go"))
    	Printf("init file by abs: %s\n", file2.GetFullPath()) // init file by abs: /Users/yujizhou/development/projects/go/readme/main.go
    }
    
  2. 创建文件夹

    package main
    
    import (
    	. "fmt"
    
    	"github.com/aid297/aid/v2/filesystems/filesystems"
    )
    
    func main() {
    	dir1 := filesystemV4.NewDir(filesystemV4.Rel("./a"))
    	Printf("./a is exist before create: %v\n", dir1.GetExist()) // ./a is exist before create: true
    
    	err := dir1.Create(filesystemV4.Flag(0644)).GetError()
    	if err != nil {
    		panic(err)
    	}
    
    	Printf("./a is exist after create: %v\n", dir1.GetExist()) // ./a is exist after create: true
    	Printf("./a fullpath is: %s \n", dir1.GetFullPath())       // ./a fullpath is: /Users/yujizhou/development/projects/go/readme/a
      // 注意:创建文件夹之后,当前对象的目录会自动移动到新文件夹下
    }
    
  3. 创建文件,并写入内容

    package main
    
    import (
    	. "fmt"
    
    	"github.com/aid297/aid/v2/filesystems/filesystems"
    )
    
    func main() {
    	dir1 := filesystemV4.NewDir(filesystemV4.Rel("./a"))
    	file1 := filesystemV4.NewFile(filesystemV4.Abs(dir1.GetFullPath(), "1.txt"))
    	Printf("file1 is exist: %v\n", file1.GetExist()) // file1 is exist: false
    
    	err := file1.Create().GetError()
    	if err != nil {
    		panic(err)
    	}
    
    	Printf("file1 is exist: %v\n", file1.GetExist()) // file1 is exist: true
    	// 注意,如果a文件夹不存在,file1.Create()会直接创建这个文件夹
    
    	err = file1.Write([]byte("Is somthing here ...")).GetError()
    	if err != nil {
    		panic(err)
    	}
    
    	fileContent, err := file1.Read()
    	if err != nil {
    		panic(err)
    	}
    
    	Printf("file1 content: %s\n", string(fileContent)) // file1 content: Is somthing here ...
    }
    
  4. 复制文件

    package main
    
    import (
    	. "fmt"
    
    	"github.com/aid297/aid/v2/filesystems/filesystemV4"
    )
    
    func main() {
    	dir1 := filesystemV4.NewDir(filesystemV4.Rel("./a"))
    	file1 := filesystemV4.NewFile(filesystemV4.Abs(dir1.GetFullPath(), "1.txt"))
    	Printf("file1 is exist: %v\n", file1.GetExist()) // file1 is exist: false
    
    	file1.CopyTo(true, "./2.txt") // copy file1 to 2.txt, but file1 is not exist, so 2.txt is not exist
    	file2 := filesystemV4.NewFile(filesystemV4.Rel("./2.txt"))
    	Printf("file1 fullpath: %s\n", file1.GetFullPath()) // 复制不会导致原文件路径发生变化
    	Printf("file2 is exist: %v\n", file2.GetExist())    // file2 is exist: true
    	Printf("file2 fullpath: %s\n", file2.GetFullPath()) // file2 fullpath: /Users/yujizhou/development/projects/go/readme/2.txt
    	// 注意:复制文件时的isRel指的是当前路径的相对路径,不是 file1 的相对路径
    }
    
  5. 删除文件

    package main
    
    import (
    	. "fmt"
    
    	"github.com/aid297/aid/v2/filesystems/filesystemV4"
    )
    
    func main() {
    	dir1 := filesystemV4.NewDir(filesystemV4.Rel("./a"))
    	file1 := filesystemV4.NewFile(filesystemV4.Abs(dir1.GetFullPath(), "1.txt"))
    	file1.CopyTo(true, "./a/2.txt")
    
    	file2 := filesystemV4.NewFile(filesystemV4.Abs(dir1.GetFullPath(), "2.txt"))
    	Printf("file2 is exist: %v\n", file2.GetExist()) // file2 is exist: true
    
    	err := file2.Remove().GetError()
    	if err != nil {
    		panic(err)
    	}
    
    	Printf("file2 is exist: %v\n", file2.GetExist()) // file2 is exist: false
    }
    
  6. 复制文件夹

    package main
    
    import (
    	. "fmt"
    
    	"github.com/aid297/aid/v2/filesystems/filesystemV4"
    )
    
    func main() {
    	// 创建目录:a
    	if dir := filesystemV4.NewDir(filesystemV4.Rel("./a")); dir.GetError() != nil {
    		panic(dir.GetError())
    	}
    
    	// 在目录a下创建5个文件:file-1.txt、file-2.txt、file-3.txt、file-4.txt、file-5.txt,并写入内容:content-1、content-2、content-3、content-4、content-5
    	for idx := range 5 {
    		if file := filesystemV4.NewFile(filesystemV4.Rel(Sprintf("./a/file-%d.txt", idx+1))).Create(); file.GetError() != nil {
    			panic(file.GetError())
    		} else {
    			if err := file.Write(Appendf(nil, "content-%d", idx+1)).GetError(); err != nil {
    				panic(err)
    			}
    		}
    	}
    
    	// 复制目录a到目录b
    	if dir := filesystemV4.NewDir(filesystemV4.Rel("./a")).CopyTo(true, "./b"); dir.GetError() != nil {
    		panic(dir.GetError())
    	}
    
    	// 获取目录b下的所有文件,并打印它们的内容
    	if dir := filesystemV4.NewDir(filesystemV4.Rel("./b")); dir.GetError() != nil {
    		panic(dir.GetError())
    	} else {
    		for _, file := range dir.LS().GetFiles() {
    			Printf("file in new dir: %s\n", file.GetFullPath())
    		}
    
    		// 打印结果:
    		// file in new dir: /Users/yujizhou/development/projects/go/readme/b/file-1.txt
    		// file in new dir: /Users/yujizhou/development/projects/go/readme/b/file-2.txt
    		// file in new dir: /Users/yujizhou/development/projects/go/readme/b/file-3.txt
    		// file in new dir: /Users/yujizhou/development/projects/go/readme/b/file-4.txt
    		// file in new dir: /Users/yujizhou/development/projects/go/readme/b/file-5.txt
    	}
    }
    
  7. 打印文件夹下所以内容

    package main
    
    import (
    	. "fmt"
    
    	"github.com/aid297/aid/v2/filesystems/filesystemV4"
    )
    
    func main() {
    	// 创建目录:a
    	if dir := filesystemV4.NewDir(filesystemV4.Rel("./a")); dir.GetError() != nil {
    		panic(dir.GetError())
    	}
    
    	// 在目录a下创建5个文件:file-1.txt、file-2.txt、file-3.txt、file-4.txt、file-5.txt,并写入内容:content-1、content-2、content-3、content-4、content-5
    	for idx := range 5 {
    		if file := filesystemV4.NewFile(filesystemV4.Rel(Sprintf("./a/file-%d.txt", idx+1))).Create(); file.GetError() != nil {
    			panic(file.GetError())
    		} else {
    			if err := file.Write(Appendf(nil, "content-a-%d", idx+1)).GetError(); err != nil {
    				panic(err)
    			}
    		}
    	}
    
    	// 在目录a下创建一个子目录:b,并创建文件
    	if dir := filesystemV4.NewDir(filesystemV4.Rel("./a/b")).Create(); dir.GetError() != nil {
    		panic(dir.GetError())
    	}
    	for idx := range 5 {
    		if file := filesystemV4.NewFile(filesystemV4.Rel(Sprintf("./a/b/file-%d.txt", idx+1))).Create(); file.GetError() != nil {
    			panic(file.GetError())
    		} else {
    			if err := file.Write(Appendf(nil, "content-b-%d", idx+1)).GetError(); err != nil {
    				panic(err)
    			}
    		}
    	}
    
    	// 列出目录a下的所有文件和目录
    	dirA := filesystemV4.NewDir(filesystemV4.Rel("./a")).LS()
    	for _, file := range dirA.GetFiles() {
    		Printf("file in a: %s\n", file.GetFullPath())
    	}
    	for _, dir := range dirA.GetDirs() {
    		Printf("dir in a: %s\n", dir.GetFullPath())
    
    		for _, file := range dir.LS().GetFiles() {
    			Printf("file in b: %s\n", file.GetFullPath())
    		}
    
    		for _, dir := range dir.LS().GetDirs() {
    			Printf("dir in b: %s\n", dir.GetFullPath())
    		}
    	}
    
    	// 打印结果:
    	// file in a: /Users/yujizhou/development/projects/go/readme/a/file-1.txt
    	// file in a: /Users/yujizhou/development/projects/go/readme/a/file-2.txt
    	// file in a: /Users/yujizhou/development/projects/go/readme/a/file-3.txt
    	// file in a: /Users/yujizhou/development/projects/go/readme/a/file-4.txt
    	// file in a: /Users/yujizhou/development/projects/go/readme/a/file-5.txt
    	// dir in a: /Users/yujizhou/development/projects/go/readme/a/b
    	// file in b: /Users/yujizhou/development/projects/go/readme/a/b/file-1.txt
    	// file in b: /Users/yujizhou/development/projects/go/readme/a/b/file-2.txt
    	// file in b: /Users/yujizhou/development/projects/go/readme/a/b/file-3.txt
    	// file in b: /Users/yujizhou/development/projects/go/readme/a/b/file-4.txt
    	// file in b: /Users/yujizhou/development/projects/go/readme/a/b/file-5.txt
    }
    
    
  8. 其他简单方法

    Lock() Filesystemer      // 加锁:写锁
    Unlock() Filesystemer    // 解锁:写锁
    RLock() Filesystemer     // 加锁:读锁
    RUnlock() Filesystemer   // 解锁:读锁
    RemoveAll() Filesystemer // 删除文件夹(包括文件夹下所有递归子集)
    Up() Filesystemer        // 返回上一级
    

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInit         = fmt.Errorf("获取文件或目录信息失败")
	ErrMissFullPath = fmt.Errorf("文件或目录完整路径不能为空")
	ErrRename       = fmt.Errorf("修改文件名失败")
	ErrRemove       = fmt.Errorf("删除文件失败")
	ErrFileNotExist = fmt.Errorf("文件不存在")
	ErrCreateFile   = fmt.Errorf("创建文件失败")
	ErrWriteFile    = fmt.Errorf("写入文件失败")
	ErrReadFile     = fmt.Errorf("读取文件失败")
	ErrOpenFile     = fmt.Errorf("打开文件失败")
	ErrDirNotExist  = fmt.Errorf("目录不存在")
	ErrCreateDir    = fmt.Errorf("创建目录失败")
	ErrReadDir      = fmt.Errorf("读取目录失败")
)
View Source
var (
	DefaultCreateFlag = os.O_APPEND | os.O_CREATE | os.O_WRONLY
	DefaultReadFlag   = os.O_RDWR
)

Functions

This section is empty.

Types

type AttrFlag

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

func (AttrFlag) Register

func (my AttrFlag) Register(o *Operation)

type AttrMode

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

func (AttrMode) Register

func (my AttrMode) Register(o *Operation)

type AttrPath

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

func (AttrPath) GetPath

func (my AttrPath) GetPath() string

func (AttrPath) Joins

func (my AttrPath) Joins(paths ...string) PathAttributer

func (AttrPath) Register

func (my AttrPath) Register(f Filesystem)

type Dir

type Dir struct {
	Error    error        `json:"error" swaggertype:"string"`       // 错误信息
	Name     string       `json:"name" swaggertype:"string"`        // 文件名
	BasePath string       `json:"basePath" swaggertype:"string"`    // 基础路径
	FullPath string       `json:"fullPath" swaggertype:"string"`    // 完整路径
	Size     int64        `json:"size" swaggertype:"integer"`       // 文件大小
	Info     os.FileInfo  `json:"info" swaggertype:"string"`        // 文件信息
	Mode     os.FileMode  `json:"mode" swaggertype:"string"`        // 文件权限
	Exist    bool         `json:"exist" swaggertype:"boolean"`      // 文件是否存在
	Files    []Filesystem `json:"files" swaggertype:"array,object"` // 目录下的文件列表
	Dirs     []Filesystem `json:"dirs" swaggertype:"array,object"`  // 子目录列表
	Kind     string       `json:"kind" swaggertype:"string"`        // 类型
	// contains filtered or unexported fields
}

func (*Dir) Copy

func (my *Dir) Copy() Filesystem

Copy 复制当前对象

func (*Dir) CopyDirsTo

func (my *Dir) CopyDirsTo(isRel bool, dstPaths ...string) *Dir

CopyTo 复制当前目录下的所有子目录到目标路径

func (*Dir) CopyFilesTo

func (my *Dir) CopyFilesTo(isRel bool, dstPaths ...string) *Dir

CopyFilesTo 复制当前目录下的所有文件到目标路径

func (*Dir) CopyTo

func (my *Dir) CopyTo(isRel bool, dstPaths ...string) Filesystem

CopyAllTo 复制当前目录下的所有文件和子目录到目标路径

func (*Dir) Create

func (my *Dir) Create(attrs ...OperationAttributer) Filesystem

Create 创建多级目录

func (*Dir) GetBasePath

func (my *Dir) GetBasePath() string

func (*Dir) GetDirs

func (my *Dir) GetDirs() []Filesystem

func (*Dir) GetError

func (my *Dir) GetError() error

func (*Dir) GetExist

func (my *Dir) GetExist() bool

func (*Dir) GetFiles

func (my *Dir) GetFiles() []Filesystem

func (*Dir) GetFullPath

func (my *Dir) GetFullPath() string

func (*Dir) GetInfo

func (my *Dir) GetInfo() os.FileInfo

func (*Dir) GetKind

func (my *Dir) GetKind() string

func (*Dir) GetName

func (my *Dir) GetName() string

func (*Dir) Join

func (my *Dir) Join(paths ...string) Filesystem

func (*Dir) LS

func (my *Dir) LS() Filesystem

LS 列出当前目录下的所有文件和子目录

func (*Dir) Lock

func (my *Dir) Lock() Filesystem

Lock 加锁 → 写

func (*Dir) RLock

func (my *Dir) RLock() Filesystem

RLock 加锁 → 读

func (*Dir) RUnlock

func (my *Dir) RUnlock() Filesystem

RUnlock 解锁 → 读

func (*Dir) Read

func (my *Dir) Read(attrs ...OperationAttributer) ([]byte, error)

func (*Dir) Remove

func (my *Dir) Remove() Filesystem

Remove 删除目录

func (*Dir) RemoveAll

func (my *Dir) RemoveAll() Filesystem

RemoveAll 递归删除目录

func (*Dir) Rename

func (my *Dir) Rename(newName string) Filesystem

Rename 重命名目录

func (*Dir) SetAttrs

func (my *Dir) SetAttrs(attrs ...PathAttributer) Filesystem

func (*Dir) SetFullPathByAttr

func (my *Dir) SetFullPathByAttr(attrs ...PathAttributer) Filesystem

func (*Dir) SetFullPathForAttr

func (my *Dir) SetFullPathForAttr(path string) Filesystem

func (*Dir) Unlock

func (my *Dir) Unlock() Filesystem

Unlock 解锁 → 写

func (*Dir) Up

func (my *Dir) Up() Filesystem

Up 向上一级目录

func (*Dir) Write

func (my *Dir) Write(content []byte, attrs ...OperationAttributer) Filesystem

func (*Dir) Zip

func (my *Dir) Zip() Filesystem

Zip 压缩整个目录到 zip 文件

type File

type File struct {
	Error    error       `json:"error" swaggertype:"string"`     // 错误信息
	Name     string      `json:"name" swaggertype:"string"`      // 文件名
	BasePath string      `json:"basePath" swaggertype:"string"`  // 基础路径
	FullPath string      `json:"fullPath" swaggertype:"string"`  // 完整路径
	Size     int64       `json:"size" swaggertype:"integer"`     // 文件大小
	Info     os.FileInfo `json:"info" swaggertype:"string"`      // 文件信息
	Mode     os.FileMode `json:"mode" swaggertype:"string"`      // 文件权限
	Exist    bool        `json:"exist" swaggertype:"boolean"`    // 文件是否存在
	Ext      string      `json:"extension" swaggertype:"string"` // 文件扩展名
	Mime     string      `json:"mime" swaggertype:"string"`      // 文件 Mime 类型
	Kind     string      `json:"kind" swaggertype:"string"`      // 类型
	// contains filtered or unexported fields
}

func (*File) Copy

func (my *File) Copy() Filesystem

func (*File) CopyTo

func (my *File) CopyTo(isRel bool, dstPaths ...string) Filesystem

CopyTo 复制文件到指定路径

func (*File) Create

func (my *File) Create(attrs ...OperationAttributer) Filesystem

Create 创建文件

func (*File) GetBasePath

func (my *File) GetBasePath() string

func (*File) GetDirs

func (my *File) GetDirs() []Filesystem

func (*File) GetError

func (my *File) GetError() error

func (*File) GetExist

func (my *File) GetExist() bool

func (*File) GetFiles

func (my *File) GetFiles() []Filesystem

func (*File) GetFullPath

func (my *File) GetFullPath() string

func (*File) GetInfo

func (my *File) GetInfo() os.FileInfo

func (*File) GetKind

func (my *File) GetKind() string

func (*File) GetName

func (my *File) GetName() string

func (*File) Join

func (my *File) Join(paths ...string) Filesystem

func (*File) LS

func (my *File) LS() Filesystem

func (*File) Lock

func (my *File) Lock() Filesystem

Lock 加锁 → 写

func (*File) RLock

func (my *File) RLock() Filesystem

RLock 加锁 → 读

func (*File) RUnlock

func (my *File) RUnlock() Filesystem

RUnlock 解锁 → 读

func (*File) Read

func (my *File) Read(attrs ...OperationAttributer) ([]byte, error)

Read 读取文件内容

func (*File) Remove

func (my *File) Remove() Filesystem

Remove 删除文件

func (*File) RemoveAll

func (my *File) RemoveAll() Filesystem

func (*File) Rename

func (my *File) Rename(newName string) Filesystem

Rename 重命名文件

func (*File) SetAttrs

func (my *File) SetAttrs(attrs ...PathAttributer) Filesystem

func (*File) SetFullPathByAttr

func (my *File) SetFullPathByAttr(attrs ...PathAttributer) Filesystem

func (*File) SetFullPathForAttr

func (my *File) SetFullPathForAttr(path string) Filesystem

func (*File) Unlock

func (my *File) Unlock() Filesystem

Unlock 解锁 → 写

func (*File) Up

func (my *File) Up() Filesystem

func (*File) Write

func (my *File) Write(content []byte, attrs ...OperationAttributer) Filesystem

向文件内写入内容

func (*File) Zip

func (my *File) Zip() Filesystem

Zip 压缩文件到 zip 格式

type Filesystem

type Filesystem interface {
	GetName() string
	GetExist() bool
	GetError() error
	GetBasePath() string
	GetFullPath() string
	GetInfo() os.FileInfo
	GetDirs() []Filesystem
	GetFiles() []Filesystem
	GetKind() string
	SetAttrs(attrs ...PathAttributer) Filesystem
	SetFullPathForAttr(path string) Filesystem
	SetFullPathByAttr(attrs ...PathAttributer) Filesystem

	Lock() Filesystem
	Unlock() Filesystem
	RLock() Filesystem
	RUnlock() Filesystem
	Join(paths ...string) Filesystem
	Create(attrs ...OperationAttributer) Filesystem
	Rename(newName string) Filesystem
	Remove() Filesystem
	RemoveAll() Filesystem
	Write(content []byte, attrs ...OperationAttributer) Filesystem
	Read(attrs ...OperationAttributer) ([]byte, error)
	CopyTo(isRel bool, dstPaths ...string) Filesystem
	Copy() Filesystem
	Up() Filesystem
	LS() Filesystem
	Zip() Filesystem
	// contains filtered or unexported methods
}

func New

func New(attr PathAttributer) (Filesystem, error)

func NewDir

func NewDir(attrs ...PathAttributer) Filesystem

func NewFile

func NewFile(attrs ...PathAttributer) Filesystem

type Operation

type Operation struct {
	Flag int
	Mode os.FileMode
}

func NewOperation

func NewOperation(attrs ...OperationAttributer) *Operation

func (*Operation) SetAttrs

func (my *Operation) SetAttrs(attrs ...OperationAttributer) *Operation

type OperationAttributer

type OperationAttributer interface{ Register(o *Operation) }

func Flag

func Flag(flag int) OperationAttributer

func Mode

func Mode(mode os.FileMode) OperationAttributer

type PathAttributer

type PathAttributer interface {
	Joins(paths ...string) PathAttributer
	Register(f Filesystem)
	GetPath() string
}

func Abs

func Abs(paths ...string) PathAttributer

func Auto

func Auto(paths ...string) PathAttributer

func Rel

func Rel(paths ...string) PathAttributer