command/ls.go

163 lines
4.2 KiB
Go
Raw Normal View History

2022-05-21 11:35:01 +08:00
// Package command ...
//
// Description : command ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-05-20 22:34
package command
import (
2022-06-27 21:57:02 +08:00
"path/filepath"
2022-05-21 11:35:01 +08:00
"strings"
2022-05-21 18:06:56 +08:00
2022-06-27 21:34:01 +08:00
"git.zhangdeman.cn/zhangdeman/util"
2022-05-21 18:06:56 +08:00
"git.zhangdeman.cn/zhangdeman/command/define"
2022-05-21 11:35:01 +08:00
)
// NewLs ls命令
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:36 2022/5/20
func NewLs(workDir string) *ls {
return &ls{
2022-06-27 21:57:02 +08:00
workDir: workDir,
paramList: []string{"-l"},
relativeDir: "." + string(filepath.Separator),
2022-05-21 11:35:01 +08:00
}
}
type ls struct {
workDir string
paramList []string
2022-06-27 21:57:02 +08:00
isRecursive bool // 是否递归查询
relativeDir string // 相对目录
2022-05-21 11:35:01 +08:00
}
// 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
2022-05-21 19:57:27 +08:00
func (l *ls) Run() (*define.Result, []define.LsFileInfo) {
2022-05-21 11:35:01 +08:00
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, "\n")
2022-06-27 18:07:40 +08:00
fileListResult := make([]define.LsFileInfo, 0)
2022-05-21 11:35:01 +08:00
for _, item := range fileList {
if item == "." || item == ".." || len(item) == 0 {
2022-05-21 11:35:01 +08:00
// 忽略 . 和 ..
continue
}
itemArr := strings.Split(item, " ")
if strings.ToLower(itemArr[0]) == "total" {
// 过滤掉对于文件总数的输出
continue
}
2022-06-27 21:34:01 +08:00
if len(itemArr) < 9 {
2022-06-27 21:57:02 +08:00
// 设置目录
if strings.HasPrefix(item, "."+string(filepath.Separator)) && strings.HasSuffix(item, ":") {
l.relativeDir = strings.TrimRight(item, ":")
}
2022-06-27 21:34:01 +08:00
continue
}
2022-06-27 18:07:40 +08:00
fileListResult = append(fileListResult, l.parseFileInfo(item))
2022-05-21 11:35:01 +08:00
}
2022-06-27 18:07:40 +08:00
return result, fileListResult
2022-05-21 11:35:01 +08:00
}
// parseFileInfo 解析文件信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:35 2022/6/27
2022-06-27 18:07:40 +08:00
func (l *ls) parseFileInfo(fileLine string) define.LsFileInfo {
2022-06-27 21:34:01 +08:00
fileInfoArrSource := strings.Split(fileLine, " ")
fileInfoArr := make([]string, 0)
for _, item := range fileInfoArrSource {
if len(item) > 0 {
fileInfoArr = append(fileInfoArr, item)
}
}
2022-06-27 18:07:40 +08:00
fileInfo := define.LsFileInfo{}
2022-06-27 21:57:02 +08:00
fileInfo.IsLink, fileInfo.IsDir, fileInfo.Permission = l.getFilePermission(fileInfoArr[0])
2022-06-27 18:07:40 +08:00
// 是否链接 / 是否目录 / 文件权限解析
2022-06-27 21:34:01 +08:00
fileInfo.Name = fileInfoArr[8]
fileInfo.User = fileInfoArr[2]
fileInfo.Group = fileInfoArr[3]
fileInfo.ModifyTime = strings.Join(fileInfoArr[5:8], " ")
_ = util.ConvertAssign(&fileInfo.Size, fileInfoArr[4])
_ = util.ConvertAssign(&fileInfo.ReferenceCnt, fileInfoArr[1])
2022-06-27 21:57:02 +08:00
if fileInfo.IsDir {
fileInfo.Type = "-"
2022-06-27 21:34:01 +08:00
} else {
2022-06-27 21:57:02 +08:00
nameArr := strings.Split(fileInfo.Name, ".")
if len(nameArr) < 2 {
fileInfo.Type = "unknown"
} else {
fileInfo.Type = nameArr[len(nameArr)-1]
}
2022-06-27 21:34:01 +08:00
}
2022-06-27 21:57:02 +08:00
2022-06-27 21:34:01 +08:00
if fileInfo.IsLink {
2022-06-27 21:57:02 +08:00
fileInfo.SourcePath = fileInfoArr[10]
2022-06-27 21:34:01 +08:00
}
2022-06-27 21:57:02 +08:00
fileInfo.RelativePath = l.relativeDir
fileInfo.FullPath = strings.ReplaceAll(l.workDir+string(filepath.Separator)+l.relativeDir, string(filepath.Separator)+"."+string(filepath.Separator), string(filepath.Separator))
2022-06-27 18:07:40 +08:00
return fileInfo
}
2022-06-27 17:46:21 +08:00
// getFilePermission 解析文件权限信息 , 根据 drwxrwxrwx 这个规范解析
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:37 2022/6/27
func (l *ls) getFilePermission(permissionLayout string) (bool, bool, *define.FullPermission) {
permissionArr := strings.Split(permissionLayout, "")
if len(permissionArr) < 10 {
return false, false, nil
}
return permissionArr[0] == "l", permissionArr[0] == "d", &define.FullPermission{
User: define.FilePermission{
Read: permissionArr[1] == "r",
Write: permissionArr[2] == "w",
Exec: permissionArr[3] == "x",
},
Group: define.FilePermission{
Read: permissionArr[4] == "r",
Write: permissionArr[5] == "w",
Exec: permissionArr[6] == "x",
},
Other: define.FilePermission{
Read: permissionArr[7] == "r",
Write: permissionArr[8] == "w",
Exec: permissionArr[9] == "x",
},
}
}