fix: 调整网元日志文件列表返回信息

This commit is contained in:
TsMask
2023-12-13 21:14:28 +08:00
parent 738ff3b6f5
commit 4340572421
2 changed files with 33 additions and 12 deletions

View File

@@ -6,11 +6,13 @@ import (
"ems.agt/src/framework/cmd" "ems.agt/src/framework/cmd"
"ems.agt/src/framework/config" "ems.agt/src/framework/config"
"ems.agt/src/framework/logger"
"ems.agt/src/framework/utils/parse" "ems.agt/src/framework/utils/parse"
) )
// FileListRow 文件列表行数据 // FileListRow 文件列表行数据
type FileListRow struct { type FileListRow struct {
FileType string `json:"fileType"` // 文件类型
FileMode string `json:"fileMode"` // 文件的权限 FileMode string `json:"fileMode"` // 文件的权限
LinkCount int64 `json:"linkCount"` // 硬链接数目 LinkCount int64 `json:"linkCount"` // 硬链接数目
Owner string `json:"owner"` // 所属用户 Owner string `json:"owner"` // 所属用户
@@ -26,7 +28,7 @@ type FileListRow struct {
// //
// return 目录大小,行记录,异常 // return 目录大小,行记录,异常
func FileList(path, neIp, search string) (string, []FileListRow, error) { func FileList(path, neIp, search string) (string, []FileListRow, error) {
total := "" totalSize := ""
var rows []FileListRow var rows []FileListRow
rowStr := "" rowStr := ""
@@ -44,13 +46,15 @@ func FileList(path, neIp, search string) (string, []FileListRow, error) {
sshHost := fmt.Sprintf("%s@%s", usernameNe, neIp) sshHost := fmt.Sprintf("%s@%s", usernameNe, neIp)
resultStr, err := cmd.ExecWithCheck("ssh", sshHost, pathStr, cmdStr) resultStr, err := cmd.ExecWithCheck("ssh", sshHost, pathStr, cmdStr)
if err != nil { if err != nil {
return total, rows, err logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error())
return totalSize, rows, err
} }
rowStr = resultStr rowStr = resultStr
} else { } else {
resultStr, err := cmd.Execf(pathStr, cmdStr) resultStr, err := cmd.Execf(pathStr, cmdStr)
if err != nil { if err != nil {
return total, rows, err logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error())
return totalSize, rows, err
} }
rowStr = resultStr rowStr = resultStr
} }
@@ -64,13 +68,24 @@ func FileList(path, neIp, search string) (string, []FileListRow, error) {
// 使用空格对字符串进行切割 // 使用空格对字符串进行切割
fields := strings.Fields(rowStr) fields := strings.Fields(rowStr)
if i == 0 { if i == 0 && searchStr == "" {
total = fields[1] totalSize = fields[1]
continue continue
} }
// 文件类型
fileMode := fields[0]
fileType := "file"
if fileMode[0] == 'd' {
fileType = "dir"
} else if fileMode[0] == 'l' {
fileType = "symlink"
}
// 提取各个字段的值 // 提取各个字段的值
rows = append(rows, FileListRow{ rows = append(rows, FileListRow{
FileMode: fields[0], FileMode: fileMode,
FileType: fileType,
LinkCount: parse.Number(fields[1]), LinkCount: parse.Number(fields[1]),
Owner: fields[2], Owner: fields[2],
Group: fields[3], Group: fields[3],
@@ -79,5 +94,5 @@ func FileList(path, neIp, search string) (string, []FileListRow, error) {
FileName: fields[6], FileName: fields[6],
}) })
} }
return total, rows, nil return totalSize, rows, nil
} }

View File

@@ -125,9 +125,14 @@ func (s *NeActionController) Files(c *gin.Context) {
return return
} }
total, rows, err := ssh.FileList(querys.Path, neInfo.IP, querys.Search) totalSize, rows, err := ssh.FileList(querys.Path, neInfo.IP, querys.Search)
if err != nil { if err != nil {
c.JSON(200, result.ErrMsg(err.Error())) c.JSON(200, result.Ok(map[string]any{
"path": querys.Path,
"totalSize": totalSize,
"total": len(rows),
"rows": []ssh.FileListRow{},
}))
return return
} }
@@ -145,8 +150,9 @@ func (s *NeActionController) Files(c *gin.Context) {
} }
c.JSON(200, result.Ok(map[string]any{ c.JSON(200, result.Ok(map[string]any{
"path": querys.Path, "path": querys.Path,
"total": total, "totalSize": totalSize,
"rows": splitRows, "total": lenNum,
"rows": splitRows,
})) }))
} }