handle recursive prefix properly

This commit is contained in:
Anand Babu (AB) Periasamy
2015-06-05 21:49:33 -07:00
parent dda0b75a97
commit 3439ca5fde
3 changed files with 20 additions and 9 deletions
+9 -3
View File
@@ -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)
}
+7 -2
View File
@@ -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))
+4 -4
View File
@@ -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)
}