From 417388912036df99e540362f49d8acbd83c04137 Mon Sep 17 00:00:00 2001 From: xiaoyaofenfen <1254525673@qq.com> Date: Mon, 5 Dec 2022 16:56:17 +0800 Subject: [PATCH] add wildchar path match method --- internal/command/album.go | 2 +- internal/command/download.go | 2 +- internal/command/locate.go | 2 +- internal/command/utils.go | 48 +++++++++++++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/internal/command/album.go b/internal/command/album.go index e641ac4..110b825 100644 --- a/internal/command/album.go +++ b/internal/command/album.go @@ -429,7 +429,7 @@ func RunAlbumAddFile(albumName string, filePathList []string, filterOption Album return } - paths, err := matchPathByShellPattern(activeUser.ActiveDriveId, filePathList...) + paths, err := makePathAbsolute(activeUser.ActiveDriveId, filePathList...) if err != nil { fmt.Println(err) return diff --git a/internal/command/download.go b/internal/command/download.go index edeb1ca..3a607b3 100644 --- a/internal/command/download.go +++ b/internal/command/download.go @@ -255,7 +255,7 @@ func RunDownload(paths []string, options *DownloadOptions) { } } - paths, err := matchPathByShellPattern(options.DriveId, paths...) + paths, err := makePathAbsolute(options.DriveId, paths...) if err != nil { fmt.Println(err) return diff --git a/internal/command/locate.go b/internal/command/locate.go index 10816e1..f0a0f2f 100644 --- a/internal/command/locate.go +++ b/internal/command/locate.go @@ -81,7 +81,7 @@ func RunLocateUrl(driveId string, paths []string, saveFilePath string) { activeUser.PanClient().ClearCache() defer activeUser.PanClient().DisableCache() - paths, err := matchPathByShellPattern(driveId, paths...) + paths, err := makePathAbsolute(driveId, paths...) if err != nil { fmt.Println(err) return diff --git a/internal/command/utils.go b/internal/command/utils.go index 953d929..8f4d86a 100644 --- a/internal/command/utils.go +++ b/internal/command/utils.go @@ -14,6 +14,7 @@ package command import ( + "errors" "fmt" "github.com/tickstep/aliyunpan-api/aliyunpan" "github.com/tickstep/aliyunpan/internal/config" @@ -49,7 +50,52 @@ func GetFileInfoByPaths(paths ...string) (fileInfoList []*aliyunpan.FileEntity, return } -func matchPathByShellPattern(driveId string, patterns ...string) (panpaths []string, err error) { +// RunTestShellPattern 执行测试通配符 +func RunTestShellPattern(driveId string, pattern string) { + acUser := GetActiveUser() + files, err := acUser.PanClient().MatchPathByShellPattern(driveId, GetActiveUser().PathJoin(driveId, pattern)) + if err != nil { + fmt.Println(err) + return + } + for _, f := range *files { + fmt.Printf("%s\n", f.Path) + } + return +} + +// matchPathByShellPatternOnce 通配符匹配唯一结果,如果有多条则报错 +func matchPathByShellPatternOnce(driveId string, pattern *string) (*aliyunpan.FileEntity, error) { + acUser := GetActiveUser() + files, err := acUser.PanClient().MatchPathByShellPattern(driveId, GetActiveUser().PathJoin(driveId, *pattern)) + if err != nil { + return nil, err + } + switch files.ItemCount() { + case 0: + return nil, errors.New("未匹配到路径, 请检测通配符") + case 1: + return files.Item(0), nil + default: + return nil, errors.New("多条通配符匹配结果") + } +} + +// matchPathByShellPattern 通配符匹配路径,允许返回多个匹配结果 +func matchPathByShellPattern(driveId string, patterns ...string) (files []*aliyunpan.FileEntity, e error) { + acUser := GetActiveUser() + for k := range patterns { + ps, err := acUser.PanClient().MatchPathByShellPattern(driveId, acUser.PathJoin(driveId, patterns[k])) + if err != nil { + return nil, err + } + files = append(files, *ps...) + } + return files, nil +} + +// makePathAbsolute 拼接路径,确定返回路径为绝对路径 +func makePathAbsolute(driveId string, patterns ...string) (panpaths []string, err error) { acUser := GetActiveUser() for k := range patterns { ps := acUser.PathJoin(driveId, patterns[k])