[#1612] neofs-cli: Remove RunE functions from commands

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-07-19 15:53:26 +03:00 committed by fyrchik
parent a52e7c2c99
commit 6049022f7e
2 changed files with 27 additions and 50 deletions

View file

@ -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 In this case --` + commonflags.RPC + ` flag should be specified and the epoch in bearer token
is set to current epoch + n. is set to current epoch + n.
`, `,
RunE: createToken, Run: createToken,
} }
func init() { func init() {
@ -56,28 +56,24 @@ func init() {
_ = cobra.MarkFlagRequired(createCmd.Flags(), outFlag) _ = 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) iat, iatRelative, err := common.ParseEpoch(cmd, issuedAtFlag)
if err != nil { common.ExitOnErr(cmd, "can't parse --"+issuedAtFlag+" flag: %w", err)
return err
}
exp, expRelative, err := common.ParseEpoch(cmd, commonflags.ExpireAt) exp, expRelative, err := common.ParseEpoch(cmd, commonflags.ExpireAt)
if err != nil { common.ExitOnErr(cmd, "can't parse --"+commonflags.ExpireAt+" flag: %w", err)
return err
}
nvb, nvbRelative, err := common.ParseEpoch(cmd, notValidBeforeFlag) nvb, nvbRelative, err := common.ParseEpoch(cmd, notValidBeforeFlag)
if err != nil { common.ExitOnErr(cmd, "can't parse --"+notValidBeforeFlag+" flag: %w", err)
return err
}
if iatRelative || expRelative || nvbRelative { if iatRelative || expRelative || nvbRelative {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel() defer cancel()
endpoint, _ := cmd.Flags().GetString(commonflags.RPC) endpoint, _ := cmd.Flags().GetString(commonflags.RPC)
currEpoch, err := internalclient.GetCurrentEpoch(ctx, endpoint) currEpoch, err := internalclient.GetCurrentEpoch(ctx, endpoint)
if err != nil { common.ExitOnErr(cmd, "can't fetch current epoch: %w", err)
return err
}
if iatRelative { if iatRelative {
iat += currEpoch iat += currEpoch
} }
@ -89,15 +85,14 @@ func createToken(cmd *cobra.Command, _ []string) error {
} }
} }
if exp < nvb { 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) ownerStr, _ := cmd.Flags().GetString(ownerFlag)
var ownerID user.ID var ownerID user.ID
if err := ownerID.DecodeString(ownerStr); err != nil { common.ExitOnErr(cmd, "can't parse recipient: %w", ownerID.DecodeString(ownerStr))
return fmt.Errorf("can't parse recipient: %w", err)
}
var b bearer.Token var b bearer.Token
b.SetExp(exp) b.SetExp(exp)
@ -109,12 +104,8 @@ func createToken(cmd *cobra.Command, _ []string) error {
if eaclPath != "" { if eaclPath != "" {
table := eaclSDK.NewTable() table := eaclSDK.NewTable()
raw, err := ioutil.ReadFile(eaclPath) raw, err := ioutil.ReadFile(eaclPath)
if err != nil { common.ExitOnErr(cmd, "can't read extended ACL file: %w", err)
return fmt.Errorf("can't read extended ACL file: %w", err) common.ExitOnErr(cmd, "can't parse extended ACL: %w", json.Unmarshal(raw, table))
}
if err := json.Unmarshal(raw, table); err != nil {
return fmt.Errorf("can't parse extended ACL: %w", err)
}
b.SetEACLTable(*table) b.SetEACLTable(*table)
} }
@ -123,17 +114,12 @@ func createToken(cmd *cobra.Command, _ []string) error {
toJSON, _ := cmd.Flags().GetBool(jsonFlag) toJSON, _ := cmd.Flags().GetBool(jsonFlag)
if toJSON { if toJSON {
data, err = json.Marshal(b) data, err = json.Marshal(b)
if err != nil { common.ExitOnErr(cmd, "can't mashal token to JSON: %w", err)
return fmt.Errorf("can't mashal token to JSON: %w", err)
}
} else { } else {
data = b.Marshal() data = b.Marshal()
} }
out, _ := cmd.Flags().GetString(outFlag) out, _ := cmd.Flags().GetString(outFlag)
if err := ioutil.WriteFile(out, data, 0644); err != nil { err = ioutil.WriteFile(out, data, 0644)
return fmt.Errorf("can't write token to file: %w", err) common.ExitOnErr(cmd, "can't write token to file: %w", err)
}
return nil
} }

View file

@ -6,6 +6,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" 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/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/network"
@ -26,7 +27,7 @@ const defaultLifetime = 10
var createCmd = &cobra.Command{ var createCmd = &cobra.Command{
Use: "create", Use: "create",
Short: "Create session token", Short: "Create session token",
RunE: createSession, Run: createSession,
PersistentPreRun: func(cmd *cobra.Command, _ []string) { PersistentPreRun: func(cmd *cobra.Command, _ []string) {
_ = viper.BindPFlag(commonflags.WalletPath, cmd.Flags().Lookup(commonflags.WalletPath)) _ = viper.BindPFlag(commonflags.WalletPath, cmd.Flags().Lookup(commonflags.WalletPath))
_ = viper.BindPFlag(commonflags.Account, cmd.Flags().Lookup(commonflags.Account)) _ = viper.BindPFlag(commonflags.Account, cmd.Flags().Lookup(commonflags.Account))
@ -47,19 +48,15 @@ func init() {
_ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.RPC) _ = cobra.MarkFlagRequired(createCmd.Flags(), commonflags.RPC)
} }
func createSession(cmd *cobra.Command, _ []string) error { func createSession(cmd *cobra.Command, _ []string) {
privKey := key.Get(cmd) privKey := key.Get(cmd)
var netAddr network.Address var netAddr network.Address
addrStr, _ := cmd.Flags().GetString(commonflags.RPC) addrStr, _ := cmd.Flags().GetString(commonflags.RPC)
if err := netAddr.FromString(addrStr); err != nil { common.ExitOnErr(cmd, "can't parse endpoint: %w", netAddr.FromString(addrStr))
return err
}
c, err := internalclient.GetSDKClient(privKey, netAddr) c, err := internalclient.GetSDKClient(privKey, netAddr)
if err != nil { common.ExitOnErr(cmd, "can't create client: %w", err)
return err
}
lifetime := uint64(defaultLifetime) lifetime := uint64(defaultLifetime)
if lfArg, _ := cmd.Flags().GetUint64(commonflags.Lifetime); lfArg != 0 { if lfArg, _ := cmd.Flags().GetUint64(commonflags.Lifetime); lfArg != 0 {
@ -69,26 +66,20 @@ func createSession(cmd *cobra.Command, _ []string) error {
var tok session.Object var tok session.Object
err = CreateSession(&tok, c, lifetime) err = CreateSession(&tok, c, lifetime)
if err != nil { common.ExitOnErr(cmd, "can't create session: %w", err)
return err
}
var data []byte var data []byte
if toJSON, _ := cmd.Flags().GetBool(jsonFlag); toJSON { if toJSON, _ := cmd.Flags().GetBool(jsonFlag); toJSON {
data, err = tok.MarshalJSON() data, err = tok.MarshalJSON()
if err != nil { common.ExitOnErr(cmd, "can't decode session token JSON: %w", err)
return fmt.Errorf("decode session token JSON: %w", err)
}
} else { } else {
data = tok.Marshal() data = tok.Marshal()
} }
filename, _ := cmd.Flags().GetString(outFlag) filename, _ := cmd.Flags().GetString(outFlag)
if err := ioutil.WriteFile(filename, data, 0644); err != nil { err = ioutil.WriteFile(filename, data, 0644)
return fmt.Errorf("can't write token to file: %w", err) common.ExitOnErr(cmd, "can't write token to file: %w", err)
}
return nil
} }
// CreateSession opens a new communication with NeoFS storage node using client connection. // CreateSession opens a new communication with NeoFS storage node using client connection.