[#240] cmd/cli: Support raw flag in object GET/HEAD/RANGE commands

Support boolean raw flag. Print split information upon receipt.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-12-14 11:56:22 +03:00 committed by Alex Vanin
parent fb50362dcc
commit 6814140a19

View file

@ -97,6 +97,11 @@ const (
const searchOIDFlag = "oid" const searchOIDFlag = "oid"
const (
rawFlag = "raw"
rawFlagDesc = "Set raw request option"
)
func init() { func init() {
rootCmd.AddCommand(objectCmd) rootCmd.AddCommand(objectCmd)
objectCmd.PersistentFlags().String("bearer", "", "File with signed JSON or binary encoded bearer token") objectCmd.PersistentFlags().String("bearer", "", "File with signed JSON or binary encoded bearer token")
@ -124,6 +129,7 @@ func init() {
_ = objectGetCmd.MarkFlagRequired("cid") _ = objectGetCmd.MarkFlagRequired("cid")
objectGetCmd.Flags().String("oid", "", "Object ID") objectGetCmd.Flags().String("oid", "", "Object ID")
_ = objectGetCmd.MarkFlagRequired("oid") _ = objectGetCmd.MarkFlagRequired("oid")
objectGetCmd.Flags().Bool(rawFlag, false, rawFlagDesc)
objectCmd.AddCommand(objectSearchCmd) objectCmd.AddCommand(objectSearchCmd)
objectSearchCmd.Flags().String("cid", "", "Container ID") objectSearchCmd.Flags().String("cid", "", "Container ID")
@ -142,6 +148,7 @@ func init() {
objectHeadCmd.Flags().Bool("main-only", false, "Return only main fields") objectHeadCmd.Flags().Bool("main-only", false, "Return only main fields")
objectHeadCmd.Flags().Bool("json", false, "Marshal output in JSON") objectHeadCmd.Flags().Bool("json", false, "Marshal output in JSON")
objectHeadCmd.Flags().Bool("proto", false, "Marshal output in Protobuf") objectHeadCmd.Flags().Bool("proto", false, "Marshal output in Protobuf")
objectHeadCmd.Flags().Bool(rawFlag, false, rawFlagDesc)
objectCmd.AddCommand(objectHashCmd) objectCmd.AddCommand(objectHashCmd)
objectHashCmd.Flags().String("cid", "", "Container ID") objectHashCmd.Flags().String("cid", "", "Container ID")
@ -158,6 +165,7 @@ func init() {
_ = objectRangeCmd.MarkFlagRequired("oid") _ = objectRangeCmd.MarkFlagRequired("oid")
objectRangeCmd.Flags().String("range", "", "Range to take data from in the form offset:length") objectRangeCmd.Flags().String("range", "", "Range to take data from in the form offset:length")
objectRangeCmd.Flags().String("file", "", "File to write object payload to. Default: stdout.") objectRangeCmd.Flags().String("file", "", "File to write object payload to. Default: stdout.")
objectRangeCmd.Flags().Bool(rawFlag, false, rawFlagDesc)
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.
@ -291,14 +299,22 @@ func getObject(cmd *cobra.Command, _ []string) error {
if err != nil { if err != nil {
return err return err
} }
raw, _ := cmd.Flags().GetBool(rawFlag)
obj, err := cli.GetObject(ctx, obj, err := cli.GetObject(ctx,
new(client.GetObjectParams). new(client.GetObjectParams).
WithAddress(objAddr). WithAddress(objAddr).
WithPayloadWriter(out), WithPayloadWriter(out).
WithRawFlag(raw),
client.WithTTL(getTTL()), client.WithTTL(getTTL()),
client.WithSession(tok), client.WithSession(tok),
client.WithBearer(btok)) client.WithBearer(btok))
if err != nil { if err != nil {
if ok := printSplitInfoErr(cmd, err); ok {
return nil
}
return fmt.Errorf("can't put object: %w", err) return fmt.Errorf("can't put object: %w", err)
} }
@ -333,11 +349,19 @@ func getObjectHeader(cmd *cobra.Command, _ []string) error {
if ok, _ := cmd.Flags().GetBool("main-only"); ok { if ok, _ := cmd.Flags().GetBool("main-only"); ok {
ps = ps.WithMainFields() ps = ps.WithMainFields()
} }
raw, _ := cmd.Flags().GetBool(rawFlag)
ps.WithRawFlag(raw)
obj, err := cli.GetObjectHeader(ctx, ps, obj, err := cli.GetObjectHeader(ctx, ps,
client.WithTTL(getTTL()), client.WithTTL(getTTL()),
client.WithSession(tok), client.WithSession(tok),
client.WithBearer(btok)) client.WithBearer(btok))
if err != nil { if err != nil {
if ok := printSplitInfoErr(cmd, err); ok {
return nil
}
return fmt.Errorf("can't put object: %w", err) return fmt.Errorf("can't put object: %w", err)
} }
@ -743,15 +767,22 @@ func getObjectRange(cmd *cobra.Command, _ []string) error {
return err return err
} }
raw, _ := cmd.Flags().GetBool(rawFlag)
_, err = c.ObjectPayloadRangeData(ctx, _, err = c.ObjectPayloadRangeData(ctx,
new(client.RangeDataParams). new(client.RangeDataParams).
WithAddress(objAddr). WithAddress(objAddr).
WithRange(ranges[0]). WithRange(ranges[0]).
WithDataWriter(out), WithDataWriter(out).
WithRaw(raw),
client.WithTTL(getTTL()), client.WithTTL(getTTL()),
client.WithSession(sessionToken), client.WithSession(sessionToken),
client.WithBearer(bearerToken)) client.WithBearer(bearerToken))
if err != nil { if err != nil {
if ok := printSplitInfoErr(cmd, err); ok {
return nil
}
return fmt.Errorf("can't get object payload range: %w", err) return fmt.Errorf("can't get object payload range: %w", err)
} }
@ -761,3 +792,30 @@ func getObjectRange(cmd *cobra.Command, _ []string) error {
return nil return nil
} }
func printSplitInfoErr(cmd *cobra.Command, err error) bool {
var errSplitInfo *object.SplitInfoError
ok := errors.As(err, &errSplitInfo)
if ok {
cmd.Println("Object is complex, split information received.")
printSplitInfo(cmd, errSplitInfo.SplitInfo())
}
return ok
}
func printSplitInfo(cmd *cobra.Command, info *object.SplitInfo) {
if splitID := info.SplitID(); splitID != nil {
cmd.Println("Split ID:", splitID)
}
if link := info.Link(); link != nil {
cmd.Println("Linking object:", link)
}
if last := info.LastPart(); last != nil {
cmd.Println("Last object:", last)
}
}