From c78e9cc8570f77d327c9e67fa371a34ca00e01f7 Mon Sep 17 00:00:00 2001
From: Dmitrii Stepanov <d.stepanov@yadro.com>
Date: Thu, 23 Mar 2023 15:13:48 +0300
Subject: [PATCH] [#165] cli: Refactor get command

Resolve funlen linter for getObject method

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
---
 cmd/frostfs-cli/modules/object/get.go | 40 +++++++++++++++++----------
 1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/cmd/frostfs-cli/modules/object/get.go b/cmd/frostfs-cli/modules/object/get.go
index 900326df9..68e47da6f 100644
--- a/cmd/frostfs-cli/modules/object/get.go
+++ b/cmd/frostfs-cli/modules/object/get.go
@@ -42,27 +42,15 @@ func initObjectGetCmd() {
 	flags.Bool(binaryFlag, false, "Serialize whole object structure into given file(id + signature + header + payload).")
 }
 
-// nolint: funlen
 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(fileFlag).Value.String()
-	if filename == "" {
-		out = os.Stdout
-	} else {
-		f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
-		if err != nil {
-			commonCmd.ExitOnErr(cmd, "", fmt.Errorf("can't open file '%s': %w", filename, err))
-		}
-
-		defer f.Close()
-
-		out = f
-	}
+	out, closer := createOutWriter(cmd, filename)
+	defer closer()
 
 	pk := key.GetOrGenerate(cmd)
 
@@ -114,6 +102,10 @@ func getObject(cmd *cobra.Command, _ []string) {
 		commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
 	}
 
+	processResult(cmd, res, binary, payloadBuffer, out, filename)
+}
+
+func processResult(cmd *cobra.Command, res *internalclient.GetObjectRes, binary bool, payloadBuffer *bytes.Buffer, out io.Writer, filename string) {
 	if binary {
 		objToStore := res.Header()
 		// TODO(@acid-ant): #1932 Use streams to marshal/unmarshal payload
@@ -130,11 +122,29 @@ func getObject(cmd *cobra.Command, _ []string) {
 
 	// Print header only if file is not streamed to stdout.
 	if filename != "" {
-		err = printHeader(cmd, res.Header())
+		err := printHeader(cmd, res.Header())
 		commonCmd.ExitOnErr(cmd, "", err)
 	}
 }
 
+func createOutWriter(cmd *cobra.Command, filename string) (out io.Writer, closer func()) {
+	if filename == "" {
+		out = os.Stdout
+		closer = func() {}
+	} else {
+		f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
+		if err != nil {
+			commonCmd.ExitOnErr(cmd, "", fmt.Errorf("can't open file '%s': %w", filename, err))
+		}
+
+		out = f
+		closer = func() {
+			f.Close()
+		}
+	}
+	return
+}
+
 func strictOutput(cmd *cobra.Command) bool {
 	toJSON, _ := cmd.Flags().GetBool(commonflags.JSON)
 	toProto, _ := cmd.Flags().GetBool("proto")