[#165] cli: Refactor get command
Resolve funlen linter for getObject method Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
3c7ed21f74
commit
c78e9cc857
1 changed files with 25 additions and 15 deletions
|
@ -42,27 +42,15 @@ func initObjectGetCmd() {
|
||||||
flags.Bool(binaryFlag, false, "Serialize whole object structure into given file(id + signature + header + payload).")
|
flags.Bool(binaryFlag, false, "Serialize whole object structure into given file(id + signature + header + payload).")
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: funlen
|
|
||||||
func getObject(cmd *cobra.Command, _ []string) {
|
func getObject(cmd *cobra.Command, _ []string) {
|
||||||
var cnr cid.ID
|
var cnr cid.ID
|
||||||
var obj oid.ID
|
var obj oid.ID
|
||||||
|
|
||||||
objAddr := readObjectAddress(cmd, &cnr, &obj)
|
objAddr := readObjectAddress(cmd, &cnr, &obj)
|
||||||
|
|
||||||
var out io.Writer
|
|
||||||
filename := cmd.Flag(fileFlag).Value.String()
|
filename := cmd.Flag(fileFlag).Value.String()
|
||||||
if filename == "" {
|
out, closer := createOutWriter(cmd, filename)
|
||||||
out = os.Stdout
|
defer closer()
|
||||||
} 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
|
|
||||||
}
|
|
||||||
|
|
||||||
pk := key.GetOrGenerate(cmd)
|
pk := key.GetOrGenerate(cmd)
|
||||||
|
|
||||||
|
@ -114,6 +102,10 @@ func getObject(cmd *cobra.Command, _ []string) {
|
||||||
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
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 {
|
if binary {
|
||||||
objToStore := res.Header()
|
objToStore := res.Header()
|
||||||
// TODO(@acid-ant): #1932 Use streams to marshal/unmarshal payload
|
// 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.
|
// Print header only if file is not streamed to stdout.
|
||||||
if filename != "" {
|
if filename != "" {
|
||||||
err = printHeader(cmd, res.Header())
|
err := printHeader(cmd, res.Header())
|
||||||
commonCmd.ExitOnErr(cmd, "", err)
|
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 {
|
func strictOutput(cmd *cobra.Command) bool {
|
||||||
toJSON, _ := cmd.Flags().GetBool(commonflags.JSON)
|
toJSON, _ := cmd.Flags().GetBool(commonflags.JSON)
|
||||||
toProto, _ := cmd.Flags().GetBool("proto")
|
toProto, _ := cmd.Flags().GetBool("proto")
|
||||||
|
|
Loading…
Reference in a new issue