account get command implementation
package accounts
import (
- "encoding/json"
- "errors"
+ "bytes"
"fmt"
"github.com/onflow/flow-cli/cmd"
"github.com/onflow/flow-cli/flow/cli"
- "github.com/onflow/flow-cli/services"
+ "github.com/onflow/flow-cli/sharedlib/services"
"github.com/onflow/flow-go-sdk"
"github.com/psiemens/sconfig"
"github.com/spf13/cobra"
+ "strings"
+ "text/tabwriter"
)
type Flags struct {
@@ -34,41 +35,52 @@ func Init() cmd.Command {
}
}
+func (a *AccountCmd) Run(cmd *cobra.Command, args []string, project *cli.Project, services services.Service) (cmd.Result, error) {
+ account, err := services.GetAccount(args[0])
+ return &Result{account}, err
+}
+
func (a *AccountCmd) SetFlags() *sconfig.Config {
return sconfig.New(&a.flags)
}
-func (a *AccountCmd) ValidateFlags() error {
- return nil
+func (a *AccountCmd) GetCmd() *cobra.Command {
+ return a.cmd
}
-func (a *AccountCmd) Run(cmd *cobra.Command, args []string, project *cli.Project) (cmd.Result, error) {
- host := a.flags.Host
+type Result struct {
+ *flow.Account
+}
- if host == "" && project != nil {
- host = project.Host("emulator")
- } else if host == "" {
- return nil, errors.New("Host must be provided using --host flag or by initializing project: flow project init")
+func (r *Result) JSON() interface{} {
+ return r
}
- account, err := services.GetAccount(host, args[0])
+func (r *Result) String() string {
+ var b bytes.Buffer
+ writer := tabwriter.NewWriter(&b, 0, 8, 1, '\t', tabwriter.AlignRight)
- return &Result{account}, err
-}
+ fmt.Fprintf(writer, "Address\t %s\n", r.Address)
+ fmt.Fprintf(writer, "Balance\t %d\n", r.Balance)
-func (a *AccountCmd) GetCmd() *cobra.Command {
- return a.cmd
-}
+ fmt.Fprintf(writer, "Keys\t %d\n", len(r.Keys))
-type Result struct {
- *flow.Account
+ for i, key := range r.Keys {
+ fmt.Fprintf(writer, "\nKey %d\tPublic Key\t %x\n", i, key.PublicKey.Encode())
+ fmt.Fprintf(writer, "\tWeight\t %d\n", key.Weight)
+ fmt.Fprintf(writer, "\tSignature Algorithm\t %s\n", key.SigAlgo)
+ fmt.Fprintf(writer, "\tHash Algorithm\t %s\n", key.HashAlgo)
+ fmt.Fprintf(writer, "\n")
}
-func (r *Result) JSON() string {
- val, _ := json.Marshal(r.Account)
- return string(val)
+ fmt.Fprintf(writer, "\nCode\t\t %s", strings.ReplaceAll(string(r.Code), "\n", "\n\t "))
+ fmt.Fprintf(writer, "\n")
+
+ writer.Flush()
+
+ return b.String()
}
-func (r *Result) String() string {
- return fmt.Sprintf("address: %s balance: %v", r.Account.Address.String(), r.Account.Balance)
+func (r *Result) Oneliner() string {
+ return fmt.Sprintf("Address: %s, Balance: %v, Keys: %s", r.Address, r.Balance, r.Keys[0].PublicKey)
}
