From dad28fbe16cb1167616010032acbf7eb82acdd3d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 15 Sep 2015 22:34:01 -0700 Subject: [PATCH] ls modifications - always print for multiple arguments files first and directories second --- ls-main.go | 47 +++++++++++++++++++++++++++++++++++++++++++---- ls.go | 13 ++----------- ls_test.go | 8 ++++---- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/ls-main.go b/ls-main.go index 75a15a0d..04799d50 100644 --- a/ls-main.go +++ b/ls-main.go @@ -17,10 +17,12 @@ package main import ( + "fmt" "strings" "github.com/fatih/color" "github.com/minio/cli" + "github.com/minio/mc/pkg/client" "github.com/minio/mc/pkg/console" ) @@ -107,13 +109,50 @@ func mainList(ctx *cli.Context) { targetURLs, err := args2URLs(args) fatalIf(err.Trace(args...), "One or more unknown URL types passed.") - var lsPrefixMode = len(targetURLs) > 1 + if len(targetURLs) == 1 { + // if recursive strip off the "..." + var clnt client.Client + clnt, err = target2Client(stripRecursiveURL(targetURLs[0])) + fatalIf(err.Trace(targetURLs[0]), "Unable to initialize target ‘"+targetURLs[0]+"’.") + + err = doList(clnt, isURLRecursive(targetURLs[0])) + fatalIf(err.Trace(targetURLs[0]), "Unable to list target ‘"+targetURLs[0]+"’.") + return + } + var newTargetFiles []client.Client + newTargetFolders := make(map[client.Client]bool) for _, targetURL := range targetURLs { // if recursive strip off the "..." - clnt, err := target2Client(stripRecursiveURL(targetURL)) + var clnt client.Client + clnt, err = target2Client(stripRecursiveURL(targetURL)) fatalIf(err.Trace(targetURL), "Unable to initialize target ‘"+targetURL+"’.") - err = doList(clnt, isURLRecursive(targetURL), lsPrefixMode) - fatalIf(err.Trace(targetURL), "Unable to list target ‘"+targetURL+"’.") + var content *client.Content + content, err = clnt.Stat() + fatalIf(err.Trace(targetURL), "Unable to stat target ‘"+targetURL+"’.") + + // Following code is an attempt to always order files + // first than directories when there are more than one + // command line arguments for 'ls' + if content.Type.IsDir() { + newTargetFolders[clnt] = isURLRecursive(targetURL) + } + if content.Type.IsRegular() { + newTargetFiles = append(newTargetFiles, clnt) + } } + for _, targetClnt := range newTargetFiles { + err = doList(targetClnt, false) + fatalIf(err.Trace(targetClnt.URL().String()), "Unable to list target ‘"+targetClnt.URL().String()+"’.") + } + for targetClnt, recursive := range newTargetFolders { + if len(newTargetFiles) > 0 { + console.Println(console.Colorize("Dir", fmt.Sprintf("\n%s:", targetClnt.URL().String()))) + } else { + console.Println(console.Colorize("Dir", fmt.Sprintf("%s:", targetClnt.URL().String()))) + } + err = doList(targetClnt, recursive) + fatalIf(err.Trace(targetClnt.URL().String()), "Unable to list target ‘"+targetClnt.URL().String()+"’.") + } + return } diff --git a/ls.go b/ls.go index 9ddb9859..dc98ee66 100644 --- a/ls.go +++ b/ls.go @@ -103,14 +103,8 @@ func parseContent(c *client.Content) ContentMessage { } // doList - list all entities inside a folder. -func doList(clnt client.Client, recursive, lsPrefixMode bool) *probe.Error { - parentContent, err := clnt.Stat() - if err != nil { - return err.Trace(clnt.URL().String()) - } - if parentContent.Type.IsDir() && lsPrefixMode { - console.Println(console.Colorize("Dir", fmt.Sprintf("%s:", clnt.URL().String()))) - } +func doList(clnt client.Client, recursive bool) *probe.Error { + var err *probe.Error for contentCh := range clnt.List(recursive) { if contentCh.Err != nil { switch contentCh.Err.ToGoError().(type) { @@ -131,9 +125,6 @@ func doList(clnt client.Client, recursive, lsPrefixMode bool) *probe.Error { err = contentCh.Err.Trace() break } - if parentContent.Type.IsRegular() && lsPrefixMode { - console.Println("") - } Prints("%s\n", parseContent(contentCh.Content)) } if err != nil { diff --git a/ls_test.go b/ls_test.go index 10c7dd2f..a3566a37 100644 --- a/ls_test.go +++ b/ls_test.go @@ -49,10 +49,10 @@ func (s *TestSuite) TestLS(c *C) { clnt, perr = target2Client(root) c.Assert(perr, IsNil) - perr = doList(clnt, false, false) + perr = doList(clnt, false) c.Assert(perr, IsNil) - perr = doList(clnt, true, false) + perr = doList(clnt, true) c.Assert(perr, IsNil) for i := 0; i < 10; i++ { @@ -66,10 +66,10 @@ func (s *TestSuite) TestLS(c *C) { clnt, perr = target2Client(server.URL + "/bucket") c.Assert(perr, IsNil) - perr = doList(clnt, false, false) + perr = doList(clnt, false) c.Assert(perr, IsNil) - perr = doList(clnt, true, false) + perr = doList(clnt, true) c.Assert(perr, IsNil) }