2021-03-31 16:56:34 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-30 10:51:31 +01:00
|
|
|
"context"
|
2022-04-28 20:14:18 +01:00
|
|
|
"encoding/json"
|
2021-03-31 16:56:34 +01:00
|
|
|
"strconv"
|
2021-04-06 04:40:04 +01:00
|
|
|
"time"
|
2021-03-31 16:56:34 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
2022-04-28 20:14:18 +01:00
|
|
|
"github.com/zeebo/errs"
|
2021-05-06 17:56:57 +01:00
|
|
|
|
2022-01-06 19:55:46 +00:00
|
|
|
"storj.io/storj/cmd/uplink/ulext"
|
|
|
|
"storj.io/storj/cmd/uplink/ulfs"
|
|
|
|
"storj.io/storj/cmd/uplink/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
|
2022-04-28 20:14:18 +01:00
|
|
|
output string
|
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'),
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-03-31 16:56:34 +01:00
|
|
|
).(bool)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.encrypted = params.Flag("encrypted", "Shows keys base64 encoded without decrypting", false,
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-03-31 16:56:34 +01:00
|
|
|
).(bool)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.pending = params.Flag("pending", "List pending object uploads instead", false,
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-04-06 04:40:04 +01:00
|
|
|
).(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'),
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-10-28 17:51:33 +01:00
|
|
|
).(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-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-05-05 22:53:08 +01:00
|
|
|
).(bool)
|
2022-04-28 20:14:18 +01:00
|
|
|
c.output = params.Flag("output", "Output Format (tabbed, json)", "tabbed",
|
|
|
|
clingy.Short('o'),
|
|
|
|
).(string)
|
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
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (c *cmdLs) Execute(ctx context.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
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (c *cmdLs) listBuckets(ctx context.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() }()
|
|
|
|
|
|
|
|
iter := project.ListBuckets(ctx, nil)
|
2022-04-28 20:14:18 +01:00
|
|
|
|
|
|
|
switch c.output {
|
|
|
|
case "tabbed":
|
|
|
|
return c.printTabbedBucket(ctx, iter)
|
|
|
|
case "json":
|
|
|
|
return c.printJSONBucket(ctx, iter)
|
|
|
|
default:
|
|
|
|
return errs.New("unknown output format, got %s", c.output)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (c *cmdLs) listLocation(ctx context.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-04-06 04:40:04 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-04-28 20:14:18 +01:00
|
|
|
switch c.output {
|
|
|
|
case "tabbed":
|
|
|
|
return c.printTabbedLocation(ctx, iter)
|
|
|
|
case "json":
|
|
|
|
return c.printJSONLocation(ctx, iter)
|
|
|
|
default:
|
|
|
|
return errs.New("unknown output format, got %s", c.output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (c *cmdLs) printTabbedBucket(ctx context.Context, iter *uplink.BucketIterator) (err error) {
|
|
|
|
tw := newTabbedWriter(clingy.Stdout(ctx), "CREATED", "NAME")
|
2022-04-28 20:14:18 +01:00
|
|
|
defer tw.Done()
|
|
|
|
|
|
|
|
for iter.Next() {
|
|
|
|
item := iter.Item()
|
|
|
|
tw.WriteLine(formatTime(c.utc, item.Created), item.Name)
|
|
|
|
}
|
|
|
|
return iter.Err()
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (c *cmdLs) printJSONBucket(ctx context.Context, iter *uplink.BucketIterator) (err error) {
|
|
|
|
jw := json.NewEncoder(clingy.Stdout(ctx))
|
2022-04-28 20:14:18 +01:00
|
|
|
|
|
|
|
for iter.Next() {
|
|
|
|
obj := iter.Item()
|
|
|
|
|
|
|
|
err = jw.Encode(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iter.Err()
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (c *cmdLs) printTabbedLocation(ctx context.Context, iter ulfs.ObjectIterator) (err error) {
|
2022-04-28 20:14:18 +01:00
|
|
|
headers := []string{"KIND", "CREATED", "SIZE", "KEY"}
|
|
|
|
if c.expanded {
|
|
|
|
headers = append(headers, "EXPIRES", "META")
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
tw := newTabbedWriter(clingy.Stdout(ctx), headers...)
|
2022-04-28 20:14:18 +01:00
|
|
|
defer tw.Done()
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (c *cmdLs) printJSONLocation(ctx context.Context, iter ulfs.ObjectIterator) (err error) {
|
|
|
|
jw := json.NewEncoder(clingy.Stdout(ctx))
|
2022-04-28 20:14:18 +01:00
|
|
|
|
|
|
|
for iter.Next() {
|
|
|
|
obj := iter.Item()
|
|
|
|
|
|
|
|
if obj.IsPrefix {
|
|
|
|
err = jw.Encode(struct {
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
}{"PRE", obj.Loc.Loc()})
|
|
|
|
} else {
|
|
|
|
err = jw.Encode(struct {
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
Created string `json:"created"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
Expires string `json:"expires,omitempty"`
|
|
|
|
Metadata int `json:"meta,omitempty"`
|
|
|
|
}{"OBJ", formatTime(c.utc, obj.Created), obj.ContentLength, obj.Loc.Loc(), formatTime(c.utc, obj.Expires), sumMetadataSize(obj.Metadata)})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|