完成文件基础信息填充

This commit is contained in:
白茶清欢 2022-06-27 21:34:01 +08:00
parent 8c3a8c5da2
commit 98203938f4
4 changed files with 46 additions and 1 deletions

View File

@ -19,9 +19,15 @@ type LsFileInfo struct {
IsHidden bool `json:"is_hidden"` // 是否是隐藏文件 IsHidden bool `json:"is_hidden"` // 是否是隐藏文件
FullPath string `json:"full_path"` // 文件全路径 FullPath string `json:"full_path"` // 文件全路径
RelativePath string `json:"relative_path"` // 相对路径 RelativePath string `json:"relative_path"` // 相对路径
Name string `json:"name"` // 文件名
Type string `json:"type"` // 文件类型, 如果为目录, 至为 - , 如果非 .xxx 结尾, 可执行为 bin , 不可执行为 unknown Type string `json:"type"` // 文件类型, 如果为目录, 至为 - , 如果非 .xxx 结尾, 可执行为 bin , 不可执行为 unknown
Permission *FullPermission `json:"permission"` // 文件权限 Permission *FullPermission `json:"permission"` // 文件权限
Dir string `json:"dir"` // 文件所属目录 Dir string `json:"dir"` // 文件所属目录
ReferenceCnt int `json:"reference_cnt"` // 引用次数
Size int64 `json:"size"` // 大小, bytes
User string `json:"user"` // 所属用户
Group string `json:"group"` // 所属用户组
ModifyTime string `json:"modify_time"` // 最近修改时间
} }
// FullPermission 文件权限 // FullPermission 文件权限

6
go.mod
View File

@ -3,3 +3,9 @@ module git.zhangdeman.cn/zhangdeman/command
go 1.17 go 1.17
require gopkg.in/yaml.v3 v3.0.1 require gopkg.in/yaml.v3 v3.0.1
require (
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220626081130-cbac0b676fb8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
)

6
go.sum
View File

@ -1,3 +1,9 @@
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220626081130-cbac0b676fb8 h1:HTa2y2Fsh9q48f29I/V2LhVQ5Pai55ULP1Qj3qwhrOE=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220626081130-cbac0b676fb8/go.mod h1:G2/OKMbEn89d+YUXQtv9Nlh0LGg14pOqDnbOgBTTRXY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

29
ls.go
View File

@ -10,6 +10,8 @@ package command
import ( import (
"strings" "strings"
"git.zhangdeman.cn/zhangdeman/util"
"git.zhangdeman.cn/zhangdeman/command/define" "git.zhangdeman.cn/zhangdeman/command/define"
) )
@ -74,6 +76,10 @@ func (l *ls) Run() (*define.Result, []define.LsFileInfo) {
// 过滤掉对于文件总数的输出 // 过滤掉对于文件总数的输出
continue continue
} }
if len(itemArr) < 9 {
// 设置目录 TODO
continue
}
fileListResult = append(fileListResult, l.parseFileInfo(item)) fileListResult = append(fileListResult, l.parseFileInfo(item))
} }
return result, fileListResult return result, fileListResult
@ -85,10 +91,31 @@ func (l *ls) Run() (*define.Result, []define.LsFileInfo) {
// //
// Date : 17:35 2022/6/27 // Date : 17:35 2022/6/27
func (l *ls) parseFileInfo(fileLine string) define.LsFileInfo { func (l *ls) parseFileInfo(fileLine string) define.LsFileInfo {
fileInfoArr := strings.Split(fileLine, " ") fileInfoArrSource := strings.Split(fileLine, " ")
fileInfoArr := make([]string, 0)
for _, item := range fileInfoArrSource {
if len(item) > 0 {
fileInfoArr = append(fileInfoArr, item)
}
}
fileInfo := define.LsFileInfo{} fileInfo := define.LsFileInfo{}
// 是否链接 / 是否目录 / 文件权限解析 // 是否链接 / 是否目录 / 文件权限解析
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])
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]) fileInfo.IsLink, fileInfo.IsDir, fileInfo.Permission = l.getFilePermission(fileInfoArr[0])
if fileInfo.IsLink {
fileInfo.SourcePath = fileInfoArr[0]
}
return fileInfo return fileInfo
} }