cosmos-sdk/x/auth/client/cli/account.go

62 lines
1.3 KiB
Go
Raw Normal View History

2018-04-25 07:18:06 -07:00
package cli
2018-02-28 17:57:38 -08:00
import (
"fmt"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
2018-02-28 17:57:38 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
)
2018-08-06 11:11:30 -07:00
// GetAccountCmd returns a query account that will display the state of the
// account at a given address.
2018-08-31 15:22:37 -07:00
// nolint: unparam
func GetAccountCmd(storeName string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
2018-04-23 08:47:39 -07:00
Use: "account [address]",
2018-02-28 17:57:38 -08:00
Short: "Query account balance",
2018-04-23 08:47:39 -07:00
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// find the key to look up the account
addr := args[0]
2018-07-09 16:06:05 -07:00
key, err := sdk.AccAddressFromBech32(addr)
2018-04-23 08:47:39 -07:00
if err != nil {
return err
}
2018-03-30 05:57:53 -07:00
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(cdc)
2018-02-28 17:57:38 -08:00
if err = cliCtx.EnsureAccountExistsFromAddr(key); err != nil {
2018-08-06 11:11:30 -07:00
return err
}
2018-08-06 11:11:30 -07:00
acc, err := cliCtx.GetAccount(key)
2018-04-23 08:47:39 -07:00
if err != nil {
return err
}
2018-02-28 17:57:38 -08:00
var output []byte
if cliCtx.Indent {
output, err = cdc.MarshalJSONIndent(acc, "", " ")
} else {
output, err = cdc.MarshalJSON(acc)
}
2018-04-23 08:47:39 -07:00
if err != nil {
return err
}
2018-08-06 11:11:30 -07:00
2018-04-23 08:47:39 -07:00
fmt.Println(string(output))
return nil
},
2018-02-28 17:57:38 -08:00
}
// Add the flags here and return the command
return client.GetCommands(cmd)[0]
2018-02-28 17:57:38 -08:00
}