Add ListOnChannel() for fs{unix,windows} and cmd-ls

This commit is contained in:
Harshavardhana
2015-04-23 20:02:48 -07:00
parent 1f0d1547c7
commit 972c476e36
5 changed files with 74 additions and 52 deletions
+4 -15
View File
@@ -59,21 +59,10 @@ func doList(clnt client.Client, targetURL string) (string, error) {
}
printItem(itemOnChannel.Item.Time, itemOnChannel.Item.Size, itemOnChannel.Item.Name)
}
switch iodine.ToError(err).(type) {
case client.APINotImplemented:
items, err := clnt.List()
if err != nil {
err = iodine.New(err, nil)
msg := fmt.Sprintf("mc: listing objects for URL [%s] failed with following reason: [%s]\n", targetURL, iodine.ToError(err))
return msg, err
}
printItems(items)
default:
if err != nil {
err = iodine.New(err, nil)
msg := fmt.Sprintf("mc: listing objects for URL [%s] failed with following reason: [%s]\n", targetURL, iodine.ToError(err))
return msg, err
}
if err != nil {
err = iodine.New(err, nil)
msg := fmt.Sprintf("mc: listing objects for URL [%s] failed with following reason: [%s]\n", targetURL, iodine.ToError(err))
return msg, err
}
return "", nil
}
+15 -12
View File
@@ -292,13 +292,14 @@ func (s *CmdTestSuite) TestLsCmdWithBucket(c *C) {
itemCh := make(chan client.ItemOnChannel)
go func() {
defer close(itemCh)
itemCh <- client.ItemOnChannel{
Item: nil,
Err: client.APINotImplemented{},
for _, item := range items {
itemCh <- client.ItemOnChannel{
Item: item,
Err: nil,
}
}
}()
cl1.On("ListOnChannel").Return(itemCh).Once()
cl1.On("List").Return(items, nil).Once()
msg, err := doListCmd(manager, sourceURL, sourceConfig, false)
c.Assert(msg, Equals, "")
c.Assert(err, IsNil)
@@ -335,13 +336,14 @@ func (s *CmdTestSuite) TestLsCmdWithFilePath(c *C) {
itemCh := make(chan client.ItemOnChannel)
go func() {
defer close(itemCh)
itemCh <- client.ItemOnChannel{
Item: nil,
Err: client.APINotImplemented{},
for _, item := range items {
itemCh <- client.ItemOnChannel{
Item: item,
Err: nil,
}
}
}()
cl1.On("ListOnChannel").Return(itemCh).Once()
cl1.On("List").Return(items, nil).Once()
msg, err := doListCmd(manager, sourceURL, sourceConfig, false)
c.Assert(msg, Equals, "")
c.Assert(err, IsNil)
@@ -372,13 +374,14 @@ func (s *CmdTestSuite) TestLsCmdListsBuckets(c *C) {
itemCh := make(chan client.ItemOnChannel)
go func() {
defer close(itemCh)
itemCh <- client.ItemOnChannel{
Item: nil,
Err: client.APINotImplemented{},
for _, bucket := range buckets {
itemCh <- client.ItemOnChannel{
Item: bucket,
Err: nil,
}
}
}()
cl1.On("ListOnChannel").Return(itemCh).Once()
cl1.On("List").Return(buckets, nil).Once()
msg, err := doListCmd(manager, sourceURL, sourceConfig, false)
c.Assert(msg, Equals, "")
c.Assert(err, IsNil)
+5 -15
View File
@@ -18,6 +18,7 @@ package client
import (
"io"
"os"
"regexp"
"time"
"unicode/utf8"
@@ -75,9 +76,10 @@ type ItemOnChannel struct {
// Item - object item list
type Item struct {
Name string
Time time.Time
Size int64
Name string
Time time.Time
Size int64
FileType os.FileMode
}
// IsValidBucketName reports whether bucket is a valid bucket name, per Amazon's naming restrictions.
@@ -97,18 +99,6 @@ func IsValidBucketName(bucket string) bool {
return match
}
// BySize implements sort.Interface for []Item based on the Size field.
type BySize []*Item
// Len -
func (a BySize) Len() int { return len(a) }
// Swap -
func (a BySize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// Less -
func (a BySize) Less(i, j int) bool { return a[i].Size < a[j].Size }
// IsValidObject - verify object name in accordance with
// - http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
func IsValidObject(object string) bool {
+25 -5
View File
@@ -22,7 +22,6 @@ import (
"io"
"os"
"path/filepath"
"sort"
"strings"
"io/ioutil"
@@ -118,9 +117,31 @@ func (f *fsClient) ListOnChannel() <-chan client.ItemOnChannel {
func (f *fsClient) listInGoroutine(itemCh chan client.ItemOnChannel) {
defer close(itemCh)
itemCh <- client.ItemOnChannel{
Item: nil,
Err: iodine.New(client.APINotImplemented{API: "ListOnChannel"}, nil),
visitFS := func(fp string, fi os.FileInfo, err error) error {
if err != nil {
if os.IsPermission(err) { // skip inaccessible files
return nil
}
return err // fatal
}
item := &client.Item{
Name: fp,
Time: fi.ModTime(),
Size: fi.Size(),
FileType: fi.Mode(),
}
itemCh <- client.ItemOnChannel{
Item: item,
Err: nil,
}
return nil
}
err := filepath.Walk(f.path, visitFS)
if err != nil {
itemCh <- client.ItemOnChannel{
Item: nil,
Err: iodine.New(err, nil),
}
}
}
@@ -146,7 +167,6 @@ func (f *fsClient) List() (items []*client.Item, err error) {
if err != nil {
return nil, iodine.New(err, nil)
}
sort.Sort(client.BySize(items))
return items, nil
}
+25 -5
View File
@@ -19,7 +19,6 @@ package fs
import (
"io"
"os"
"sort"
"strings"
"io/ioutil"
@@ -133,9 +132,31 @@ func (f *fsClient) ListOnChannel() <-chan client.ItemOnChannel {
func (f *fsClient) listInGoroutine(itemCh chan client.ItemOnChannel) {
defer close(itemCh)
itemCh <- client.ItemOnChannel{
Item: nil,
Err: iodine.New(client.APINotImplemented{API: "ListOnChannel"}, nil),
visitFS := func(fp string, fi os.FileInfo, err error) error {
if err != nil {
if os.IsPermission(err) { // skip inaccessible files
return nil
}
return err // fatal
}
item := &client.Item{
Name: fp,
Time: fi.ModTime(),
Size: fi.Size(),
FileType: fi.Mode(),
}
itemCh <- client.ItemOnChannel{
Item: item,
Err: nil,
}
return nil
}
err := filepath.Walk(f.path, visitFS)
if err != nil {
itemCh <- client.ItemOnChannel{
Item: nil,
Err: iodine.New(err, nil),
}
}
}
@@ -166,7 +187,6 @@ func (f *fsClient) List() (items []*client.Item, err error) {
if err != nil {
return nil, iodine.New(err, nil)
}
sort.Sort(client.BySize(items))
return items, nil
}
}