2022-06-01 12:42:28 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/cheggaaa/pb"
|
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var objectGetCmd = &cobra.Command{
|
|
|
|
Use: "get",
|
|
|
|
Short: "Get object from NeoFS",
|
|
|
|
Long: "Get object from NeoFS",
|
|
|
|
Run: getObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initObjectGetCmd() {
|
|
|
|
commonflags.Init(objectGetCmd)
|
2022-10-20 09:40:33 +00:00
|
|
|
initFlagSession(objectGetCmd, "GET")
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
flags := objectGetCmd.Flags()
|
|
|
|
|
|
|
|
flags.String("cid", "", "Container ID")
|
|
|
|
_ = objectGetCmd.MarkFlagRequired("cid")
|
|
|
|
|
|
|
|
flags.String("oid", "", "Object ID")
|
|
|
|
_ = objectGetCmd.MarkFlagRequired("oid")
|
|
|
|
|
|
|
|
flags.String("file", "", "File to write object payload to. Default: stdout.")
|
|
|
|
flags.String("header", "", "File to write header to. Default: stdout.")
|
|
|
|
flags.Bool(rawFlag, false, rawFlagDesc)
|
|
|
|
flags.Bool(noProgressFlag, false, "Do not show progress bar")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObject(cmd *cobra.Command, _ []string) {
|
|
|
|
var cnr cid.ID
|
|
|
|
var obj oid.ID
|
|
|
|
|
|
|
|
objAddr := readObjectAddress(cmd, &cnr, &obj)
|
|
|
|
|
|
|
|
var out io.Writer
|
|
|
|
filename := cmd.Flag("file").Value.String()
|
|
|
|
if filename == "" {
|
|
|
|
out = os.Stdout
|
|
|
|
} else {
|
2022-08-23 15:02:19 +00:00
|
|
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
2022-06-01 12:42:28 +00:00
|
|
|
if err != nil {
|
|
|
|
common.ExitOnErr(cmd, "", fmt.Errorf("can't open file '%s': %w", filename, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
out = f
|
|
|
|
}
|
|
|
|
|
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
|
2022-10-10 18:31:19 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
|
|
|
|
|
2022-06-01 12:42:28 +00:00
|
|
|
var prm internalclient.GetObjectPrm
|
2022-10-10 18:31:19 +00:00
|
|
|
prm.SetClient(cli)
|
2022-06-01 12:42:28 +00:00
|
|
|
Prepare(cmd, &prm)
|
2022-10-10 18:31:19 +00:00
|
|
|
readSession(cmd, &prm, pk, cnr, obj)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
raw, _ := cmd.Flags().GetBool(rawFlag)
|
|
|
|
prm.SetRawFlag(raw)
|
|
|
|
prm.SetAddress(objAddr)
|
|
|
|
|
|
|
|
var p *pb.ProgressBar
|
|
|
|
noProgress, _ := cmd.Flags().GetBool(noProgressFlag)
|
|
|
|
|
|
|
|
if filename == "" || noProgress {
|
|
|
|
prm.SetPayloadWriter(out)
|
|
|
|
} else {
|
|
|
|
p = pb.New64(0)
|
|
|
|
p.Output = cmd.OutOrStdout()
|
|
|
|
prm.SetPayloadWriter(p.NewProxyWriter(out))
|
|
|
|
prm.SetHeaderCallback(func(o *object.Object) {
|
|
|
|
p.SetTotal64(int64(o.PayloadSize()))
|
|
|
|
p.Start()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := internalclient.GetObject(prm)
|
2022-06-08 07:59:11 +00:00
|
|
|
if p != nil {
|
|
|
|
p.Finish()
|
|
|
|
}
|
2022-06-01 12:42:28 +00:00
|
|
|
if err != nil {
|
|
|
|
if ok := printSplitInfoErr(cmd, err); ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdrFile := cmd.Flag("header").Value.String()
|
|
|
|
if filename != "" {
|
|
|
|
if hdrFile != "" || !strictOutput(cmd) {
|
|
|
|
cmd.Printf("[%s] Object successfully saved\n", filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print header only if file is not streamed to stdout.
|
|
|
|
if filename != "" || hdrFile != "" {
|
|
|
|
err = saveAndPrintHeader(cmd, res.Header(), hdrFile)
|
|
|
|
common.ExitOnErr(cmd, "", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func strictOutput(cmd *cobra.Command) bool {
|
2022-06-23 13:52:47 +00:00
|
|
|
toJSON, _ := cmd.Flags().GetBool(commonflags.JSON)
|
2022-06-01 12:42:28 +00:00
|
|
|
toProto, _ := cmd.Flags().GetBool("proto")
|
|
|
|
return toJSON || toProto
|
|
|
|
}
|