Skip to content

Commit 2056867

Browse files
committed
cli/object: Share file open code in get and range commands
Signed-off-by: Leonard Lyubich <[email protected]>
1 parent 750d53d commit 2056867

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

cmd/neofs-cli/modules/object/get.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ func getObject(cmd *cobra.Command, _ []string) error {
5757
if filename == "" {
5858
out = os.Stdout
5959
} else {
60-
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
60+
f, err := openFileForPayload(filename)
6161
if err != nil {
62-
return fmt.Errorf("can't open file '%s': %w", filename, err)
62+
return err
6363
}
6464

6565
defer func() { _ = f.Close() }()

cmd/neofs-cli/modules/object/range.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ func getObjectRange(cmd *cobra.Command, _ []string) error {
6969
if filename == "" {
7070
out = os.Stdout
7171
} else {
72-
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
72+
f, err := openFileForPayload(filename)
7373
if err != nil {
74-
return fmt.Errorf("can't open file '%s': %w", filename, err)
74+
return err
7575
}
7676

7777
defer f.Close()

cmd/neofs-cli/modules/object/util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/ecdsa"
66
"errors"
77
"fmt"
8+
"io"
89
"os"
910
"strings"
1011

@@ -361,3 +362,11 @@ func finalizeSession(cmd *cobra.Command, dst SessionPrm, tok *session.Object, ke
361362
func initFlagSession(cmd *cobra.Command, verb string) {
362363
commonflags.InitSession(cmd, "object "+verb)
363364
}
365+
366+
func openFileForPayload(name string) (io.WriteCloser, error) {
367+
f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
368+
if err != nil {
369+
return nil, fmt.Errorf("can't open file '%s': %w", name, err)
370+
}
371+
return f, nil
372+
}

0 commit comments

Comments
 (0)