From cb87550978acc2ff789e985f24a0716ff4854735 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 8 Nov 2015 00:07:09 -0800 Subject: [PATCH] list: Fix listing command output Fix all the incorrectness behavior of `ls` introduced after recent changes. --- ls.go | 46 ++++++++++++++++++++++++++++++++------------- pkg/client/s3/s3.go | 12 ++++-------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/ls.go b/ls.go index 8a7f4052..a54c6b12 100644 --- a/ls.go +++ b/ls.go @@ -101,6 +101,32 @@ func parseContent(c *client.Content) ContentMessage { return content } +// trimContent to fancify the output for directories +func trimContent(parentContent, childContent *client.Content, recursive bool) *client.Content { + if recursive { + trimmedContent := new(client.Content) + trimmedContent = childContent + if strings.Index(childContent.URL.Path, string(childContent.URL.Separator)) == 0 { + trimmedContent.URL.Path = trimmedContent.URL.Path[1:] + return trimmedContent + } + return trimmedContent + } + if parentContent.Type.IsDir() { + trimmedContent := new(client.Content) + trimmedContent = childContent + if parentContent.URL.Path == string(parentContent.URL.Separator) { + trimmedContent.URL.Path = strings.TrimPrefix(childContent.URL.Path, parentContent.URL.Path) + return trimmedContent + } + trimPrefixContentPath := parentContent.URL.Path[:strings.LastIndex(parentContent.URL.Path, + string(parentContent.URL.Separator))+1] + trimmedContent.URL.Path = strings.TrimPrefix(childContent.URL.Path, trimPrefixContentPath) + return trimmedContent + } + return childContent +} + // doList - list all entities inside a folder. func doList(clnt client.Client, recursive, multipleArgs bool) *probe.Error { var err *probe.Error @@ -137,19 +163,7 @@ func doList(clnt client.Client, recursive, multipleArgs bool) *probe.Error { err = contentCh.Err.Trace() break } - // if not recursive trim prefixes - if !recursive { - if parentContent.Type.IsDir() { - prefix := func() string { - trimmedPrefix := strings.TrimSuffix(parentContent.URL.Path, string(parentContent.URL.Separator)) + string(parentContent.URL.Separator) - if trimmedPrefix == string(parentContent.URL.Path) { - return "" - } - return trimmedPrefix - }() - contentCh.Content.URL.Path = strings.TrimPrefix(contentCh.Content.URL.Path, prefix) - } - } + contentCh.Content = trimContent(parentContent, contentCh.Content, recursive) printMsg(parseContent(contentCh.Content)) } if err != nil { @@ -161,6 +175,11 @@ func doList(clnt client.Client, recursive, multipleArgs bool) *probe.Error { // doListIncomplete - list all incomplete uploads entities inside a folder. func doListIncomplete(clnt client.Client, recursive, multipleArgs 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()) + } for contentCh := range clnt.List(recursive, true) { if contentCh.Err != nil { switch contentCh.Err.ToGoError().(type) { @@ -185,6 +204,7 @@ func doListIncomplete(clnt client.Client, recursive, multipleArgs bool) *probe.E err = contentCh.Err.Trace() break } + contentCh.Content = trimContent(parentContent, contentCh.Content, recursive) printMsg(parseContent(contentCh.Content)) } if err != nil { diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index 02ac787e..5a40457c 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -351,17 +351,15 @@ func (c *s3Client) listIncompleteInRoutine(contentCh chan client.ContentOnChanne return } content := new(client.Content) + url := *c.hostURL + url.Path = filepath.Join(url.Path, strings.TrimPrefix(object.Stat.Key, o)) switch { case strings.HasSuffix(object.Stat.Key, string(c.hostURL.Separator)): // We need to keep the trailing Separator, do not use filepath.Join() - url := *c.hostURL - url.Path = url.Path + object.Stat.Key content.URL = url content.Time = time.Now() content.Type = os.ModeDir default: - url := *c.hostURL - url.Path = filepath.Join(url.Path, object.Stat.Key) content.URL = url content.Size = object.Stat.Size content.Time = object.Stat.Initiated @@ -481,17 +479,15 @@ func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { return } content := new(client.Content) + url := *c.hostURL + url.Path = filepath.Join(url.Path, strings.TrimPrefix(object.Stat.Key, o)) switch { case strings.HasSuffix(object.Stat.Key, string(c.hostURL.Separator)): // We need to keep the trailing Separator, do not use filepath.Join() - url := *c.hostURL - url.Path = strings.TrimSuffix(url.Path, string(c.hostURL.Separator)) + string(c.hostURL.Separator) + strings.TrimPrefix(object.Stat.Key, o) content.URL = url content.Time = time.Now() content.Type = os.ModeDir default: - url := *c.hostURL - url.Path = filepath.Join(url.Path, strings.TrimPrefix(object.Stat.Key, o)) content.URL = url content.Size = object.Stat.Size content.Time = object.Stat.LastModified