[#1379] neofs-cli: Move accounting command to a separate package

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-18 14:17:31 +03:00 committed by fyrchik
parent 56f33436dd
commit c6325fdc91
3 changed files with 44 additions and 42 deletions

View file

@ -1,4 +1,4 @@
package cmd package accounting
import ( import (
"math/big" "math/big"
@ -7,6 +7,7 @@ import (
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/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/pkg/util/precision" "github.com/nspcc-dev/neofs-node/pkg/util/precision"
"github.com/nspcc-dev/neofs-sdk-go/accounting" "github.com/nspcc-dev/neofs-sdk-go/accounting"
"github.com/nspcc-dev/neofs-sdk-go/owner" "github.com/nspcc-dev/neofs-sdk-go/owner"
@ -14,24 +15,10 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var ( const (
balanceOwner string ownerFlag = "owner"
) )
// accountingCmd represents the accounting command
var accountingCmd = &cobra.Command{
Use: "accounting",
Short: "Operations with accounts and balances",
Long: `Operations with accounts and balances`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
flags := cmd.Flags()
_ = viper.BindPFlag(commonflags.WalletPath, flags.Lookup(commonflags.WalletPath))
_ = viper.BindPFlag(commonflags.Account, flags.Lookup(commonflags.Account))
_ = viper.BindPFlag(commonflags.RPC, flags.Lookup(commonflags.RPC))
},
}
var accountingBalanceCmd = &cobra.Command{ var accountingBalanceCmd = &cobra.Command{
Use: "balance", Use: "balance",
Short: "Get internal balance of NeoFS account", Short: "Get internal balance of NeoFS account",
@ -39,19 +26,23 @@ var accountingBalanceCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var oid *owner.ID var oid *owner.ID
key, err := getKey() pk, err := key.GetOrGenerate()
common.ExitOnErr(cmd, "", err) common.ExitOnErr(cmd, "", err)
balanceOwner, _ := cmd.Flags().GetString(ownerFlag)
if balanceOwner == "" { if balanceOwner == "" {
oid = owner.NewIDFromPublicKey(&key.PublicKey) oid = owner.NewIDFromPublicKey(&pk.PublicKey)
} else { } else {
oid, err = ownerFromString(balanceOwner) oid := owner.NewID()
common.ExitOnErr(cmd, "", err) err := oid.Parse(balanceOwner)
common.ExitOnErr(cmd, "can't decode owner ID wallet address: %w", err)
} }
var prm internalclient.BalanceOfPrm cli, err := internalclient.GetSDKClientByFlag(pk, commonflags.RPC)
common.ExitOnErr(cmd, "create API client: %w", err)
prepareAPIClientWithKey(cmd, key, &prm) var prm internalclient.BalanceOfPrm
prm.SetClient(cli)
prm.SetAccount(*oid) prm.SetAccount(*oid)
res, err := internalclient.BalanceOf(prm) res, err := internalclient.BalanceOf(prm)
@ -68,25 +59,7 @@ func initAccountingBalanceCmd() {
ff.StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage) ff.StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage)
ff.StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage) ff.StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage)
ff.StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage) ff.StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage)
ff.String(ownerFlag, "", "owner of balance account (omit to use owner from private key)")
accountingBalanceCmd.Flags().StringVar(&balanceOwner, "owner", "", "owner of balance account (omit to use owner from private key)")
}
func init() {
rootCmd.AddCommand(accountingCmd)
accountingCmd.AddCommand(accountingBalanceCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// accountingCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// accountingCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
initAccountingBalanceCmd()
} }
func prettyPrintDecimal(cmd *cobra.Command, decimal *accounting.Decimal) { func prettyPrintDecimal(cmd *cobra.Command, decimal *accounting.Decimal) {

View file

@ -0,0 +1,27 @@
package accounting
import (
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Cmd represents the accounting command
var Cmd = &cobra.Command{
Use: "accounting",
Short: "Operations with accounts and balances",
Long: `Operations with accounts and balances`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
flags := cmd.Flags()
_ = viper.BindPFlag(commonflags.WalletPath, flags.Lookup(commonflags.WalletPath))
_ = viper.BindPFlag(commonflags.Account, flags.Lookup(commonflags.Account))
_ = viper.BindPFlag(commonflags.RPC, flags.Lookup(commonflags.RPC))
},
}
func init() {
Cmd.AddCommand(accountingBalanceCmd)
initAccountingBalanceCmd()
}

View file

@ -13,6 +13,7 @@ import (
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "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"
accountingCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/accounting"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl"
bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer" bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer"
sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session" sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session"
@ -91,6 +92,7 @@ func init() {
rootCmd.AddCommand(acl.Cmd) rootCmd.AddCommand(acl.Cmd)
rootCmd.AddCommand(bearerCli.Cmd) rootCmd.AddCommand(bearerCli.Cmd)
rootCmd.AddCommand(sessionCli.Cmd) rootCmd.AddCommand(sessionCli.Cmd)
rootCmd.AddCommand(accountingCli.Cmd)
} }
func entryPoint(cmd *cobra.Command, _ []string) { func entryPoint(cmd *cobra.Command, _ []string) {