add file order & file direction option for file list

This commit is contained in:
tickstep
2022-02-13 22:32:21 +08:00
parent 9bffdc1cff
commit cc1fc526ef
3 changed files with 55 additions and 6 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ require (
github.com/oleiade/lane v0.0.0-20160817071224-3053869314bb
github.com/olekukonko/tablewriter v0.0.2-0.20190618033246-cc27d85e17ce
github.com/peterh/liner v1.2.1
github.com/tickstep/aliyunpan-api v0.0.4
github.com/tickstep/aliyunpan-api v0.0.5
github.com/tickstep/bolt v1.3.3
github.com/tickstep/library-go v0.0.5
github.com/urfave/cli v1.21.1-0.20190817182405-23c83030263f
+2
View File
@@ -65,6 +65,8 @@ github.com/tickstep/aliyunpan-api v0.0.3 h1:DBxRIjRUuebIjAgAPtS7rYZ4tafzOEF6J294
github.com/tickstep/aliyunpan-api v0.0.3/go.mod h1:EdciagdVeAHfqvzStVEVp1UVtySaq1DFd5RSucDfKWY=
github.com/tickstep/aliyunpan-api v0.0.4 h1:+ct8x6F6vpkezi3EMLb8zDBoWdQBA+BFuzWiYWf8q1s=
github.com/tickstep/aliyunpan-api v0.0.4/go.mod h1:EdciagdVeAHfqvzStVEVp1UVtySaq1DFd5RSucDfKWY=
github.com/tickstep/aliyunpan-api v0.0.5 h1:41eCJ1iGMtj2TXZGdrgv66RRcZpWpWq19yk8jOOpqdE=
github.com/tickstep/aliyunpan-api v0.0.5/go.mod h1:EdciagdVeAHfqvzStVEVp1UVtySaq1DFd5RSucDfKWY=
github.com/tickstep/bolt v1.3.3 h1:3abb88T5JJocnBlYTJE3EqKvwWV6+D/JZD2Fsdt1QHI=
github.com/tickstep/bolt v1.3.3/go.mod h1:Y24RwCywbOsKAyHiVcJ4K2QQfED+q67x7PEOH9OG7Q4=
github.com/tickstep/cloudpan189-api v0.0.3 h1:L8woe6XSUjQOsa7XRC7zH8VZj8SNKo/1qQb1k2ttLkU=
+52 -5
View File
@@ -73,9 +73,34 @@ func CmdLs() cli.Command {
return nil
}
var (
orderBy aliyunpan.FileOrderBy = aliyunpan.FileOrderByUpdatedAt
orderSort aliyunpan.FileOrderDirection = aliyunpan.FileOrderDirectionDesc
)
switch {
case c.IsSet("asc"):
orderSort = aliyunpan.FileOrderDirectionAsc
case c.IsSet("desc"):
orderSort = aliyunpan.FileOrderDirectionDesc
default:
orderSort = aliyunpan.FileOrderDirectionDesc
}
switch {
case c.IsSet("time"):
orderBy = aliyunpan.FileOrderByUpdatedAt
case c.IsSet("name"):
orderBy = aliyunpan.FileOrderByName
case c.IsSet("size"):
orderBy = aliyunpan.FileOrderBySize
default:
orderBy = aliyunpan.FileOrderByUpdatedAt
}
RunLs(parseDriveId(c), c.Args().Get(0), &LsOptions{
Total: c.Bool("l") || c.Parent().Args().Get(0) == "ll",
})
}, orderBy, orderSort)
return nil
},
@@ -85,15 +110,36 @@ func CmdLs() cli.Command {
Usage: "网盘ID",
Value: "",
},
cli.BoolFlag{
Name: "asc",
Usage: "升序排序",
},
cli.BoolFlag{
Name: "desc",
Usage: "降序排序",
},
cli.BoolFlag{
Name: "time",
Usage: "根据修改时间排序",
},
cli.BoolFlag{
Name: "name",
Usage: "根据文件名排序",
},
cli.BoolFlag{
Name: "size",
Usage: "根据大小排序",
},
},
}
}
func RunLs(driveId, targetPath string, lsOptions *LsOptions) {
func RunLs(driveId, targetPath string, lsOptions *LsOptions,
orderBy aliyunpan.FileOrderBy, orderDirection aliyunpan.FileOrderDirection) {
activeUser := config.Config.ActiveUser()
targetPath = activeUser.PathJoin(driveId, targetPath)
if targetPath[len(targetPath) - 1] == '/' {
targetPath = text.Substr(targetPath, 0, len(targetPath) - 1)
if targetPath[len(targetPath)-1] == '/' {
targetPath = text.Substr(targetPath, 0, len(targetPath)-1)
}
targetPathInfo, err := activeUser.PanClient().FileInfoByPath(driveId, targetPath)
@@ -106,6 +152,8 @@ func RunLs(driveId, targetPath string, lsOptions *LsOptions) {
fileListParam := &aliyunpan.FileListParam{}
fileListParam.ParentFileId = targetPathInfo.FileId
fileListParam.DriveId = driveId
fileListParam.OrderBy = orderBy
fileListParam.OrderDirection = orderDirection
if targetPathInfo.IsFolder() {
fileResult, err := activeUser.PanClient().FileListGetAll(fileListParam)
if err != nil {
@@ -119,7 +167,6 @@ func RunLs(driveId, targetPath string, lsOptions *LsOptions) {
renderTable(opLs, lsOptions.Total, targetPath, fileList)
}
func renderTable(op int, isTotal bool, path string, files aliyunpan.FileList) {
tb := cmdtable.NewTable(os.Stdout)
var (