storj/cmd/uplinkng/cmd_meta_get.go
Jeff Wendling ef7b89cc03 cmd/uplinkng: remove global flags
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
2021-07-06 17:26:51 -04:00

84 lines
1.8 KiB
Go

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"encoding/json"
"fmt"
"strconv"
"github.com/zeebo/clingy"
"github.com/zeebo/errs"
"storj.io/storj/cmd/uplinkng/ulext"
"storj.io/storj/cmd/uplinkng/ulloc"
)
type cmdMetaGet struct {
ex ulext.External
access string
encrypted bool
location ulloc.Location
entry *string
}
func newCmdMetaGet(ex ulext.External) *cmdMetaGet {
return &cmdMetaGet{ex: ex}
}
func (c *cmdMetaGet) Setup(params clingy.Parameters) {
c.access = params.Flag("access", "Which access to use", "").(string)
c.encrypted = params.Flag("encrypted", "Shows keys base64 encoded without decrypting", false,
clingy.Transform(strconv.ParseBool),
).(bool)
c.location = params.Arg("location", "Location of object (sj://BUCKET/KEY)",
clingy.Transform(ulloc.Parse),
).(ulloc.Location)
c.entry = params.Arg("entry", "Metadata entry to get", clingy.Optional).(*string)
}
func (c *cmdMetaGet) Execute(ctx clingy.Context) error {
project, err := c.ex.OpenProject(ctx, c.access, ulext.BypassEncryption(c.encrypted))
if err != nil {
return err
}
defer func() { _ = project.Close() }()
bucket, key, ok := c.location.RemoteParts()
if !ok {
return errs.New("location must be remote")
}
object, err := project.StatObject(ctx, bucket, key)
if err != nil {
return err
}
if c.entry != nil {
value, ok := object.Custom[*c.entry]
if !ok {
return errs.New("entry %q does not exist", *c.entry)
}
fmt.Fprintln(ctx.Stdout(), value)
return nil
}
if object.Custom == nil {
fmt.Fprintln(ctx.Stdout(), "{}")
return nil
}
data, err := json.MarshalIndent(object.Custom, "", " ")
if err != nil {
return errs.Wrap(err)
}
fmt.Fprintln(ctx.Stdout(), string(data))
return nil
}