文件列表,填充路径信息

This commit is contained in:
白茶清欢 2022-06-27 21:57:02 +08:00
parent 98203938f4
commit 803e3fc0ab

21
ls.go
View File

@ -8,6 +8,7 @@
package command package command
import ( import (
"path/filepath"
"strings" "strings"
"git.zhangdeman.cn/zhangdeman/util" "git.zhangdeman.cn/zhangdeman/util"
@ -24,6 +25,7 @@ func NewLs(workDir string) *ls {
return &ls{ return &ls{
workDir: workDir, workDir: workDir,
paramList: []string{"-l"}, paramList: []string{"-l"},
relativeDir: "." + string(filepath.Separator),
} }
} }
@ -31,6 +33,7 @@ type ls struct {
workDir string workDir string
paramList []string paramList []string
isRecursive bool // 是否递归查询 isRecursive bool // 是否递归查询
relativeDir string // 相对目录
} }
// ShowHideFile 显示隐藏文件 // ShowHideFile 显示隐藏文件
@ -77,7 +80,10 @@ func (l *ls) Run() (*define.Result, []define.LsFileInfo) {
continue continue
} }
if len(itemArr) < 9 { if len(itemArr) < 9 {
// 设置目录 TODO // 设置目录
if strings.HasPrefix(item, "."+string(filepath.Separator)) && strings.HasSuffix(item, ":") {
l.relativeDir = strings.TrimRight(item, ":")
}
continue continue
} }
fileListResult = append(fileListResult, l.parseFileInfo(item)) fileListResult = append(fileListResult, l.parseFileInfo(item))
@ -99,6 +105,7 @@ func (l *ls) parseFileInfo(fileLine string) define.LsFileInfo {
} }
} }
fileInfo := define.LsFileInfo{} fileInfo := define.LsFileInfo{}
fileInfo.IsLink, fileInfo.IsDir, fileInfo.Permission = l.getFilePermission(fileInfoArr[0])
// 是否链接 / 是否目录 / 文件权限解析 // 是否链接 / 是否目录 / 文件权限解析
fileInfo.Name = fileInfoArr[8] fileInfo.Name = fileInfoArr[8]
fileInfo.User = fileInfoArr[2] fileInfo.User = fileInfoArr[2]
@ -106,16 +113,22 @@ func (l *ls) parseFileInfo(fileLine string) define.LsFileInfo {
fileInfo.ModifyTime = strings.Join(fileInfoArr[5:8], " ") fileInfo.ModifyTime = strings.Join(fileInfoArr[5:8], " ")
_ = util.ConvertAssign(&fileInfo.Size, fileInfoArr[4]) _ = util.ConvertAssign(&fileInfo.Size, fileInfoArr[4])
_ = util.ConvertAssign(&fileInfo.ReferenceCnt, fileInfoArr[1]) _ = util.ConvertAssign(&fileInfo.ReferenceCnt, fileInfoArr[1])
if fileInfo.IsDir {
fileInfo.Type = "-"
} else {
nameArr := strings.Split(fileInfo.Name, ".") nameArr := strings.Split(fileInfo.Name, ".")
if len(nameArr) < 2 { if len(nameArr) < 2 {
fileInfo.Type = "unknown" fileInfo.Type = "unknown"
} else { } else {
fileInfo.Type = nameArr[len(nameArr)-1] fileInfo.Type = nameArr[len(nameArr)-1]
} }
fileInfo.IsLink, fileInfo.IsDir, fileInfo.Permission = l.getFilePermission(fileInfoArr[0])
if fileInfo.IsLink {
fileInfo.SourcePath = fileInfoArr[0]
} }
if fileInfo.IsLink {
fileInfo.SourcePath = fileInfoArr[10]
}
fileInfo.RelativePath = l.relativeDir
fileInfo.FullPath = strings.ReplaceAll(l.workDir+string(filepath.Separator)+l.relativeDir, string(filepath.Separator)+"."+string(filepath.Separator), string(filepath.Separator))
return fileInfo return fileInfo
} }