| 33 | ) |
| 34 | |
| 35 | func main() { |
| 36 | app := cli.NewApp() |
| 37 | app.Usage = "executable command tool for easy test" |
| 38 | app.Flags = []cli.Flag{ |
| 39 | cli.StringFlag{ |
| 40 | Name: ENDPOINT + ",ep", |
| 41 | Value: "http://127.0.0.1:8521", |
| 42 | Usage: "endpoint like http://127.0.0.1:8521", |
| 43 | }, |
| 44 | } |
| 45 | |
| 46 | app.Commands = []cli.Command{ |
| 47 | { |
| 48 | Name: "get_block_number", |
| 49 | Aliases: []string{"bn"}, |
| 50 | Usage: "get block number", |
| 51 | Action: func(c *cli.Context) error { |
| 52 | endpoint := c.GlobalString(ENDPOINT) |
| 53 | client, err := cpclient.Dial(endpoint) |
| 54 | if err != nil { |
| 55 | log.Fatal(err.Error()) |
| 56 | } |
| 57 | blockNumber := client.GetBlockNumber() |
| 58 | fmt.Println(blockNumber) |
| 59 | return nil |
| 60 | }, |
| 61 | }, |
| 62 | { |
| 63 | Name: "balance", |
| 64 | Aliases: []string{"bal"}, |
| 65 | Usage: "get balance by address. \n\t\texample: ./testtool bal ${addr}", |
| 66 | Action: func(c *cli.Context) error { |
| 67 | endpoint := c.GlobalString(ENDPOINT) |
| 68 | client, err := cpclient.Dial(endpoint) |
| 69 | if err != nil { |
| 70 | log.Fatal(err.Error()) |
| 71 | } |
| 72 | balance, _ := client.BalanceAt(context.Background(), common.HexToAddress(c.Args().First()), nil) |
| 73 | fmt.Println(balance) |
| 74 | return nil |
| 75 | }, |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | sort.Sort(cli.FlagsByName(app.Flags)) |
| 80 | sort.Sort(cli.CommandsByName(app.Commands)) |
| 81 | |
| 82 | err := app.Run(os.Args) |
| 83 | if err != nil { |
| 84 | log.Fatal(err) |
| 85 | } |
| 86 | } |