From 15a04669661f632898833e9545a0c9d37598c2b3 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 14 Nov 2015 12:09:28 -0800 Subject: [PATCH] ls: Fix inconsistent ls output --- ls.go | 27 ++++++++++++++------------- url.go | 10 +++++++--- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/ls.go b/ls.go index 3b6c787e..360cf2ff 100644 --- a/ls.go +++ b/ls.go @@ -102,13 +102,13 @@ func parseContent(c *client.Content) contentMessage { } // trimContent to fancify the output for directories. -func trimContent(parentContent, childContent *client.Content, recursive bool) *client.Content { +func trimContent(parentContentDir, childContent *client.Content, recursive bool) *client.Content { if recursive { - // If recursive remove the unnecessary parentContent prefix. '/', in the beginning. + // If recursive remove the unnecessary parentContentDir prefix. '/', in the beginning. trimmedContent := new(client.Content) trimmedContent = childContent - if strings.HasSuffix(parentContent.URL.Path, string(parentContent.URL.Separator)) { - trimmedContent.URL.Path = strings.TrimPrefix(trimmedContent.URL.Path, parentContent.URL.Path) + if strings.HasSuffix(parentContentDir.URL.Path, string(parentContentDir.URL.Separator)) { + trimmedContent.URL.Path = strings.TrimPrefix(trimmedContent.URL.Path, parentContentDir.URL.Path) } if strings.Index(trimmedContent.URL.Path, string(trimmedContent.URL.Separator)) == 0 { if len(trimmedContent.URL.Path) > 0 { @@ -117,21 +117,21 @@ func trimContent(parentContent, childContent *client.Content, recursive bool) *c } return trimmedContent } - // If parentContent is a directory, use it to trim the sub-folders. - if parentContent.Type.IsDir() { + // If parentContentDir is a directory, use it to trim the sub-folders. + if parentContentDir.Type.IsDir() { // Allocate a new client.Content for trimmed output. trimmedContent := new(client.Content) trimmedContent = childContent - if parentContent.URL.Path == string(parentContent.URL.Separator) { - trimmedContent.URL.Path = strings.TrimPrefix(trimmedContent.URL.Path, parentContent.URL.Path) + if parentContentDir.URL.Path == string(parentContentDir.URL.Separator) { + trimmedContent.URL.Path = strings.TrimPrefix(trimmedContent.URL.Path, parentContentDir.URL.Path) return trimmedContent } // If the beginning of the trimPrefix is a URL.Separator ignore it. trimmedContent.URL.Path = strings.TrimPrefix(trimmedContent.URL.Path, string(trimmedContent.URL.Separator)) - trimPrefixContentPath := parentContent.URL.Path[:strings.LastIndex(parentContent.URL.Path, - string(parentContent.URL.Separator))+1] + trimPrefixContentPath := parentContentDir.URL.Path[:strings.LastIndex(parentContentDir.URL.Path, + string(parentContentDir.URL.Separator))+1] // If the beginning of the trimPrefix is a URL.Separator ignore it. - trimPrefixContentPath = strings.TrimPrefix(trimPrefixContentPath, string(parentContent.URL.Separator)) + trimPrefixContentPath = strings.TrimPrefix(trimPrefixContentPath, string(parentContentDir.URL.Separator)) trimmedContent.URL.Path = strings.TrimPrefix(trimmedContent.URL.Path, trimPrefixContentPath) return trimmedContent } @@ -141,7 +141,8 @@ func trimContent(parentContent, childContent *client.Content, recursive bool) *c // doList - list all entities inside a folder. func doList(clnt client.Client, isRecursive, isIncomplete bool) *probe.Error { - parentContent, err := url2Content(clnt.GetURL().String()) + // parentContentDir is verified prefix of clnt URL used for trimming purposes. + parentContentDir, err := url2DirContent(clnt.GetURL().String()) if err != nil { return err.Trace(clnt.GetURL().String()) } @@ -166,7 +167,7 @@ func doList(clnt client.Client, isRecursive, isIncomplete bool) *probe.Error { break } // trim incoming content based on if its recursive or not. - trimmedContent := trimContent(parentContent, contentCh.Content, isRecursive) + trimmedContent := trimContent(parentContentDir, contentCh.Content, isRecursive) // parse trimmed content into printable form. parsedContent := parseContent(trimmedContent) // print colorized or jsonized content info. diff --git a/url.go b/url.go index 5e6b18d5..fdf04d49 100644 --- a/url.go +++ b/url.go @@ -17,6 +17,7 @@ package main import ( + "os" "path/filepath" "strings" @@ -100,8 +101,8 @@ func url2Stat(urlStr string) (client client.Client, content *client.Content, err return client, content, nil } -// url2Content returns content info for URL. -func url2Content(urlStr string) (content *client.Content, err *probe.Error) { +// url2DirContent returns directory content info for a URL. +func url2DirContent(urlStr string) (content *client.Content, err *probe.Error) { clnt, err := url2Client(urlStr) if err != nil { return nil, err.Trace(urlStr) @@ -113,7 +114,10 @@ func url2Content(urlStr string) (content *client.Content, err *probe.Error) { return nil, entry.Err.Trace(urlStr) } if strings.HasPrefix(entry.Content.URL.Path, clnt.GetURL().Path) { - return entry.Content, nil + content := new(client.Content) + content.URL = clnt.GetURL() + content.Type = os.ModeDir + return content, nil } } return nil, errDummy().Trace(urlStr)