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"
|
2021-05-06 17:56:57 +01:00
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
"storj.io/storj/cmd/uplinkng/ulext"
|
2021-05-06 17:56:57 +01:00
|
|
|
"storj.io/storj/cmd/uplinkng/ulfs"
|
|
|
|
"storj.io/storj/cmd/uplinkng/ulloc"
|
2021-10-28 17:51:33 +01:00
|
|
|
"storj.io/uplink"
|
2021-03-31 16:56:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdLs struct {
|
2021-05-26 21:19:29 +01:00
|
|
|
ex ulext.External
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
access string
|
2021-03-31 16:56:34 +01:00
|
|
|
recursive bool
|
|
|
|
encrypted bool
|
2021-10-28 17:51:33 +01:00
|
|
|
expanded bool
|
2021-04-06 04:40:04 +01:00
|
|
|
pending bool
|
2021-05-05 22:53:08 +01:00
|
|
|
utc bool
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
prefix *ulloc.Location
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
func newCmdLs(ex ulext.External) *cmdLs {
|
|
|
|
return &cmdLs{ex: ex}
|
|
|
|
}
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
func (c *cmdLs) Setup(params clingy.Parameters) {
|
2021-06-25 17:51:05 +01:00
|
|
|
c.access = params.Flag("access", "Access name or value to use", "").(string)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.recursive = params.Flag("recursive", "List recursively", false,
|
2021-03-31 16:56:34 +01:00
|
|
|
clingy.Short('r'),
|
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.encrypted = params.Flag("encrypted", "Shows keys base64 encoded without decrypting", false,
|
2021-03-31 16:56:34 +01:00
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.pending = params.Flag("pending", "List pending object uploads instead", false,
|
2021-04-06 04:40:04 +01:00
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-10-28 17:51:33 +01:00
|
|
|
c.expanded = params.Flag("expanded", "Use expanded output, showing object expiration times and whether there is custom metadata attached", false,
|
|
|
|
clingy.Short('x'),
|
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.utc = params.Flag("utc", "Show all timestamps in UTC instead of local time", false,
|
2021-05-05 22:53:08 +01:00
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-25 00:11:50 +01:00
|
|
|
c.prefix = params.Arg("prefix", "Prefix to list (sj://BUCKET[/KEY])", clingy.Optional,
|
2021-05-06 17:56:57 +01:00
|
|
|
clingy.Transform(ulloc.Parse),
|
|
|
|
).(*ulloc.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 {
|
2021-05-26 21:19:29 +01:00
|
|
|
project, err := c.ex.OpenProject(ctx, c.access)
|
2021-03-31 16:56:34 +01:00
|
|
|
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-05-05 22:53:08 +01:00
|
|
|
tw.WriteLine(formatTime(c.utc, item.Created), item.Name)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
return iter.Err()
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
func (c *cmdLs) listLocation(ctx clingy.Context, prefix ulloc.Location) error {
|
2021-05-26 21:19:29 +01:00
|
|
|
fs, err := c.ex.OpenFilesystem(ctx, c.access, ulext.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
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
if fs.IsLocalDir(ctx, prefix) {
|
|
|
|
prefix = prefix.AsDirectoryish()
|
|
|
|
}
|
|
|
|
|
2021-10-28 17:51:33 +01:00
|
|
|
headers := []string{"KIND", "CREATED", "SIZE", "KEY"}
|
|
|
|
if c.expanded {
|
|
|
|
headers = append(headers, "EXPIRES", "META")
|
|
|
|
}
|
|
|
|
|
|
|
|
tw := newTabbedWriter(ctx.Stdout(), headers...)
|
2021-04-06 04:40:04 +01:00
|
|
|
defer tw.Done()
|
|
|
|
|
|
|
|
// create the object iterator of either existing objects or pending multipart uploads
|
2021-10-02 00:47:53 +01:00
|
|
|
iter, err := fs.List(ctx, prefix, &ulfs.ListOptions{
|
|
|
|
Recursive: c.recursive,
|
|
|
|
Pending: c.pending,
|
2021-10-28 17:51:33 +01:00
|
|
|
Expanded: c.expanded,
|
2021-10-02 00:47:53 +01:00
|
|
|
})
|
2021-04-06 20:19:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-06 04:40:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// iterate and print the results
|
|
|
|
for iter.Next() {
|
|
|
|
obj := iter.Item()
|
2021-10-28 17:51:33 +01:00
|
|
|
|
|
|
|
var parts []interface{}
|
2021-04-06 04:40:04 +01:00
|
|
|
if obj.IsPrefix {
|
2021-10-28 17:51:33 +01:00
|
|
|
parts = append(parts, "PRE", "", "", obj.Loc.Loc())
|
|
|
|
if c.expanded {
|
|
|
|
parts = append(parts, "", "")
|
|
|
|
}
|
2021-04-06 04:40:04 +01:00
|
|
|
} else {
|
2021-10-28 17:51:33 +01:00
|
|
|
parts = append(parts, "OBJ", formatTime(c.utc, obj.Created), obj.ContentLength, obj.Loc.Loc())
|
|
|
|
if c.expanded {
|
|
|
|
parts = append(parts, formatTime(c.utc, obj.Expires), sumMetadataSize(obj.Metadata))
|
|
|
|
}
|
2021-04-06 04:40:04 +01:00
|
|
|
}
|
2021-10-28 17:51:33 +01:00
|
|
|
|
|
|
|
tw.WriteLine(parts...)
|
2021-04-06 04:40:04 +01:00
|
|
|
}
|
|
|
|
return iter.Err()
|
|
|
|
}
|
|
|
|
|
2021-05-05 22:53:08 +01:00
|
|
|
func formatTime(utc bool, x time.Time) string {
|
2021-10-28 17:51:33 +01:00
|
|
|
if x.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-05-05 22:53:08 +01:00
|
|
|
if utc {
|
|
|
|
x = x.UTC()
|
|
|
|
} else {
|
|
|
|
x = x.Local()
|
|
|
|
}
|
|
|
|
return x.Format("2006-01-02 15:04:05")
|
2021-04-06 04:40:04 +01:00
|
|
|
}
|
2021-10-28 17:51:33 +01:00
|
|
|
|
|
|
|
func sumMetadataSize(md uplink.CustomMetadata) int {
|
|
|
|
size := 0
|
|
|
|
for k, v := range md {
|
|
|
|
size += len(k)
|
|
|
|
size += len(v)
|
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|