command/dir.go

58 lines
1.2 KiB
Go
Raw Normal View History

2022-05-21 18:00:13 +08:00
// Package command ...
//
// Description : command ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-05-21 17:31
package command
import (
"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
2022-05-21 19:56:30 +08:00
func (d *dir) Create(dirPath string) *define.Result {
2022-05-21 18:00:13 +08:00
if !strings.HasPrefix(dirPath, "/") {
2022-05-21 20:01:58 +08:00
dirPath = strings.ReplaceAll(d.workDir+"/"+dirPath, "//", "/")
2022-05-21 18:00:13 +08:00
}
paramList := make([]string, 0)
if d.recursiveCreate {
paramList = append(paramList, "-p")
}
paramList = append(paramList, dirPath)
return Execute(d.workDir, "mkdir", paramList)
}