2021-03-31 16:56:34 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-05-12 17:23:17 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2021-03-31 16:56:34 +01:00
|
|
|
"github.com/zeebo/clingy"
|
2021-05-12 17:23:17 +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/ulloc"
|
2021-03-31 16:56:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdMetaGet 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-05-12 17:23:17 +01:00
|
|
|
encrypted bool
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
location ulloc.Location
|
2021-04-06 20:19:11 +01:00
|
|
|
entry *string
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
func newCmdMetaGet(ex ulext.External) *cmdMetaGet {
|
|
|
|
return &cmdMetaGet{ex: ex}
|
|
|
|
}
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
func (c *cmdMetaGet) 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.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-05-12 17:23:17 +01:00
|
|
|
).(bool)
|
|
|
|
|
2021-05-25 00:11:50 +01:00
|
|
|
c.location = params.Arg("location", "Location of object (sj://BUCKET/KEY)",
|
2021-05-06 17:56:57 +01:00
|
|
|
clingy.Transform(ulloc.Parse),
|
|
|
|
).(ulloc.Location)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.entry = params.Arg("entry", "Metadata entry to get", clingy.Optional).(*string)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdMetaGet) Execute(ctx clingy.Context) error {
|
2021-05-26 21:19:29 +01:00
|
|
|
project, err := c.ex.OpenProject(ctx, c.access, ulext.BypassEncryption(c.encrypted))
|
2021-05-12 17:23:17 +01:00
|
|
|
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))
|
2021-03-31 16:56:34 +01:00
|
|
|
return nil
|
|
|
|
}
|