From b675aceb33a80390e9e5fdd68cc7fc69e27687b6 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 11:35:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/ls.go | 47 ++++++++++++++++++++++++++++++ define/result.go | 2 +- execute.go | 11 +++---- go.mod | 2 +- go.sum | 0 ls.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 define/ls.go create mode 100644 go.sum create mode 100644 ls.go diff --git a/define/ls.go b/define/ls.go new file mode 100644 index 0000000..e7f9db4 --- /dev/null +++ b/define/ls.go @@ -0,0 +1,47 @@ +// Package define ... +// +// Description : define ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-05-20 23:09 +package define + +// LsFileInfo ls查询出来的文件详细信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 23:10 2022/5/20 +type LsFileInfo struct { + IsDir bool `json:"is_dir"` // 是否是目录 + IsLink bool `json:"is_link"` // 是否链接文件 + SourcePath string `json:"source_path"` // 是链接文件时, 链接的源文件路径 + IsHidden bool `json:"is_hidden"` // 是否是隐藏文件 + FullPath string `json:"full_path"` // 文件全路径 + RelativePath string `json:"relative_path"` // 相对路径 + Type string `json:"type"` // 文件类型, 如果为目录, 至为 - , 如果非 .xxx 结尾, 可执行为 bin , 不可执行为 unknown + Total int64 `json:"total"` // 文件总数 + Permission *FullPermission `json:"permission"` // 文件权限 +} + +// FullPermission 文件权限 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 23:45 2022/5/20 +type FullPermission struct { + User FilePermission `json:"user"` + Group FilePermission `json:"group"` + Other FilePermission `json:"other"` +} + +// FilePermission 文件操作权限 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 23:31 2022/5/20 +type FilePermission struct { + Read bool `json:"read"` + Write bool `json:"write"` + Exec bool `json:"exec"` +} diff --git a/define/result.go b/define/result.go index bc15520..44a6b68 100644 --- a/define/result.go +++ b/define/result.go @@ -15,7 +15,7 @@ package define type Result struct { WorkDir string // 执行的目录 CmdPath string // 命令的路径 - Err error // 异常信息 + Error error // 异常信息 Output []byte // 输出的内容 StartTime int64 // 开始时间 FinishTime int64 // 完成时间 diff --git a/execute.go b/execute.go index 311f7f1..7c5b1e6 100644 --- a/execute.go +++ b/execute.go @@ -1,4 +1,4 @@ -// Package linux ... +// Package command ... // // Description : command ... // @@ -8,10 +8,11 @@ package command import ( - "command/define" "errors" "os/exec" "time" + + "git.zhangdeman.cn/zhangdeman/command/define" ) // Execute 执行系统命令 @@ -23,7 +24,7 @@ func Execute(workDir string, command string, param []string) define.Result { //执行命令 result := define.Result{ WorkDir: workDir, - Err: nil, + Error: nil, Output: nil, StartTime: time.Now().UnixNano(), FinishTime: 0, @@ -31,12 +32,12 @@ func Execute(workDir string, command string, param []string) define.Result { defer func() { result.FinishTime = time.Now().UnixNano() }() - if result.CmdPath, result.Err = Check(command); nil != result.Err { + if result.CmdPath, result.Error = Check(command); nil != result.Error { return result } cmdInstance := exec.Command(command, param...) cmdInstance.Dir = workDir - result.Output, result.Err = cmdInstance.CombinedOutput() + result.Output, result.Error = cmdInstance.CombinedOutput() return result } diff --git a/go.mod b/go.mod index a5e7e3e..8904d5b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module command +module git.zhangdeman.cn/zhangdeman/command go 1.17 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/ls.go b/ls.go new file mode 100644 index 0000000..98bfd6a --- /dev/null +++ b/ls.go @@ -0,0 +1,74 @@ +// Package command ... +// +// Description : command ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-05-20 22:34 +package command + +import ( + "command/define" + "fmt" + "strings" +) + +// NewLs ls命令 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 22:36 2022/5/20 +func NewLs(workDir string) *ls { + return &ls{ + workDir: workDir, + paramList: []string{"-l"}, + } +} + +type ls struct { + workDir string + paramList []string + isRecursive bool // 是否递归查询 +} + +// ShowHideFile 显示隐藏文件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 22:38 2022/5/20 +func (l *ls) ShowHideFile() *ls { + l.paramList = append(l.paramList, "-a") + return l +} + +// Recursive 递归查看所有目录下的文件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 22:38 2022/5/20 +func (l *ls) Recursive() *ls { + l.paramList = append(l.paramList, "-R") + return l +} + +// Run 执行命令 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 22:59 2022/5/20 +func (l *ls) Run() (define.Result, []define.LsFileInfo) { + result := Execute(l.workDir, "ls", l.paramList) + if nil != result.Error { + return result, make([]define.LsFileInfo, 0) + } + resultStr := string(result.Output) + fileList := strings.Split(resultStr, "\t") + for _, item := range fileList { + if item == "." || item == ".." { + // 忽略 . 和 .. + continue + } + fmt.Println(item) + } + return result, nil +}