Skip to content

Commit 1313dc6

Browse files
author
gregorgololicic
committed
string and json serialization of account
1 parent ca890c0 commit 1313dc6

3 files changed

Lines changed: 105 additions & 7 deletions

File tree

cmd/account/account.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
// NewCmdAccount account cmd factory
99
func NewCmdAccount() *cobra.Command {
1010
var accountCmd = &cobra.Command{
11-
Use: "account",
12-
Short: "Get account information, create account, add account key",
11+
Use: "account",
12+
Short: "Get account information, create account, add account key",
13+
Aliases: []string{"acc"},
14+
Args: cobra.ExactArgs(0),
1315
Long: `
1416
Manage and view flow account.
1517
`,

cmd/account/get/get.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,29 @@ func NewCmdGet() *cobra.Command {
1212
var (
1313
blockHeight int
1414
filter string
15+
json bool
1516
)
1617

1718
getCmd := &cobra.Command{
18-
Use: "get <address>",
19-
Short: "Gets an account by address",
19+
Use: "get <address>",
20+
Short: "Gets an account by address",
21+
Aliases: []string{"fetch", "g"},
2022
Long: `
2123
Gets an account by address (address, balance, keys, code)`,
2224
Args: cobra.ExactArgs(1),
2325
Run: func(cmd *cobra.Command, args []string) {
2426
gw := gateway.CreateGateway("")
25-
fmt.Println(gw.GetAccount(args[0]))
27+
account := gw.GetAccount(args[0])
28+
29+
if !json {
30+
fmt.Println(account.String(filter))
31+
} else {
32+
fmt.Println(account.JSON(filter))
33+
}
2634
},
2735
}
2836

37+
getCmd.PersistentFlags().BoolVarP(&json, "json", "j", false, "show output format as JSON")
2938
getCmd.Flags().IntVarP(&blockHeight, "block-height", "b", -1, "gets an account at the given block height")
3039
getCmd.Flags().StringVarP(&filter, "filter", "f", "", "filter output to show only provided field (address, balance, code, keys)")
3140

models/account.go

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package models
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
7+
"strings"
8+
"text/tabwriter"
59

610
"github.com/onflow/flow-go-sdk"
711
)
@@ -13,6 +17,89 @@ type Account struct {
1317
}
1418

1519
// String serializer for the account
16-
func (a *Account) String() string {
17-
return fmt.Sprintf("address: %s, balance: %d", a.Address.String(), a.Balance)
20+
func (a *Account) String(filter string) string {
21+
switch filter {
22+
case "":
23+
return a.stringAll()
24+
case "balance":
25+
return fmt.Sprintf("%d", a.Balance)
26+
case "code":
27+
return fmt.Sprintf("%s", a.Code)
28+
case "address":
29+
return fmt.Sprintf("%s", a.Address)
30+
case "keys":
31+
var b bytes.Buffer
32+
writer := tabwriter.NewWriter(&b, 0, 8, 1, '\t', tabwriter.AlignRight)
33+
a.stringKeys(writer)
34+
writer.Flush()
35+
return b.String()
36+
default:
37+
// todo error not valid filter option
38+
return ""
39+
}
40+
}
41+
42+
// serialize complete account to string
43+
func (a *Account) stringAll() string {
44+
// buffer to write to instead of io - eaiser testing and independed from medium
45+
var b bytes.Buffer
46+
writer := tabwriter.NewWriter(&b, 0, 8, 1, '\t', tabwriter.AlignRight)
47+
48+
fmt.Fprintf(writer, "\n")
49+
fmt.Fprintf(writer, "Address\t %s\n", a.Address)
50+
fmt.Fprintf(writer, "Balance\t %d\n", a.Balance)
51+
52+
fmt.Fprintf(writer, "Keys\t %d\n", len(a.Keys))
53+
a.stringKeys(writer)
54+
55+
fmt.Fprintf(writer, "\nCode\t\t %s", strings.ReplaceAll(string(a.Code), "\n", "\n\t "))
56+
fmt.Fprintf(writer, "\n")
57+
58+
writer.Flush()
59+
60+
return b.String()
61+
}
62+
63+
func (a *Account) stringKeys(writer *tabwriter.Writer) {
64+
for i, key := range a.Keys {
65+
fmt.Fprintf(writer, "\nKey %d\tPublic Key\t %x\n", i, key.PublicKey.Encode())
66+
fmt.Fprintf(writer, "\tWeight\t %d\n", key.Weight)
67+
fmt.Fprintf(writer, "\tSignature Algorithm\t %s\n", key.SigAlgo)
68+
fmt.Fprintf(writer, "\tHash Algorithm\t %s\n", key.HashAlgo)
69+
fmt.Fprintf(writer, "\n")
70+
}
71+
}
72+
73+
// JSON serializer for account
74+
func (a *Account) JSON(filter string) string {
75+
var account []byte
76+
var err error
77+
78+
switch filter {
79+
case "":
80+
account, err = json.Marshal(a)
81+
case "balance":
82+
account, err = json.Marshal(
83+
struct{ Balance uint64 }{Balance: a.Balance},
84+
)
85+
case "code":
86+
account, err = json.Marshal(
87+
struct{ Code []byte }{Code: a.Code},
88+
)
89+
case "address":
90+
account, err = json.Marshal(
91+
struct{ Address flow.Address }{Address: a.Address},
92+
)
93+
case "keys":
94+
account, err = json.Marshal(a.Keys)
95+
default:
96+
// todo error not valid filter option
97+
return ""
98+
}
99+
100+
if err != nil {
101+
fmt.Println("error serializing account")
102+
}
103+
104+
return string(account)
18105
}

0 commit comments

Comments
 (0)