From a15f70467265a7f089320533d57d08a101482484 Mon Sep 17 00:00:00 2001 From: xiaoyaofenfen <1254525673@qq.com> Date: Mon, 5 Dec 2022 17:14:03 +0800 Subject: [PATCH] cd command support wildcard patterns --- internal/command/cd.go | 31 ++++++++++++++++++++++++++----- internal/command/utils.go | 4 ++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/internal/command/cd.go b/internal/command/cd.go index 0e71038..ffa457c 100644 --- a/internal/command/cd.go +++ b/internal/command/cd.go @@ -15,6 +15,7 @@ package command import ( "fmt" + "github.com/tickstep/aliyunpan-api/aliyunpan" "github.com/tickstep/aliyunpan/cmder" "github.com/tickstep/aliyunpan/internal/config" "github.com/urfave/cli" @@ -33,6 +34,9 @@ func CmdCd() cli.Command { 切换 /我的资源 工作目录: aliyunpan cd /我的资源 + 切换 /我的资源 工作目录,使用通配符: + aliyunpan cd /我的* + 切换上级目录: aliyunpan cd .. @@ -90,24 +94,41 @@ func RunChangeDirectory(driveId, targetPath string) { user := config.Config.ActiveUser() targetPath = user.PathJoin(driveId, targetPath) - targetPathInfo, err := user.PanClient().FileInfoByPath(driveId, targetPath) + //targetPathInfo, err := user.PanClient().FileInfoByPath(driveId, targetPath) + files, err := matchPathByShellPattern(driveId, targetPath) if err != nil { fmt.Println(err) 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 !targetPathInfo.IsFolder() { - fmt.Printf("错误: %s 不是一个目录 (文件夹)\n", targetPath) + fmt.Printf("错误: %s 不是一个目录 (文件夹)\n", targetPathInfo.Path) return } if user.IsFileDriveActive() { - user.Workdir = targetPath + user.Workdir = targetPathInfo.Path user.WorkdirFileEntity = *targetPathInfo } else if user.IsAlbumDriveActive() { - user.AlbumWorkdir = targetPath + user.AlbumWorkdir = targetPathInfo.Path user.AlbumWorkdirFileEntity = *targetPathInfo } - fmt.Printf("改变工作目录: %s\n", targetPath) + fmt.Printf("改变工作目录: %s\n", targetPathInfo.Path) } diff --git a/internal/command/utils.go b/internal/command/utils.go index 8f4d86a..185e382 100644 --- a/internal/command/utils.go +++ b/internal/command/utils.go @@ -65,9 +65,9 @@ func RunTestShellPattern(driveId string, pattern string) { } // matchPathByShellPatternOnce 通配符匹配唯一结果,如果有多条则报错 -func matchPathByShellPatternOnce(driveId string, pattern *string) (*aliyunpan.FileEntity, error) { +func matchPathByShellPatternOnce(driveId string, pattern string) (*aliyunpan.FileEntity, error) { acUser := GetActiveUser() - files, err := acUser.PanClient().MatchPathByShellPattern(driveId, GetActiveUser().PathJoin(driveId, *pattern)) + files, err := acUser.PanClient().MatchPathByShellPattern(driveId, GetActiveUser().PathJoin(driveId, pattern)) if err != nil { return nil, err }