Skip to content

Commit

Permalink
refactor: moved terminal.go from aeternity/ to cmd/, getErrorReason()…
Browse files Browse the repository at this point in the history
… was only used by api.go so moved there instead
  • Loading branch information
randomshinichi committed Jul 26, 2019
1 parent 361aea5 commit c769bc0
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 75 deletions.
28 changes: 28 additions & 0 deletions aeternity/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,39 @@ package aeternity

import (
"fmt"
"reflect"

"github.com/aeternity/aepp-sdk-go/swagguard/node/client/external"
"github.com/aeternity/aepp-sdk-go/swagguard/node/models"
)

func getErrorReason(v interface{}) (msg string) {
var p func(v reflect.Value) (msg string)
p = func(v reflect.Value) (msg string) {
switch v.Kind() {
// If it is a pointer we need to unwrap and call once again
case reflect.Ptr:
if v.IsValid() {
msg = p(v.Elem())
}
case reflect.Struct:
if v.Type() == reflect.TypeOf(models.Error{}) {
msg = fmt.Sprint(reflect.Indirect(v.FieldByName("Reason")))
break
}
for i := 0; i < v.NumField(); i++ {
msg = p(v.Field(i))
}
}
return
}
msg = p(reflect.ValueOf(v))
if len(msg) == 0 {
msg = fmt.Sprint(v)
}
return
}

