ef7b89cc03
this changes globalFlags to be a ulext.External interface value that is passed to each command. rather than have the ulext.External have a Setup call in the way that the projectProvider used to we make all of the state arguments to the functions and have the commands call setup themselves. the reason it is in its own package is so that cmd/uplinkng can import cmd/uplinkng/ultest but cmd/uplinkng/ultest needs to refer to whatever the interface type is to call the function that creates the commands. there's also quite a bit of shuffling around of code and names. sorry if that makes it tricky to review. there should be no logic changes, though. a side benefit is there's no longer a need to do a type assertion in ultest to make it set the fake filesystem to use. that can be passed in directly now. additionally, this makes the access commands much easier to test. Change-Id: I29cf6a2144248a58b7a605a7ae0a5ada5cfd57b6
120 lines
2.8 KiB
Go
120 lines
2.8 KiB
Go
// Copyright (C) 2021 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package main
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
"storj.io/storj/cmd/uplinkng/ulext"
|
|
"storj.io/storj/cmd/uplinkng/ulfs"
|
|
"storj.io/storj/cmd/uplinkng/ulloc"
|
|
)
|
|
|
|
type cmdLs struct {
|
|
ex ulext.External
|
|
|
|
access string
|
|
recursive bool
|
|
encrypted bool
|
|
pending bool
|
|
utc bool
|
|
|
|
prefix *ulloc.Location
|
|
}
|
|
|
|
func newCmdLs(ex ulext.External) *cmdLs {
|
|
return &cmdLs{ex: ex}
|
|
}
|
|
|
|
func (c *cmdLs) Setup(params clingy.Parameters) {
|
|
c.access = params.Flag("access", "Which access to use", "").(string)
|
|
c.recursive = params.Flag("recursive", "List recursively", false,
|
|
clingy.Short('r'),
|
|
clingy.Transform(strconv.ParseBool),
|
|
).(bool)
|
|
c.encrypted = params.Flag("encrypted", "Shows keys base64 encoded without decrypting", false,
|
|
clingy.Transform(strconv.ParseBool),
|
|
).(bool)
|
|
c.pending = params.Flag("pending", "List pending object uploads instead", false,
|
|
clingy.Transform(strconv.ParseBool),
|
|
).(bool)
|
|
c.utc = params.Flag("utc", "Show all timestamps in UTC instead of local time", false,
|
|
clingy.Transform(strconv.ParseBool),
|
|
).(bool)
|
|
|
|
c.prefix = params.Arg("prefix", "Prefix to list (sj://BUCKET[/KEY])", clingy.Optional,
|
|
clingy.Transform(ulloc.Parse),
|
|
).(*ulloc.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.ex.OpenProject(ctx, c.access)
|
|
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(c.utc, item.Created), item.Name)
|
|
}
|
|
return iter.Err()
|
|
}
|
|
|
|
func (c *cmdLs) listLocation(ctx clingy.Context, prefix ulloc.Location) error {
|
|
fs, err := c.ex.OpenFilesystem(ctx, c.access, ulext.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 ulfs.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(c.utc, obj.Created), obj.ContentLength, obj.Loc.Key())
|
|
}
|
|
}
|
|
return iter.Err()
|
|
}
|
|
|
|
func formatTime(utc bool, x time.Time) string {
|
|
if utc {
|
|
x = x.UTC()
|
|
} else {
|
|
x = x.Local()
|
|
}
|
|
return x.Format("2006-01-02 15:04:05")
|
|
}
|