Simplify listing recursively into one function, diff now supports <filename> <directory> style

This commit is contained in:
Harshavardhana
2015-05-20 02:44:15 -07:00
parent c8a60e3781
commit dcccafacbe
10 changed files with 96 additions and 112 deletions
+1 -1
View File
@@ -31,7 +31,7 @@ lint:
cyclo:
@echo "Running $@:"
@test -z "$$(gocyclo -over 15 . | grep -v Godeps/_workspace/src/ | tee /dev/stderr)"
@test -z "$$(gocyclo -over 16 . | grep -v Godeps/_workspace/src/ | tee /dev/stderr)"
deadcode:
@echo "Running $@:"
+28 -10
View File
@@ -69,8 +69,9 @@ func runDiffCmd(ctx *cli.Context) {
console.Fatalf("Unable to parse argument [%s]. Reason: [%s].\n", secondURL, iodine.ToError(err))
}
}
for diff := range doDiffCmd(firstURL, secondURL) {
// TODO recursive is not working yet
newFirstURL := stripRecursiveURL(firstURL)
for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) {
if diff.err != nil {
console.Fatalf(diff.message)
}
@@ -78,7 +79,8 @@ func runDiffCmd(ctx *cli.Context) {
}
}
func doDiffInRoutine(firstURL, secondURL string, ch chan diff) {
func doDiffInRoutine(firstURL, secondURL string, recursive bool, ch chan diff) {
defer close(ch)
_, firstContent, err := url2Stat(firstURL)
if err != nil {
ch <- diff{
@@ -96,29 +98,45 @@ func doDiffInRoutine(firstURL, secondURL string, ch chan diff) {
return
}
if firstContent.Type.IsRegular() {
if !secondContent.Type.IsRegular() {
switch {
case secondContent.Type.IsDir():
newSecondURL, err := urlJoinPath(secondURL, firstURL)
if err != nil {
ch <- diff{
message: "Unable to construct new URL from ‘" + secondURL + "’ using ‘" +
firstURL + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
err: iodine.New(err, nil),
}
return
}
doDiffObjects(firstURL, newSecondURL, ch)
case !secondContent.Type.IsRegular():
ch <- diff{
message: firstURL + " and " + secondURL + " differs in type.\n",
err: nil,
}
return
case secondContent.Type.IsRegular():
doDiffObjects(firstURL, secondURL, ch)
}
doDiffObjects(firstURL, secondURL, ch)
}
if firstContent.Type.IsDir() {
if !secondContent.Type.IsDir() {
switch {
case !secondContent.Type.IsDir():
ch <- diff{
message: firstURL + " and " + secondURL + " differs in type.\n",
err: nil,
}
return
default:
doDiffDirs(firstURL, secondURL, recursive, ch)
}
doDiffDirs(firstURL, secondURL, ch)
}
close(ch)
}
// doDiffCmd - Execute the diff command
func doDiffCmd(firstURL, secondURL string) <-chan diff {
func doDiffCmd(firstURL, secondURL string, recursive bool) <-chan diff {
ch := make(chan diff)
go doDiffInRoutine(firstURL, secondURL, ch)
go doDiffInRoutine(firstURL, secondURL, recursive, ch)
return ch
}
+9 -32
View File
@@ -57,47 +57,24 @@ func runListCmd(ctx *cli.Context) {
targetURLConfigMap[targetURL] = targetConfig
}
for targetURL, targetConfig := range targetURLConfigMap {
if isURLRecursive(targetURL) {
// if recursive strip off the "..."
targetURL = stripRecursiveURL(targetURL)
err = doListRecursiveCmd(targetURL, targetConfig, globalDebugFlag)
err = iodine.New(err, nil)
if err != nil {
console.Debugln(err)
console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err))
}
} else {
err = doListCmd(targetURL, targetConfig, globalDebugFlag)
if err != nil {
if err != nil {
console.Debugln(err)
console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err))
}
}
// if recursive strip off the "..."
newTargetURL := stripRecursiveURL(targetURL)
err = doListCmd(newTargetURL, targetConfig, isURLRecursive(targetURL), globalDebugFlag)
err = iodine.New(err, nil)
if err != nil {
console.Debugln(err)
console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err))
}
}
}
// doListCmd -
func doListCmd(targetURL string, targetConfig *hostConfig, debug bool) error {
func doListCmd(targetURL string, targetConfig *hostConfig, recursive, debug bool) error {
clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag)
if err != nil {
return iodine.New(err, map[string]string{"Target": targetURL})
}
err = doList(clnt, targetURL)
if err != nil {
return iodine.New(err, nil)
}
return nil
}
// doListRecursiveCmd -
func doListRecursiveCmd(targetURL string, targetConfig *hostConfig, debug bool) error {
clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag)
if err != nil {
return iodine.New(err, map[string]string{"Target": targetURL})
}
err = doListRecursive(clnt, targetURL)
err = doList(clnt, targetURL, recursive)
if err != nil {
return iodine.New(err, nil)
}
+1 -1
View File
@@ -306,7 +306,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs {
return
}
for sourceContent := range sourceClient.ListRecursive() {
for sourceContent := range sourceClient.List(true) {
if sourceContent.Err != nil {
// Listing failed.
cpURLsCh <- &cpURLs{Error: iodine.New(sourceContent.Err, nil)}
+26 -19
View File
@@ -29,22 +29,22 @@ type diff struct {
}
// urlJoinPath Join a path to existing URL
func urlJoinPath(urlStr, path string) (newURLStr string, err error) {
u, err := client.Parse(urlStr)
func urlJoinPath(url1, url2 string) (newURLStr string, err error) {
u1, err := client.Parse(url1)
if err != nil {
return "", iodine.New(err, nil)
}
u.Path = filepath.Join(u.Path, path)
newURLStr = u.String()
u2, err := client.Parse(url2)
if err != nil {
return "", iodine.New(err, nil)
}
u1.Path = filepath.Join(u1.Path, u2.Path)
newURLStr = u1.String()
return newURLStr, nil
}
// doDiffObjects - Diff two object URLs
func doDiffObjects(firstURL, secondURL string, ch chan diff) {
if firstURL == secondURL {
return
}
_, firstContent, err := url2Stat(firstURL)
if err != nil {
ch <- diff{
@@ -63,6 +63,10 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) {
return
}
if firstContent.Name == secondContent.Name {
return
}
switch {
case firstContent.Type.IsRegular():
if !secondContent.Type.IsRegular() {
@@ -76,6 +80,7 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) {
message: "‘" + firstURL + "’ is not an object. Please report this bug with ‘--debug’ option\n.",
err: iodine.New(errNotAnObject{url: firstURL}, nil),
}
return
}
if firstContent.Size != secondContent.Size {
@@ -87,7 +92,7 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) {
}
// doDiffDirs - Diff two Dir URLs
func doDiffDirs(firstURL, secondURL string, ch chan diff) {
func doDiffDirs(firstURL, secondURL string, recursive bool, ch chan diff) {
firstClnt, firstContent, err := url2Stat(firstURL)
if err != nil {
ch <- diff{
@@ -117,8 +122,9 @@ func doDiffDirs(firstURL, secondURL string, ch chan diff) {
message: "‘" + firstURL + "’ is not an object. Please report this bug with ‘--debug’ option\n.",
err: iodine.New(errNotAnObject{url: firstURL}, nil),
}
return
}
for contentCh := range firstClnt.List() {
for contentCh := range firstClnt.List(recursive) {
if contentCh.Err != nil {
ch <- diff{
message: "Failed to list ‘" + firstURL + "’. Reason: [" + iodine.ToError(contentCh.Err).Error() + "].\n",
@@ -129,16 +135,18 @@ func doDiffDirs(firstURL, secondURL string, ch chan diff) {
newFirstURL, err := urlJoinPath(firstURL, contentCh.Content.Name)
if err != nil {
ch <- diff{
message: "Unable to construct new URL from ‘" + firstURL + "’ using ‘" + contentCh.Content.Name + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
err: iodine.New(err, nil),
message: "Unable to construct new URL from ‘" + firstURL + "’ using ‘" +
contentCh.Content.Name + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
err: iodine.New(err, nil),
}
return
}
newSecondURL, err := urlJoinPath(secondURL, contentCh.Content.Name)
if err != nil {
ch <- diff{
message: "Unable to construct new URL from ‘" + secondURL + "’ using ‘" + contentCh.Content.Name + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
err: iodine.New(err, nil),
message: "Unable to construct new URL from ‘" + secondURL + "’ using ‘" +
contentCh.Content.Name + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
err: iodine.New(err, nil),
}
return
}
@@ -149,15 +157,14 @@ func doDiffDirs(firstURL, secondURL string, ch chan diff) {
err: iodine.New(err, nil),
}
return
}
_, newSecondContent, err := url2Stat(newSecondURL)
if err != nil {
ch <- diff{
message: "‘" + filepath.Base(newFirstContent.Name) + "’ only in ‘" + firstURL + "’.\n",
err: nil,
message: "Failed to stat ‘" + newSecondURL + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
err: iodine.New(err, nil),
}
continue
return
}
switch {
case newFirstContent.Type.IsDir():
@@ -166,8 +173,8 @@ func doDiffDirs(firstURL, secondURL string, ch chan diff) {
message: newFirstURL + " and " + newSecondURL + " differs in type.\n",
err: nil,
}
continue
}
continue
case newFirstContent.Type.IsRegular():
if !newSecondContent.Type.IsRegular() {
ch <- diff{
+9 -22
View File
@@ -55,34 +55,21 @@ func printContent(date time.Time, v int64, name string, fileType os.FileMode) {
}
// doList - list all entities inside a folder
func doList(clnt client.Client, targetURL string) error {
func doList(clnt client.Client, targetURL string, recursive bool) error {
var err error
for contentCh := range clnt.List() {
for contentCh := range clnt.List(recursive) {
if contentCh.Err != nil {
err = contentCh.Err
break
}
printContent(contentCh.Content.Time, contentCh.Content.Size, contentCh.Content.Name, contentCh.Content.Type)
}
if err != nil {
return iodine.New(err, map[string]string{"Target": targetURL})
}
return nil
}
// doListRecursive - list all entities inside folders and sub-folders recursively
func doListRecursive(clnt client.Client, targetURL string) error {
var err error
for contentCh := range clnt.ListRecursive() {
if contentCh.Err != nil {
err = contentCh.Err
break
contentName := contentCh.Content.Name
if recursive {
// this special handling is necessary since we are sending back absolute paths with in ListRecursive()
// a user would not wish to see a URL just for recursive and not for regular List()
//
// To be consistent we have to filter them out
contentName = strings.TrimPrefix(contentName, strings.TrimSuffix(targetURL, "/")+"/")
}
// this special handling is necessary since we are sending back absolute paths with in ListRecursive()
// a user would not wish to see a URL just for recursive and not for regular List()
//
// To be consistent we have to filter them out
contentName := strings.TrimPrefix(contentCh.Content.Name, strings.TrimSuffix(targetURL, "/")+"/")
printContent(contentCh.Content.Time, contentCh.Content.Size, contentName, contentCh.Content.Type)
}
if err != nil {
+1 -2
View File
@@ -26,8 +26,7 @@ import (
type Client interface {
// Common operations
Stat() (content *Content, err error)
List() <-chan ContentOnChannel
ListRecursive() <-chan ContentOnChannel
List(recursive bool) <-chan ContentOnChannel
// Bucket operations
MakeBucket() error
+9 -11
View File
@@ -122,13 +122,18 @@ func (f *fsClient) GetObject(offset, length uint64) (io.ReadCloser, uint64, erro
}
// List - list files and folders
func (f *fsClient) List() <-chan client.ContentOnChannel {
func (f *fsClient) List(recursive bool) <-chan client.ContentOnChannel {
contentCh := make(chan client.ContentOnChannel)
go f.list(contentCh)
switch recursive {
case true:
go f.listRecursiveInRoutine(contentCh)
default:
go f.listInRoutine(contentCh)
}
return contentCh
}
func (f *fsClient) list(contentCh chan client.ContentOnChannel) {
func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) {
defer close(contentCh)
fpath := f.path
@@ -215,14 +220,7 @@ func (f *fsClient) list(contentCh chan client.ContentOnChannel) {
}
}
// ListRecursive - list files and folders recursively
func (f *fsClient) ListRecursive() <-chan client.ContentOnChannel {
contentCh := make(chan client.ContentOnChannel)
go f.listRecursive(contentCh)
return contentCh
}
func (f *fsClient) listRecursive(contentCh chan client.ContentOnChannel) {
func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel) {
defer close(contentCh)
visitFS := func(fp string, fi os.FileInfo, err error) error {
if err != nil {
+1 -1
View File
@@ -60,7 +60,7 @@ func (s *MySuite) TestList(c *C) {
c.Assert(err, IsNil)
var contents []*client.Content
for contentCh := range fsc.ListRecursive() {
for contentCh := range fsc.List(true) {
contents = append(contents, contentCh.Content)
}
c.Assert(err, IsNil)
+11 -13
View File
@@ -152,7 +152,7 @@ func (c *s3Client) Stat() (*client.Content, error) {
return nil, iodine.New(err, nil)
}
objectMetadata := new(client.Content)
objectMetadata.Name = c.hostURL.String()
objectMetadata.Name = metadata.Key
objectMetadata.Time = metadata.LastModified
objectMetadata.Size = metadata.Size
objectMetadata.Type = os.FileMode(0664)
@@ -187,21 +187,19 @@ func (c *s3Client) url2BucketAndObject() (bucketName, objectName string) {
/// Bucket API operations
// List - list at delimited path not recursive
func (c *s3Client) List() <-chan client.ContentOnChannel {
// List - list at delimited path, if not recursive
func (c *s3Client) List(recursive bool) <-chan client.ContentOnChannel {
contentCh := make(chan client.ContentOnChannel)
go c.listInGoRoutine(contentCh)
switch recursive {
case true:
go c.listRecursiveInRoutine(contentCh)
default:
go c.listInRoutine(contentCh)
}
return contentCh
}
// ListRecursive - list buckets and objects recursively
func (c *s3Client) ListRecursive() <-chan client.ContentOnChannel {
contentCh := make(chan client.ContentOnChannel)
go c.listRecursiveInGoRoutine(contentCh)
return contentCh
}
func (c *s3Client) listInGoRoutine(contentCh chan client.ContentOnChannel) {
func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) {
defer close(contentCh)
bucket, object := c.url2BucketAndObject()
switch {
@@ -266,7 +264,7 @@ func (c *s3Client) listInGoRoutine(contentCh chan client.ContentOnChannel) {
}
}
func (c *s3Client) listRecursiveInGoRoutine(contentCh chan client.ContentOnChannel) {
func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel) {
defer close(contentCh)
bucket, object := c.url2BucketAndObject()
for object := range c.api.ListObjects(bucket, object, true) {