From 3439ca5fde231f1b8968fdf256fb0635fa8802a2 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Fri, 5 Jun 2015 21:49:33 -0700 Subject: [PATCH] handle recursive prefix properly --- cmd-ls.go | 12 +++++++++--- ls.go | 9 +++++++-- ls_test.go | 8 ++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd-ls.go b/cmd-ls.go index 3a764e3f..d48905ed 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -19,6 +19,8 @@ package main import ( "errors" "fmt" + "path/filepath" + "strings" "github.com/minio/cli" "github.com/minio/mc/pkg/console" @@ -126,7 +128,11 @@ func runListCmd(ctx *cli.Context) { for targetURL, targetConfig := range targetURLConfigMap { // if recursive strip off the "..." newTargetURL := stripRecursiveURL(targetURL) - err = doListCmd(newTargetURL, targetConfig, isURLRecursive(targetURL)) + recursivePrefix := "" // is set when ... includes a directory, i.e. /path/to/dir... instead of path/to/dir/... + if isURLRecursive(targetURL) && !strings.HasSuffix(newTargetURL, string(filepath.Separator)) { + recursivePrefix = filepath.Base(newTargetURL) + } + err = doListCmd(newTargetURL, targetConfig, isURLRecursive(targetURL), recursivePrefix) if err != nil { console.Fatals(ErrorMessage{ Message: fmt.Sprintf("Failed to list ā€˜%s’", targetURL), @@ -137,12 +143,12 @@ func runListCmd(ctx *cli.Context) { } // doListCmd - -func doListCmd(targetURL string, targetConfig *hostConfig, recursive bool) error { +func doListCmd(targetURL string, targetConfig *hostConfig, recursive bool, recursivePrefix string) error { clnt, err := getNewClient(targetURL, targetConfig) if err != nil { return iodine.New(err, map[string]string{"Target": targetURL}) } - err = doList(clnt, targetURL, recursive) + err = doList(clnt, targetURL, recursive, recursivePrefix) if err != nil { return iodine.New(err, nil) } diff --git a/ls.go b/ls.go index 09f09117..534651ad 100644 --- a/ls.go +++ b/ls.go @@ -18,6 +18,7 @@ package main import ( "fmt" + "path/filepath" "runtime" "strings" @@ -67,7 +68,7 @@ func parseContent(c *client.Content) Content { } // doList - list all entities inside a folder -func doList(clnt client.Client, targetURL string, recursive bool) error { +func doList(clnt client.Client, targetURL string, recursive bool, recursivePrefix string) error { var err error for contentCh := range clnt.List(recursive) { if contentCh.Err != nil { @@ -80,7 +81,11 @@ func doList(clnt client.Client, targetURL string, recursive bool) error { // a user would not wish to see a URL just for recursive and not for regular List() // // To be consistent we have to filter them out - contentName = strings.TrimPrefix(contentName, strings.TrimSuffix(targetURL, "/")+"/") + contentName = strings.TrimPrefix(contentName, + strings.TrimSuffix(targetURL, string(filepath.Separator))+string(filepath.Separator)) + if recursivePrefix != "" { + contentName = recursivePrefix + string(filepath.Separator) + contentName + } } contentCh.Content.Name = contentName console.Println(parseContent(contentCh.Content)) diff --git a/ls_test.go b/ls_test.go index 1249ad0b..d549f0ed 100644 --- a/ls_test.go +++ b/ls_test.go @@ -40,10 +40,10 @@ func (s *CmdTestSuite) TestLSCmd(c *C) { c.Assert(err, IsNil) } - err = doListCmd(root, &hostConfig{}, false) + err = doListCmd(root, &hostConfig{}, false, "") c.Assert(err, IsNil) - err = doListCmd(root, &hostConfig{}, true) + err = doListCmd(root, &hostConfig{}, true, "test") c.Assert(err, IsNil) for i := 0; i < 10; i++ { @@ -53,10 +53,10 @@ func (s *CmdTestSuite) TestLSCmd(c *C) { err := putTarget(objectPath, &hostConfig{}, int64(dataLen), bytes.NewReader([]byte(data))) c.Assert(err, IsNil) } - err = doListCmd(server.URL+"/bucket", &hostConfig{}, false) + err = doListCmd(server.URL+"/bucket", &hostConfig{}, false, "") c.Assert(err, IsNil) - err = doListCmd(server.URL+"/bucket", &hostConfig{}, true) + err = doListCmd(server.URL+"/bucket", &hostConfig{}, true, "test") c.Assert(err, IsNil) }