diff --git a/cmd/neofs-cli/modules/accounting.go b/cmd/neofs-cli/modules/accounting.go index 8d22d5cd..27a00de0 100644 --- a/cmd/neofs-cli/modules/accounting.go +++ b/cmd/neofs-cli/modules/accounting.go @@ -1,8 +1,11 @@ package cmd import ( + "context" "fmt" + "math" + "github.com/nspcc-dev/neofs-api-go/pkg/accounting" "github.com/spf13/cobra" ) @@ -11,13 +14,39 @@ var accountingCmd = &cobra.Command{ Use: "accounting", Short: "Operations with accounts and balances", Long: `Operations with accounts and balances`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("accounting called") +} + +var accountingBalanceCmd = &cobra.Command{ + Use: "balance", + Short: "Get internal balance of NeoFS account", + Long: `Get internal balance of NeoFS account`, + RunE: func(cmd *cobra.Command, args []string) error { + var ( + response *accounting.Decimal + err error + ctx = context.Background() + ) + + cli, err := getSDKClient() + if err != nil { + return err + } + + response, err = cli.GetSelfBalance(ctx) + if err != nil { + return fmt.Errorf("rpc error: %w", err) + } + + // print to stdout + prettyPrintDecimal(response) + + return nil }, } func init() { rootCmd.AddCommand(accountingCmd) + accountingCmd.AddCommand(accountingBalanceCmd) // Here you will define your flags and configuration settings. @@ -29,3 +58,22 @@ func init() { // is called directly, e.g.: // accountingCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } + +func prettyPrintDecimal(decimal *accounting.Decimal) { + if decimal == nil { + return + } + + if verbose { + fmt.Println("value:", decimal.GetValue()) + fmt.Println("precision:", decimal.GetPrecision()) + } else { + // divider = 10^{precision}; v:365, p:2 => 365 / 10^2 = 3.65 + divider := math.Pow(10, float64(decimal.GetPrecision())) + + // %0.8f\n for precision 8 + format := fmt.Sprintf("%%0.%df\n", decimal.GetPrecision()) + + fmt.Printf(format, float64(decimal.GetValue())/divider) + } +} diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 48328a80..ba0a1355 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -7,6 +7,7 @@ import ( "os" "github.com/mitchellh/go-homedir" + "github.com/nspcc-dev/neofs-api-go/pkg/client" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/network" "github.com/spf13/cobra" @@ -111,3 +112,24 @@ func getEndpointAddress() (*network.Address, error) { return addr, nil } + +// getSDKClient returns default neofs-api-go sdk client. Consider using +// opts... to provide TTL or other global configuration flags. +func getSDKClient() (*client.Client, error) { + key, err := getKey() + if err != nil { + return nil, err + } + + netAddr, err := getEndpointAddress() + if err != nil { + return nil, err + } + + ipAddr, err := netAddr.IPAddrString() + if err != nil { + return nil, errInvalidEndpoint + } + + return client.New(key, client.WithAddress(ipAddr)) +}