Merge pull request #1269 from abperiasamy/ls-overhaul

simplify ls command
This commit is contained in:
Anand Babu (AB) Periasamy
2015-11-11 17:48:19 -08:00
4 changed files with 47 additions and 96 deletions
+34 -38
View File
@@ -23,7 +23,14 @@ import (
"github.com/minio/cli"
"github.com/minio/mc/pkg/client"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio-xl/pkg/probe"
)
// ls specific flags.
var (
lsFlagIncomplete = cli.BoolFlag{
Name: "incomplete, I",
Usage: "remove incomplete uploads.",
}
)
// list files and folders.
@@ -31,11 +38,15 @@ var lsCmd = cli.Command{
Name: "ls",
Usage: "List files and folders.",
Action: mainList,
Flags: []cli.Flag{lsFlagIncomplete},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} TARGET [TARGET ...]
mc {{.Name}} [OPTIONS] TARGET [TARGET ...]
OPTIONS:
--incomplete - List incomplete upload of objects.
EXAMPLES:
1. List buckets on Amazon S3 cloud storage.
@@ -43,32 +54,32 @@ EXAMPLES:
[2015-01-20 15:42:00 PST] 0B rom/
[2015-01-15 00:05:40 PST] 0B zek/
2. List buckets from Amazon S3 cloud storage and recursively list objects from Minio cloud storage.
$ mc {{.Name}} https://s3.amazonaws.com/ https://play.minio.io:9000/backup/... https://storage.googleapis.com
2. List buckets and all its contents from Amazon S3 cloud storage recursively.
$ mc {{.Name}} https://s3.amazonaws.com/...
2015-01-15 00:05:40 PST 0B zek/
2015-03-31 14:46:33 PDT 55MiB backup.tar.gz
2015-04-15 20:11:22 PST 0B miniocloud
3. List files recursively on local filesystem on Windows.
3. List files recursively on a local filesystem on Microsoft Windows.
$ mc {{.Name}} C:\Users\Worf\...
[2015-03-31 14:46:33 PDT] 15.00MiB Gowron\Khitomer Conference Details.pdf
...
4. List files with non english characters on Amazon S3 cloud storage.
4. List files with non-English characters on Amazon S3 cloud storage.
$ mc ls s3/andoria/本...
[2015-05-19 17:28:22 PDT] 41B 本語.md
5. List files with space characters on Amazon S3 cloud storage.
5. List folders with space separated names on Amazon S3 cloud storage.
$ mc ls 's3/miniocloud/Community Files/'
[2015-05-19 17:28:22 PDT] 41B 本語.md
6. Behave like operating system tool ‘ls’, used for shell aliases.
6. Behave like an operating system ‘ls’. [HINT: Add it to your shell alias].
$ mc --mimic ls
[2015-05-19 17:28:22 PDT] 41B 本語.md
7. List incompletely uploaded files for a given bucket
$ mc ls s3/miniocloud incomplete
[2015-10-19 22:28:02 PDT] 0B bin/
7. List incomplete uploads of objects on Amazon S3.
$ mc ls --incomplete s3/mybucket
[2015-10-19 22:28:02 PDT] 32MiB backup/server112-11Nov15.tgz
`,
}
@@ -104,37 +115,22 @@ func mainList(ctx *cli.Context) {
checkListSyntax(ctx)
args := ctx.Args()
isIncomplete := ctx.Bool("incomplete")
// mimic operating system tool behavior
if globalMimicFlag && !ctx.Args().Present() {
args = []string{"."}
}
var targetURLs []string
var err *probe.Error
if args.Last() == "incomplete" {
targetURLs, err = args2URLs(args.Head())
fatalIf(err.Trace(args...), "One or more unknown URL types passed.")
for _, targetURL := range targetURLs {
// if recursive strip off the "..."
var clnt client.Client
clnt, err = url2Client(stripRecursiveURL(targetURL))
fatalIf(err.Trace(targetURL), "Unable to initialize target ‘"+targetURL+"’.")
targetURLs, err := args2URLs(args.Head())
fatalIf(err.Trace(args...), "One or more unknown URL types passed.")
for _, targetURL := range targetURLs {
// if recursive strip off the "..."
var clnt client.Client
clnt, err = url2Client(stripRecursiveURL(targetURL))
fatalIf(err.Trace(targetURL), "Unable to initialize target ‘"+targetURL+"’.")
err = doListIncomplete(clnt, isURLRecursive(targetURL))
fatalIf(err.Trace(clnt.GetURL().String()), "Unable to list target ‘"+clnt.GetURL().String()+"’.")
}
} else {
targetURLs, err = args2URLs(args)
fatalIf(err.Trace(args...), "One or more unknown URL types passed.")
for _, targetURL := range targetURLs {
// if recursive strip off the "..."
var clnt client.Client
clnt, err = url2Client(stripRecursiveURL(targetURL))
fatalIf(err.Trace(targetURL), "Unable to initialize target ‘"+targetURL+"’.")
err = doList(clnt, isURLRecursive(targetURL))
fatalIf(err.Trace(clnt.GetURL().String()), "Unable to list target ‘"+clnt.GetURL().String()+"’.")
}
err = doList(clnt, isURLRecursive(targetURL), isIncomplete)
fatalIf(err.Trace(clnt.GetURL().String()), "Unable to list target ‘"+clnt.GetURL().String()+"’.")
}
}
+5 -50
View File
@@ -139,15 +139,13 @@ func trimContent(parentContent, childContent *client.Content, recursive bool) *c
}
// doList - list all entities inside a folder.
func doList(clnt client.Client, recursive bool) *probe.Error {
var err *probe.Error
var parentContent *client.Content
_, parentContent, err = url2Stat(clnt.GetURL().String())
func doList(clnt client.Client, isRecursive, isIncomplete bool) *probe.Error {
_, parentContent, err := url2Stat(clnt.GetURL().String())
if err != nil {
return err.Trace(clnt.GetURL().String())
}
isIncomplete := false // do not list incomplete uploads
for contentCh := range clnt.List(recursive, isIncomplete) {
for contentCh := range clnt.List(isRecursive, isIncomplete) {
if contentCh.Err != nil {
switch contentCh.Err.ToGoError().(type) {
// handle this specifically for filesystem
@@ -175,50 +173,7 @@ func doList(clnt client.Client, recursive bool) *probe.Error {
err = contentCh.Err.Trace()
break
}
trimmedContent := trimContent(parentContent, contentCh.Content, recursive)
parsedContent := parseContent(trimmedContent)
printMsg(parsedContent)
}
if err != nil {
return err.Trace()
}
return nil
}
// doListIncomplete - list all incomplete uploads entities inside a folder.
func doListIncomplete(clnt client.Client, recursive bool) *probe.Error {
var err *probe.Error
var parentContent *client.Content
_, parentContent, err = url2Stat(clnt.GetURL().String())
if err != nil {
return err.Trace(clnt.GetURL().String())
}
isIncomplete := true // list only incomplete uploads
for contentCh := range clnt.List(recursive, isIncomplete) {
if contentCh.Err != nil {
switch contentCh.Err.ToGoError().(type) {
// handle this specifically for filesystem
case client.BrokenSymlink:
errorIf(contentCh.Err.Trace(), "Unable to list broken link.")
continue
case client.TooManyLevelsSymlink:
errorIf(contentCh.Err.Trace(), "Unable to list too many levels link.")
continue
}
if os.IsNotExist(contentCh.Err.ToGoError()) || os.IsPermission(contentCh.Err.ToGoError()) {
if contentCh.Content != nil {
if contentCh.Content.Type.IsDir() && (contentCh.Content.Type&os.ModeSymlink == os.ModeSymlink) {
errorIf(contentCh.Err.Trace(), "Unable to list broken folder link.")
continue
}
}
errorIf(contentCh.Err.Trace(), "Unable to list.")
continue
}
err = contentCh.Err.Trace()
break
}
trimmedContent := trimContent(parentContent, contentCh.Content, recursive)
trimmedContent := trimContent(parentContent, contentCh.Content, isRecursive)
parsedContent := parseContent(trimmedContent)
printMsg(parsedContent)
}
+1 -1
View File
@@ -34,7 +34,7 @@ var mcHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}} {{if .Flags}}[global flags] {{end}}command{{if .Flags}} [command flags]{{end}} [arguments...]
{{.Name}} {{if .Flags}}[GLOBAL FLAGS] {{end}}command{{if .Flags}} [COMMAND FLAGS]{{end}} [ARGUMENTS...]
COMMANDS:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
+7 -7
View File
@@ -30,12 +30,12 @@ import (
// rm specific flags.
var (
forceFlag = cli.BoolFlag{
rmFlagForce = cli.BoolFlag{
Name: "force",
Usage: "force a dangerous remove operation.",
}
incompleteFlag = cli.BoolFlag{
rmFlagIncomplete = cli.BoolFlag{
Name: "incomplete, I",
Usage: "remove incomplete uploads.",
}
@@ -46,7 +46,7 @@ var rmCmd = cli.Command{
Name: "rm",
Usage: "Remove file or bucket [WARNING: Use with care].",
Action: mainRm,
Flags: []cli.Flag{forceFlag, incompleteFlag},
Flags: []cli.Flag{rmFlagForce, rmFlagIncomplete},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
@@ -105,10 +105,10 @@ func (r rmMessage) JSON() string {
func checkRmSyntax(ctx *cli.Context) {
args := ctx.Args()
help := ctx.GlobalBool("help")
ishelp := ctx.GlobalBool("help")
isForce := ctx.Bool("force")
if !args.Present() || help {
if !args.Present() || ishelp {
exitCode := 1
cli.ShowCommandHelpAndExit(ctx, "rm", exitCode)
}
@@ -156,8 +156,8 @@ func rmAll(url string, isIncomplete bool) {
return // End of journey.
}
recursive := false // Disable recursion and only list this folder's contents.
for entry := range clnt.List(recursive, isIncomplete) {
isRecursive := false // Disable recursion and only list this folder's contents.
for entry := range clnt.List(isRecursive, isIncomplete) {
if entry.Err != nil {
errorIf(entry.Err.Trace(url), "Unable to list ‘"+url+"’.")
return // End of journey.