ls modifications - always print for multiple arguments files first and directories second
This commit is contained in:
+43
-4
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+4
-4
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user