// GetStatus post transaction
func (c *Node) GetStatus() (status *models.Status, err error) {
r, err := c.External.GetStatus(nil)
Expand Down
27 changes: 0 additions & 27 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,6 @@ func BroadcastTransaction(c transactionPoster, txSignedBase64 string) (err error
return
}

// PrintGenerationByHeight utility function to print a generation by it's height
// TODO this belongs in cmd and needs to be tested with error cases
func (c *Node) PrintGenerationByHeight(height uint64) {
r, err := c.GetGenerationByHeight(height)
if err == nil {
PrintObject("generation", r)
// search for transaction in the microblocks
for _, mbh := range r.MicroBlocks {
// get the microblok
mbhs := fmt.Sprint(mbh)
r, err := c.GetMicroBlockTransactionsByHash(mbhs)
if err != nil {
Pp("Error:", err)
}
// go through all the hashes
for _, btx := range r.Transactions {
p, err := c.GetTransactionByHash(fmt.Sprint(btx.Hash))
if err == nil {
PrintObject("transaction", p)
}
}
}
} else {
fmt.Println("Something went wrong in PrintGenerationByHeight")
}
}

// Context stores relevant context (node connection, account address) that one might not want to spell out each time one creates a transaction
type Context struct {
Client *Node
Expand Down
12 changes: 6 additions & 6 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func addressFunc(cmd *cobra.Command, args []string) error {
return err
}

aeternity.Pp("Account address", account.Address)
Pp("Account address", account.Address)
if printPrivateKey {
if AskYes("Are you sure you want to print your private key? This could be insecure.", false) {
aeternity.Pp("Account private key", account.SigningKeyToHexString())
Pp("Account private key", account.SigningKeyToHexString())
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ func createFunc(cmd *cobra.Command, args []string) (err error) {
return err
}

aeternity.Pp(
Pp(
"Wallet path", f,
"Account address", account.Address,
)
Expand Down Expand Up @@ -141,7 +141,7 @@ func balanceFunc(cmd *cobra.Command, args []string) (err error) {
return err
}

aeternity.PrintObject("account", a)
PrintObject("account", a)
return nil
}

Expand Down Expand Up @@ -169,7 +169,7 @@ func signFunc(cmd *cobra.Command, args []string) (err error) {
return err
}

aeternity.Pp(
Pp(
"Signing account address", account.Address,
"Signature", signature,
"Unsigned", txUnsignedBase64,
Expand Down Expand Up @@ -202,7 +202,7 @@ func saveFunc(cmd *cobra.Command, args []string) (err error) {
return err
}

aeternity.Pp("Keystore path ", f)
Pp("Keystore path ", f)

return nil
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func topFunc(cmd *cobra.Command, args []string) (err error) {
if err != nil {
return err
}
aeternity.PrintObject("block", v)
PrintObject("block", v)
return nil
}

Expand Down Expand Up @@ -73,7 +73,7 @@ func statusFunc(cmd *cobra.Command, args []string) (err error) {
if err != nil {
return err
}
aeternity.PrintObject("node", v)
PrintObject("node", v)
return nil
}

Expand Down Expand Up @@ -116,7 +116,7 @@ func playFunc(cmd *cobra.Command, args []string) (err error) {
}
// run the play
for ; blockHeight > targetHeight; blockHeight-- {
aeNode.PrintGenerationByHeight(blockHeight)
PrintGenerationByHeight(aeNode, blockHeight)
fmt.Println("")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func printResult(title string, v interface{}, err error) {
fmt.Println(err)
os.Exit(1)
}
aeternity.PrintObject(title, v)
PrintObject(title, v)
}

func inspectFunc(cmd *cobra.Command, args []string) (err error) {
Expand All @@ -66,7 +66,7 @@ func inspectFunc(cmd *cobra.Command, args []string) (err error) {
// height
if matched, _ := regexp.MatchString(`^\d+$`, object); matched {
height, _ := strconv.ParseUint(object, 10, 64)
aeNode.PrintGenerationByHeight(height)
PrintGenerationByHeight(aeNode, height)
continue
}
// name
Expand Down
70 changes: 36 additions & 34 deletions aeternity/terminal.go → cmd/terminal.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package aeternity
package cmd

import (
"encoding/json"
Expand All @@ -8,6 +8,7 @@ import (

"time"

"github.com/aeternity/aepp-sdk-go/aeternity"
"github.com/aeternity/aepp-sdk-go/swagguard/node/models"
)

Expand Down Expand Up @@ -98,41 +99,9 @@ func printIf(title string, v interface{}) {
p(title, "", reflect.ValueOf(v), 0)
}

func getErrorReason(v interface{}) (msg string) {
var p func(v reflect.Value) (msg string)
p = func(v reflect.Value) (msg string) {
switch v.Kind() {
// If it is a pointer we need to unwrap and call once again
case reflect.Ptr:
if v.IsValid() {
msg = p(v.Elem())
}
case reflect.Struct:
if v.Type() == reflect.TypeOf(models.Error{}) {
msg = fmt.Sprint(reflect.Indirect(v.FieldByName("Reason")))
break
}
for i := 0; i < v.NumField(); i++ {
msg = p(v.Field(i))
}
}
return
}
msg = p(reflect.ValueOf(v))
if len(msg) == 0 {
msg = fmt.Sprint(v)
}
return
}

// PrintError print error
func PrintError(code string, e *models.Error) {
Pp(code, e.Reason)
}

// PrintObject pretty print an object obtained from the api with a title
func PrintObject(title string, i interface{}) {
if Config.Tuning.OutputFormatJSON {
if aeternity.Config.Tuning.OutputFormatJSON {
j, _ := json.MarshalIndent(i, "", " ")
fmt.Printf("%s\n", j)
return
Expand All @@ -142,3 +111,36 @@ func PrintObject(title string, i interface{}) {
print("\n")

}

type getGenerationMicroBlockTransactioner interface {
GetGenerationByHeight(height uint64) (g *models.Generation, err error)
GetMicroBlockTransactionsByHash(microBlockID string) (txs *models.GenericTxs, err error)
GetTransactionByHash(txHash string) (tx *models.GenericSignedTx, err error)
}

// PrintGenerationByHeight utility function to print a generation by it's height
// TODO needs to be tested with error cases
func PrintGenerationByHeight(c getGenerationMicroBlockTransactioner, height uint64) {
r, err := c.GetGenerationByHeight(height)
if err == nil {
PrintObject("generation", r)
// search for transaction in the microblocks
for _, mbh := range r.MicroBlocks {
// get the microblok
mbhs := fmt.Sprint(mbh)
r, err := c.GetMicroBlockTransactionsByHash(mbhs)
if err != nil {
Pp("Error:", err)
}
// go through all the hashes
for _, btx := range r.Transactions {
p, err := c.GetTransactionByHash(fmt.Sprint(btx.Hash))
if err == nil {
PrintObject("transaction", p)
}
}
}
} else {
fmt.Println("Something went wrong in PrintGenerationByHeight")
}
}
2 changes: 1 addition & 1 deletion aeternity/terminal_test.go → cmd/terminal_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package aeternity
package cmd

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func txSpendFunc(cmd *cobra.Command, args []string) (err error) {
}

// Sender, Recipient, Amount, Ttl, Fee, Nonce, Payload, Encoded
aeternity.Pp(
Pp(
"Sender acount", sender,
"Recipient account", recipient,
"Amount", amount,
Expand Down Expand Up @@ -130,7 +130,7 @@ func txContractCreateFunc(cmd *cobra.Command, args []string) (err error) {
return err
}

aeternity.Pp(
Pp(
"OwnerID", tx.OwnerID,
"AccountNonce", tx.AccountNonce,
"Code", tx.Code,
Expand Down

0 comments on commit c769bc0

Please sign in to comment.