46a3242ed4
Change-Id: I9c251028d9f72572f4d42815de31d44517cd5525
103 lines
2.3 KiB
Go
103 lines
2.3 KiB
Go
// Copyright (C) 2021 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package main
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/zeebo/clingy"
|
|
)
|
|
|
|
type cmdLs struct {
|
|
projectProvider
|
|
|
|
recursive bool
|
|
encrypted bool
|
|
pending bool
|
|
|
|
prefix *Location
|
|
}
|
|
|
|
func (c *cmdLs) Setup(a clingy.Arguments, f clingy.Flags) {
|
|
c.projectProvider.Setup(a, f)
|
|
|
|
c.recursive = f.New("recursive", "List recursively", false,
|
|
clingy.Short('r'),
|
|
clingy.Transform(strconv.ParseBool),
|
|
).(bool)
|
|
c.encrypted = f.New("encrypted", "Shows keys base64 encoded without decrypting", false,
|
|
clingy.Transform(strconv.ParseBool),
|
|
).(bool)
|
|
c.pending = f.New("pending", "List pending object uploads instead", false,
|
|
clingy.Transform(strconv.ParseBool),
|
|
).(bool)
|
|
|
|
c.prefix = a.New("prefix", "Prefix to list (sj://BUCKET[/KEY])", clingy.Optional,
|
|
clingy.Transform(parseLocation),
|
|
).(*Location)
|
|
}
|
|
|
|
func (c *cmdLs) Execute(ctx clingy.Context) error {
|
|
if c.prefix == nil {
|
|
return c.listBuckets(ctx)
|
|
}
|
|
return c.listLocation(ctx, *c.prefix)
|
|
}
|
|
|
|
func (c *cmdLs) listBuckets(ctx clingy.Context) error {
|
|
project, err := c.OpenProject(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = project.Close() }()
|
|
|
|
tw := newTabbedWriter(ctx.Stdout(), "CREATED", "NAME")
|
|
defer tw.Done()
|
|
|
|
iter := project.ListBuckets(ctx, nil)
|
|
for iter.Next() {
|
|
item := iter.Item()
|
|
tw.WriteLine(formatTime(item.Created), item.Name)
|
|
}
|
|
return iter.Err()
|
|
}
|
|
|
|
func (c *cmdLs) listLocation(ctx clingy.Context, prefix Location) error {
|
|
fs, err := c.OpenFilesystem(ctx, bypassEncryption(c.encrypted))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = fs.Close() }()
|
|
|
|
tw := newTabbedWriter(ctx.Stdout(), "KIND", "CREATED", "SIZE", "KEY")
|
|
defer tw.Done()
|
|
|
|
// create the object iterator of either existing objects or pending multipart uploads
|
|
var iter objectIterator
|
|
if c.pending {
|
|
iter, err = fs.ListUploads(ctx, prefix, c.recursive)
|
|
} else {
|
|
iter, err = fs.ListObjects(ctx, prefix, c.recursive)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// iterate and print the results
|
|
for iter.Next() {
|
|
obj := iter.Item()
|
|
if obj.IsPrefix {
|
|
tw.WriteLine("PRE", "", "", obj.Loc.Key())
|
|
} else {
|
|
tw.WriteLine("OBJ", formatTime(obj.Created), obj.ContentLength, obj.Loc.Key())
|
|
}
|
|
}
|
|
return iter.Err()
|
|
}
|
|
|
|
func formatTime(x time.Time) string {
|
|
return x.Local().Format("2006-01-02 15:04:05")
|
|
}
|