Skip to content

Commit

Permalink
cmd: chain use error return objects
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Feb 28, 2019
1 parent c396532 commit 5093af2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 37 deletions.
64 changes: 35 additions & 29 deletions cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package cmd

import (
"errors"
"fmt"
"os"

"github.com/aeternity/aepp-sdk-go/aeternity"
"github.com/spf13/cobra"
Expand All @@ -33,56 +33,55 @@ var topCmd = &cobra.Command{
Use: "top",
Short: "Query the top block of the chain",
Long: ``,
Run: topFunc,
RunE: topFunc,
}

func topFunc(cmd *cobra.Command, args []string) {
func topFunc(cmd *cobra.Command, args []string) (err error) {
aeCli := NewAeCli()
v, err := aeCli.APIGetTopBlock()
if err != nil {
fmt.Println(err)
os.Exit(1)
return err
}
aeternity.PrintObject("block", v)
return nil
}

var statusCmd = &cobra.Command{
Use: "status",
Short: "Get the status and status of the node running the chain",
Long: ``,
Run: statusFunc,
RunE: statusFunc,
}

func statusFunc(cmd *cobra.Command, args []string) {
func statusFunc(cmd *cobra.Command, args []string) (err error) {
aeCli := NewAeCli()
v, err := aeCli.APIGetStatus()
if err != nil {
fmt.Println(err)
os.Exit(1)
return err
}
aeternity.PrintObject("epoch node", v)
return nil
}

var limit, startFromHeight uint64
var playCmd = &cobra.Command{
Use: "play",
Short: "Query the blocks of the chain one after the other",
Long: ``,
Run: playFunc,
RunE: playFunc,
}

func playFunc(cmd *cobra.Command, args []string) {
func playFunc(cmd *cobra.Command, args []string) (err error) {
aeCli := NewAeCli()
blockHeight, err := aeCli.APIGetHeight()
if err != nil {
fmt.Println(err)
os.Exit(1)
return err
}

// deal with the height parameter
if startFromHeight > blockHeight {
fmt.Printf("Height (%d) is greater that the top block (%d)", startFromHeight, blockHeight)
os.Exit(1)
err := fmt.Errorf("Height (%d) is greater that the top block (%d)", startFromHeight, blockHeight)
return err
}

if startFromHeight > 0 {
Expand All @@ -101,65 +100,72 @@ func playFunc(cmd *cobra.Command, args []string) {
aeCli.PrintGenerationByHeight(blockHeight)
fmt.Println("")
}

return nil
}

var broadcastCmd = &cobra.Command{
Use: "broadcast SIGNED_TRANSACTION",
Short: "Broadcast a transaction to the network",
Long: ``,
Args: cobra.ExactArgs(1),
Run: broadcastFunc,
RunE: broadcastFunc,
}

func broadcastFunc(cmd *cobra.Command, args []string) {
func broadcastFunc(cmd *cobra.Command, args []string) (err error) {
// Load variables from arguments
txSignedBase64 := args[0]

if len(txSignedBase64) == 0 || txSignedBase64[0:3] != "tx_" {
fmt.Println("Error, missing or invalid recipient address")
os.Exit(1)
err := errors.New("Error, missing or invalid recipient address")
return err
}

err := aeternity.BroadcastTransaction(txSignedBase64)
err = aeternity.BroadcastTransaction(txSignedBase64)
if err != nil {
fmt.Println("Error while broadcasting transaction: ", err)
errFinal := fmt.Errorf("Error while broadcasting transaction: %v", err)
return errFinal
}

return nil
}

var ttlCmd = &cobra.Command{
Use: "ttl",
Short: "Get the absolute TTL for a Transaction",
Long: `Get the absolute TTL (node's height + recommended TTL offset) for a Transaction`,
Args: cobra.ExactArgs(0),
Run: ttlFunc,
RunE: ttlFunc,
}

func ttlFunc(cmd *cobra.Command, args []string) {
func ttlFunc(cmd *cobra.Command, args []string) (err error) {
ae := NewAeCli()
height, err := ae.APIGetHeight()
if err != nil {
fmt.Printf("Error getting height from the node: %v", err)
return
errFinal := fmt.Errorf("Error getting height from the node: %v", err)
return errFinal
}
fmt.Println(height + aeternity.Config.Client.TTL)
return nil
}

var networkIDCmd = &cobra.Command{
Use: "networkid",
Short: "Get the node's network_id",
Long: ``,
Args: cobra.ExactArgs(0),
Run: networkIDFunc,
RunE: networkIDFunc,
}

func networkIDFunc(cmd *cobra.Command, args []string) {
func networkIDFunc(cmd *cobra.Command, args []string) (err error) {
ae := NewAeCli()
resp, err := ae.APIGetStatus()
if err != nil {
fmt.Printf("Error getting status information from the node: %v", err)
return
errFinal := fmt.Errorf("Error getting status information from the node: %v", err)
return errFinal
}
fmt.Println(*resp.NetworkID)
return nil
}

func init() {
Expand Down
28 changes: 20 additions & 8 deletions cmd/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,38 @@ func setCLIConfig() {
}
}

func TestTop(t *testing.T) {
func TestChainTop(t *testing.T) {
setCLIConfig()
emptyCmd := cobra.Command{}
topFunc(&emptyCmd, []string{})
err := topFunc(&emptyCmd, []string{})
if err != nil {
t.Error(err)
}
}

func TestStatus(t *testing.T) {
func TestChainStatus(t *testing.T) {
setCLIConfig()
emptyCmd := cobra.Command{}
statusFunc(&emptyCmd, []string{})
err := statusFunc(&emptyCmd, []string{})
if err != nil {
t.Error(err)
}
}

func TestTtl(t *testing.T) {
func TestChainTtl(t *testing.T) {
setCLIConfig()
emptyCmd := cobra.Command{}
ttlFunc(&emptyCmd, []string{})
err := ttlFunc(&emptyCmd, []string{})
if err != nil {
t.Error(err)
}
}

func TestNetworkID(t *testing.T) {
func TestChainNetworkID(t *testing.T) {
setCLIConfig()
emptyCmd := cobra.Command{}
networkIDFunc(&emptyCmd, []string{})
err := networkIDFunc(&emptyCmd, []string{})
if err != nil {
t.Error(err)
}
}

0 comments on commit 5093af2

Please sign in to comment.