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