From 666a0dbab6378601f290fe9b4981192c28d48bed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 21 May 2022 18:00:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dir.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 dir.go diff --git a/dir.go b/dir.go new file mode 100644 index 0000000..5c00f2a --- /dev/null +++ b/dir.go @@ -0,0 +1,57 @@ +// 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 +func (d *dir) Create(dirPath string) define.Result { + if !strings.HasPrefix(dirPath, "/") { + dirPath = strings.ReplaceAll(d.workDir, "//", "/") + } + paramList := make([]string, 0) + if d.recursiveCreate { + paramList = append(paramList, "-p") + } + paramList = append(paramList, dirPath) + return Execute(d.workDir, "mkdir", paramList) +}