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
|
|
|
|
|
|
|
"storj.io/storj/cmd/uplinkng/ulloc"
|
2021-03-31 16:56:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdMetaGet struct {
|
|
|
|
projectProvider
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdMetaGet) Setup(a clingy.Arguments, f clingy.Flags) {
|
|
|
|
c.projectProvider.Setup(a, f)
|
|
|
|
|
2021-05-12 17:23:17 +01:00
|
|
|
c.encrypted = f.New("encrypted", "Shows keys base64 encoded without decrypting", false,
|
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
|
|
|
|
2021-05-05 22:53:08 +01:00
|
|
|
c.location = a.New("location", "Location of object (sj://BUCKET/KEY)",
|
2021-05-06 17:56:57 +01:00
|
|
|
clingy.Transform(ulloc.Parse),
|
|
|
|
).(ulloc.Location)
|
2021-03-31 16:56:34 +01:00
|
|
|
c.entry = a.New("entry", "Metadata entry to get", clingy.Optional).(*string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdMetaGet) Execute(ctx clingy.Context) error {
|
2021-05-12 17:23:17 +01:00
|
|
|
project, err := c.OpenProject(ctx, 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))
|
2021-03-31 16:56:34 +01:00
|
|
|
return nil
|
|
|
|
}
|