From cc1fc526ef41ffbd34eacffde23dabbb0554abc5 Mon Sep 17 00:00:00 2001 From: tickstep Date: Sun, 13 Feb 2022 22:32:21 +0800 Subject: [PATCH] add file order & file direction option for file list --- go.mod | 2 +- go.sum | 2 ++ internal/command/ls_search.go | 57 ++++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 03f027d..86ca53d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e860be3..3859d10 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/command/ls_search.go b/internal/command/ls_search.go index ffa7ce2..d13cbd2 100644 --- a/internal/command/ls_search.go +++ b/internal/command/ls_search.go @@ -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 (