[#76] Implement accounting.balance command in CLI

It prints result as a float type value. With verbose flag
it prints decimal precision and value.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-12 17:19:57 +03:00 committed by Stanislav Bogatyrev
parent 84928527d7
commit 80cd4039a8
2 changed files with 72 additions and 2 deletions

View file

@ -1,8 +1,11 @@
package cmd package cmd
import ( import (
"context"
"fmt" "fmt"
"math"
"github.com/nspcc-dev/neofs-api-go/pkg/accounting"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -11,13 +14,39 @@ var accountingCmd = &cobra.Command{
Use: "accounting", Use: "accounting",
Short: "Operations with accounts and balances", Short: "Operations with accounts and balances",
Long: `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() { func init() {
rootCmd.AddCommand(accountingCmd) rootCmd.AddCommand(accountingCmd)
accountingCmd.AddCommand(accountingBalanceCmd)
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.
@ -29,3 +58,22 @@ func init() {
// is called directly, e.g.: // is called directly, e.g.:
// accountingCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") // 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)
}
}

View file

@ -7,6 +7,7 @@ import (
"os" "os"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
crypto "github.com/nspcc-dev/neofs-crypto" crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/network"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -111,3 +112,24 @@ func getEndpointAddress() (*network.Address, error) {
return addr, nil 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))
}