// Package command ... // // Description : command ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2022-05-21 17:31 package command import ( "errors" "path/filepath" "strings" "git.zhangdeman.cn/zhangdeman/command/define" ) // Dir 目录操作实例 // // Author : go_developer@163.com<白茶清欢> // // Date : 17:33 2022/5/21 func Dir(workDir string) *dir { return &dir{ workDir: workDir, } } type dir struct { workDir string // 工作目录 recursiveCreate bool // 创建目录时,是否递归创建 } // RecursiveCreate 递归创建目录 // // Author : go_developer@163.com<白茶清欢> // // Date : 17:37 2022/5/21 func (d *dir) RecursiveCreate() *dir { d.recursiveCreate = true return d } // Create 创建目录, 若 dirPath 以 / 开头, 则为据对路径, 否则是相对 workDir 的相对路径 // // Author : go_developer@163.com<白茶清欢> // // Date : 17:42 2022/5/21 func (d *dir) Create(dirPath string) *define.Result { if !strings.HasPrefix(dirPath, "/") { dirPath = strings.ReplaceAll(d.workDir+"/"+dirPath, "//", "/") } paramList := make([]string, 0) if d.recursiveCreate { paramList = append(paramList, "-p") } paramList = append(paramList, dirPath) return Execute(d.workDir, "mkdir", paramList) } // Delete 删除目录 // // Author : go_developer@163.com<白茶清欢> // // Date : 20:12 2022/6/14 func (d *dir) Delete(dirPath string) *define.Result { if strings.HasPrefix(dirPath, string(filepath.Separator)) && !strings.HasPrefix(dirPath, d.workDir) { return &define.Result{ WorkDir: d.workDir, CmdPath: "", Param: nil, Error: errors.New("仅允许删除工作目录下的子目录"), Output: nil, StartTime: 0, FinishTime: 0, } } return Execute(d.workDir, "rm", []string{"-rf", dirPath}) }