From da134e32a121b815437fbe575130d799bbfdd096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 22 May 2022 13:45:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file.go | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 file.go diff --git a/file.go b/file.go new file mode 100644 index 0000000..28ea6dd --- /dev/null +++ b/file.go @@ -0,0 +1,145 @@ +// Package command ... +// +// Description : 文件相关操作 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-05-22 12:23 +package command + +import ( + "errors" + "os" + "strings" +) + +// File ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:25 2022/5/22 +func File(workDir string, filePath string) *fileOperate { + if len(workDir) == 0 { + workDir, _ = os.Getwd() + } + if !strings.HasPrefix(filePath, "/") { + filePath = strings.ReplaceAll(workDir+"/"+filePath, "//", "/") + } + return &fileOperate{ + workDir: workDir, + filePath: filePath, + permission: 0644, + openFlag: os.O_RDONLY, + } +} + +// file ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:24 2022/5/22 +type fileOperate struct { + workDir string + filePath string + autoCreate bool + permission os.FileMode + openFlag int + fileHandler *os.File +} + +// IsExist 文件是否存在 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:30 2022/5/22 +func (f *fileOperate) IsExist() (bool, bool, error) { + fileState, err := os.Stat(f.filePath) + if err != nil { + if os.IsNotExist(err) { + return false, false, nil + } + return false, false, err + } + return true, fileState.IsDir(), nil +} + +// AutoCreate 文件不存在时自动创建 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:28 2022/5/22 +func (f *fileOperate) AutoCreate() *fileOperate { + f.autoCreate = true + return f +} + +// Permission 设置文件权限 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 13:00 2022/5/22 +func (f *fileOperate) Permission(fileModel os.FileMode) *fileOperate { + f.permission = fileModel + return f +} + +// SetOpenFlag ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 13:40 2022/5/22 +func (f *fileOperate) SetOpenFlag(flagList ...int) *fileOperate { + for _, flag := range flagList { + f.openFlag = f.openFlag | flag + } + return f +} + +// Create 创建文件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 13:04 2022/5/22 +func (f *fileOperate) Create() error { + + var ( + fileExist bool + isDir bool + err error + ) + + if fileExist, isDir, err = f.IsExist(); nil != err { + return err + } + + if isDir { + return errors.New("路径已存在, 且为目录 : " + f.filePath) + } + + if !fileExist { + // 文件不存在 + if !f.autoCreate { + return errors.New("文件不存在, 且不允许自动创建 : " + f.filePath) + } + } + f.fileHandler, err = os.OpenFile(f.filePath, f.openFlag, f.permission) + return err +} + +// Open 打开文件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 13:22 2022/5/22 +func (f *fileOperate) Open() error { + return f.Create() +} + +// GetFileHandler 获取文件处理句柄 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 13:23 2022/5/22 +func (f *fileOperate) GetFileHandler() *os.File { + return f.fileHandler +}