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