From 6049022f7e155c311d6d6c4f70b08583616ca38a Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 19 Jul 2022 15:53:26 +0300 Subject: [PATCH] [#1612] neofs-cli: Remove `RunE` functions from commands Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/modules/bearer/create.go | 50 +++++++++---------------- cmd/neofs-cli/modules/session/create.go | 27 +++++-------- 2 files changed, 27 insertions(+), 50 deletions(-) diff --git a/cmd/neofs-cli/modules/bearer/create.go b/cmd/neofs-cli/modules/bearer/create.go index ca3210bc..9f2a4b50 100644 --- a/cmd/neofs-cli/modules/bearer/create.go +++ b/cmd/neofs-cli/modules/bearer/create.go @@ -34,7 +34,7 @@ All epoch flags can be specified relative to the current epoch with the +n synta In this case --` + commonflags.RPC + ` flag should be specified and the epoch in bearer token is set to current epoch + n. `, - RunE: createToken, + Run: createToken, } func init() { @@ -56,28 +56,24 @@ func init() { _ = cobra.MarkFlagRequired(createCmd.Flags(), outFlag) } -func createToken(cmd *cobra.Command, _ []string) error { +func createToken(cmd *cobra.Command, _ []string) { iat, iatRelative, err := common.ParseEpoch(cmd, issuedAtFlag) - if err != nil { - return err - } + common.ExitOnErr(cmd, "can't parse --"+issuedAtFlag+" flag: %w", err) + exp, expRelative, err := common.ParseEpoch(cmd, commonflags.ExpireAt) - if err != nil { - return err - } + common.ExitOnErr(cmd, "can't parse --"+commonflags.ExpireAt+" flag: %w", err) + nvb, nvbRelative, err := common.ParseEpoch(cmd, notValidBeforeFlag) - if err != nil { - return err - } + common.ExitOnErr(cmd, "can't parse --"+notValidBeforeFlag+" flag: %w", err) + if iatRelative || expRelative || nvbRelative { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() endpoint, _ := cmd.Flags().GetString(commonflags.RPC) currEpoch, err := internalclient.GetCurrentEpoch(ctx, endpoint) - if err != nil { - return err - } + common.ExitOnErr(cmd, "can't fetch current epoch: %w", err) + if iatRelative { iat += currEpoch } @@ -89,15 +85,14 @@ func createToken(cmd *cobra.Command, _ []string) error { } } if exp < nvb { - return fmt.Errorf("expiration epoch is less than not-valid-before epoch: %d < %d", exp, nvb) + common.ExitOnErr(cmd, "", + fmt.Errorf("expiration epoch is less than not-valid-before epoch: %d < %d", exp, nvb)) } ownerStr, _ := cmd.Flags().GetString(ownerFlag) var ownerID user.ID - if err := ownerID.DecodeString(ownerStr); err != nil { - return fmt.Errorf("can't parse recipient: %w", err) - } + common.ExitOnErr(cmd, "can't parse recipient: %w", ownerID.DecodeString(ownerStr)) var b bearer.Token b.SetExp(exp) @@ -109,12 +104,8 @@ func createToken(cmd *cobra.Command, _ []string) error { if eaclPath != "" { table := eaclSDK.NewTable() raw, err := ioutil.ReadFile(eaclPath) - if err != nil { - return fmt.Errorf("can't read extended ACL file: %w", err) - } - if err := json.Unmarshal(raw, table); err != nil { - return fmt.Errorf("can't parse extended ACL: %w", err) - } + common.ExitOnErr(cmd, "can't read extended ACL file: %w", err) + common.ExitOnErr(cmd, "can't parse extended ACL: %w", json.Unmarshal(raw, table)) b.SetEACLTable(*table) } @@ -123,17 +114,12 @@ func createToken(cmd *cobra.Command, _ []string) error { toJSON, _ := cmd.Flags().GetBool(jsonFlag) if toJSON { data, err = json.Marshal(b) - if err != nil { - return fmt.Errorf("can't mashal token to JSON: %w", err) - } + common.ExitOnErr(cmd, "can't mashal token to JSON: %w", err) } else { data = b.Marshal() } out, _ := cmd.Flags().GetString(outFlag) - if err := ioutil.WriteFile(out, data, 0644); err != nil { - return fmt.Errorf("can't write token to file: %w", err) - } - - return nil + err = ioutil.WriteFile(out, data, 0644) + common.ExitOnErr(cmd, "can't write token to file: %w", err) } diff --git a/cmd/neofs-cli/modules/session/create.go b/cmd/neofs-cli/modules/session/create.go index 45d81b29..615c7293 100644 --- a/cmd/neofs-cli/modules/session/create.go +++ b/cmd/neofs-cli/modules/session/create.go @@ -6,6 +6,7 @@ import ( "github.com/google/uuid" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" "github.com/nspcc-dev/neofs-node/pkg/network" @@ -26,7 +27,7 @@ const defaultLifetime = 10 var createCmd = &cobra.Command{ Use: "create", Short: "Create session token", - RunE: createSession, + Run: createSession, PersistentPreRun: func(cmd *cobra.Command, _ []string) { _ = viper.BindPFlag(commonflags.WalletPath, cmd.Flags().Lookup(commonflags.WalletPath)) _ = viper.BindPFlag(commonflags.Account, cmd.Flags().Lookup(commonflags.Account)) @@ -47,19 +48,15 @@ func init() { _ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.RPC) } -func createSession(cmd *cobra.Command, _ []string) error { +func createSession(cmd *cobra.Command, _ []string) { privKey := key.Get(cmd) var netAddr network.Address addrStr, _ := cmd.Flags().GetString(commonflags.RPC) - if err := netAddr.FromString(addrStr); err != nil { - return err - } + common.ExitOnErr(cmd, "can't parse endpoint: %w", netAddr.FromString(addrStr)) c, err := internalclient.GetSDKClient(privKey, netAddr) - if err != nil { - return err - } + common.ExitOnErr(cmd, "can't create client: %w", err) lifetime := uint64(defaultLifetime) if lfArg, _ := cmd.Flags().GetUint64(commonflags.Lifetime); lfArg != 0 { @@ -69,26 +66,20 @@ func createSession(cmd *cobra.Command, _ []string) error { var tok session.Object err = CreateSession(&tok, c, lifetime) - if err != nil { - return err - } + common.ExitOnErr(cmd, "can't create session: %w", err) var data []byte if toJSON, _ := cmd.Flags().GetBool(jsonFlag); toJSON { data, err = tok.MarshalJSON() - if err != nil { - return fmt.Errorf("decode session token JSON: %w", err) - } + common.ExitOnErr(cmd, "can't decode session token JSON: %w", err) } else { data = tok.Marshal() } filename, _ := cmd.Flags().GetString(outFlag) - if err := ioutil.WriteFile(filename, data, 0644); err != nil { - return fmt.Errorf("can't write token to file: %w", err) - } - return nil + err = ioutil.WriteFile(filename, data, 0644) + common.ExitOnErr(cmd, "can't write token to file: %w", err) } // CreateSession opens a new communication with NeoFS storage node using client connection.