[#1233] neofs-cli: Respect format flags for SplitInfo output

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-14 16:27:22 +03:00 committed by Alex Vanin
parent 5c5279688b
commit 5eef0f46c5

View file

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"bytes"
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex" "encoding/hex"
"errors" "errors"
@ -1096,7 +1097,7 @@ func printSplitInfoErr(cmd *cobra.Command, err error) bool {
ok := errors.As(err, &errSplitInfo) ok := errors.As(err, &errSplitInfo)
if ok { if ok {
cmd.Println("Object is complex, split information received.") cmd.PrintErrln("Object is complex, split information received.")
printSplitInfo(cmd, errSplitInfo.SplitInfo()) printSplitInfo(cmd, errSplitInfo.SplitInfo())
} }
@ -1104,15 +1105,33 @@ func printSplitInfoErr(cmd *cobra.Command, err error) bool {
} }
func printSplitInfo(cmd *cobra.Command, info *object.SplitInfo) { func printSplitInfo(cmd *cobra.Command, info *object.SplitInfo) {
bs, err := marshalSplitInfo(cmd, info)
exitOnErr(cmd, fmt.Errorf("can't marshal split info: %w", err))
cmd.Println(string(bs))
}
func marshalSplitInfo(cmd *cobra.Command, info *object.SplitInfo) ([]byte, error) {
toJSON, _ := cmd.Flags().GetBool("json")
toProto, _ := cmd.Flags().GetBool("proto")
switch {
case toJSON && toProto:
return nil, errors.New("'--json' and '--proto' flags are mutually exclusive")
case toJSON:
return info.MarshalJSON()
case toProto:
return info.Marshal()
default:
b := bytes.NewBuffer(nil)
if splitID := info.SplitID(); splitID != nil { if splitID := info.SplitID(); splitID != nil {
cmd.Println("Split ID:", splitID) b.WriteString("Split ID: " + splitID.String() + "\n")
} }
if link := info.Link(); link != nil { if link := info.Link(); link != nil {
cmd.Println("Linking object:", link) b.WriteString("Linking object: " + link.String() + "\n")
} }
if last := info.LastPart(); last != nil { if last := info.LastPart(); last != nil {
cmd.Println("Last object:", last) b.WriteString("Last object: " + last.String() + "\n")
}
return b.Bytes(), nil
} }
} }