Files
mc/cmd-ls.go
T

100 lines
2.4 KiB
Go

/*
* Mini Object Storage, (C) 2014,2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"os"
"time"
"github.com/cheggaaa/pb"
"github.com/codegangsta/cli"
"github.com/minio-io/mc/pkg/s3"
)
func printBuckets(v []*s3.Bucket) {
for _, b := range v {
msg := fmt.Sprintf("%v %9s %s", b.CreationDate.Local(), "", b.Name)
info(msg)
}
}
func printObjects(v []*s3.Item) {
if len(v) > 0 {
// Items are already sorted
for _, b := range v {
printObject(b.LastModified.Time, b.Size, b.Key)
}
}
}
func printObject(date time.Time, v int64, key string) {
msg := fmt.Sprintf("%v %9s %s", date.Local(), pb.FormatBytes(v), key)
info(msg)
}
func doFsList(c *cli.Context) {
var items []*s3.Item
s3c, err := getNewClient(c)
if err != nil {
fatal(err.Error())
}
cmdoptions, err := parseOptions(c)
if err != nil {
fatal(err.Error())
}
switch true {
case cmdoptions.bucket == "": // List all buckets
buckets, err := s3c.ListBuckets()
if err != nil {
fatal(err.Error())
}
printBuckets(buckets)
case cmdoptions.key == "": // List objects in a bucket
items, _, err = s3c.ListObjects(cmdoptions.bucket, "", "", "", s3.MaxKeys)
if err != nil {
fatal(err.Error())
}
printObjects(items)
case cmdoptions.key != "": // List objects matching the key prefix
var date time.Time
var size int64
size, date, err = s3c.Stat(cmdoptions.key, cmdoptions.bucket)
switch err {
case nil: // List a single object. Exact key
printObject(date, size, cmdoptions.key)
case os.ErrNotExist:
// List all objects matching the key prefix
items, _, err = s3c.ListObjects(cmdoptions.bucket, "", cmdoptions.key, "", s3.MaxKeys)
if err != nil {
fatal(err.Error())
}
if len(items) > 0 {
printObjects(items)
} else {
fatal(os.ErrNotExist.Error())
}
default: // Error
fatal(err.Error())
}
default:
fatal(err.Error())
}
}