tree command support wildcard pattern

This commit is contained in:
xiaoyaofenfen 2022-12-05 20:19:52 +08:00
parent fb24f703c2
commit 36a5eed5af

View File

@ -73,11 +73,32 @@ func getTree(driveId, pathStr string, depth int, statistic *treeStatistic, showF
pathStr = activeUser.PathJoin(driveId, pathStr) pathStr = activeUser.PathJoin(driveId, pathStr)
pathStr = path.Clean(pathStr) pathStr = path.Clean(pathStr)
targetPathInfo, err1 := activeUser.PanClient().FileInfoByPath(driveId, pathStr) files, err := matchPathByShellPattern(driveId, pathStr)
if err1 != nil { if err != nil {
fmt.Println(err1) fmt.Println(err)
return return
} }
var targetPathInfo *aliyunpan.FileEntity
if len(files) == 1 {
targetPathInfo = files[0]
} else {
for _, f := range files {
if f.IsFolder() {
targetPathInfo = f
break
}
}
}
if targetPathInfo == nil {
fmt.Println("路径不存在")
return
}
if depth == 0 {
fmt.Printf("%s\n", targetPathInfo.Path)
}
fileList := aliyunpan.FileList{} fileList := aliyunpan.FileList{}
fileListParam := &aliyunpan.FileListParam{} fileListParam := &aliyunpan.FileListParam{}
fileListParam.ParentFileId = targetPathInfo.FileId fileListParam.ParentFileId = targetPathInfo.FileId
@ -85,7 +106,7 @@ func getTree(driveId, pathStr string, depth int, statistic *treeStatistic, showF
fileListParam.OrderBy = aliyunpan.FileOrderByName fileListParam.OrderBy = aliyunpan.FileOrderByName
fileListParam.OrderDirection = aliyunpan.FileOrderDirectionAsc fileListParam.OrderDirection = aliyunpan.FileOrderDirectionAsc
if targetPathInfo.IsFolder() { if targetPathInfo.IsFolder() {
fileResult, err := activeUser.PanClient().FileListGetAll(fileListParam, 0) fileResult, err := activeUser.PanClient().FileListGetAll(fileListParam, 100)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
@ -104,11 +125,11 @@ func getTree(driveId, pathStr string, depth int, statistic *treeStatistic, showF
if file.IsFolder() { if file.IsFolder() {
statistic.CountOfDir += 1 statistic.CountOfDir += 1
if showFullPath { if showFullPath {
fmt.Printf("%v%v %v/ -> %s\n", indentPrefixStr, pathPrefix, file.FileName, pathStr+"/"+file.FileName) fmt.Printf("%v%v %v/ -> %s\n", indentPrefixStr, pathPrefix, file.FileName, targetPathInfo.Path+"/"+file.FileName)
} else { } else {
fmt.Printf("%v%v %v/\n", indentPrefixStr, pathPrefix, file.FileName) fmt.Printf("%v%v %v/\n", indentPrefixStr, pathPrefix, file.FileName)
} }
getTree(driveId, pathStr+"/"+file.Path, depth+1, statistic, showFullPath) getTree(driveId, targetPathInfo.Path+"/"+file.Path, depth+1, statistic, showFullPath)
continue continue
} }
statistic.CountOfFile += 1 statistic.CountOfFile += 1
@ -119,7 +140,7 @@ func getTree(driveId, pathStr string, depth int, statistic *treeStatistic, showF
} }
if showFullPath { if showFullPath {
fmt.Printf("%v%v %v -> %s\n", indentPrefixStr, prefix, file.FileName, pathStr+"/"+file.FileName) fmt.Printf("%v%v %v -> %s\n", indentPrefixStr, prefix, file.FileName, targetPathInfo.Path+"/"+file.FileName)
} else { } else {
fmt.Printf("%v%v %v\n", indentPrefixStr, prefix, file.FileName) fmt.Printf("%v%v %v\n", indentPrefixStr, prefix, file.FileName)
} }
@ -140,7 +161,6 @@ func RunTree(driveId, pathStr string, showFullPath bool) {
CountOfFile: 0, CountOfFile: 0,
SizeOfFile: 0, SizeOfFile: 0,
} }
fmt.Printf("%s\n", pathStr)
getTree(driveId, pathStr, 0, statistic, showFullPath) getTree(driveId, pathStr, 0, statistic, showFullPath)
fmt.Printf("\n%d 个文件夹, %d 个文件, %s 总大小\n", statistic.CountOfDir, statistic.CountOfFile, converter.ConvertFileSize(statistic.SizeOfFile, 2)) fmt.Printf("\n%d 个文件夹, %d 个文件, %s 总大小\n", statistic.CountOfDir, statistic.CountOfFile, converter.ConvertFileSize(statistic.SizeOfFile, 2))
} }