Skip to content

Commit

Permalink
[cmd] Provide cookbook command, change binary name
Browse files Browse the repository at this point in the history
  • Loading branch information
fxfactorial committed Sep 13, 2019
1 parent fc0f844 commit c805656
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 77 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ built_by := ${USER}@harmony.one
flags := -gcflags="all=-N -l -c 2"
ldflags := -X main.version=v${version} -X main.commit=${commit}
ldflags += -X main.builtAt=${built_at} -X main.builtBy=${built_by}
cli := ./dist/hmy_cli
cli := ./dist/hmy

env := GO111MODULE=on

Expand Down
59 changes: 9 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,23 @@ simple invocation of `make`

# Build

Invoke `make` to build the `hmy_cli` binary.
Invoke `make` to build the `hmy` binary.

# Usage & Examples

hmy_cli implements a fluent API

```bash
./hmy_cli blockchain protocol-version
{"jsonrpc":"2.0","id":"0","result":"0x1"}

`hmy` implements a fluent API, that is, there is a hierarchy of commands.

invoke the following command to see the most command usages of `hmy`

./hmy_cli blockchain transaction-by-hash 0x9175e8a3a16afccf6c2d197ed97531de15fcea5b595a4c9ddf4d4d4a22beaab8 --node="http://localhost:9501" --pretty
{
"id": "0",
"jsonrpc": "2.0",
"result": {
"blockHash": "0x787e5d6570eb7091110239e623a75901fb5d426f2a7ac42b60f83b7b455e200e",
"blockNumber": "0x100",
"from": "0xebcd16e8c1d8f493ba04e99a56474122d81a9c58",
"gas": "0x5208",
"gasPrice": "0x0",
"hash": "0x9175e8a3a16afccf6c2d197ed97531de15fcea5b595a4c9ddf4d4d4a22beaab8",
"input": "0x",
"nonce": "0x2",
"r": "0x5bf54b3ae151240d13cfd52acabd78f4161a7f71e5fee203c6ef98719f9ca327",
"s": "0x129e31ea901f9471597c0c722183ccb0cd7ac628b2831e28068990b087032760",
"to": "0x514650ca30b3c79f693e14220115434236d44aeb",
"transactionIndex": "0x0",
"v": "0x28",
"value": "0xad78ebc5ac620000"
}
}

./hmy_cli blockchain transaction-by-receipt 0x8a67a436eadf827faf17bfff18570c1c6c5c63f993d90511364f3adc9dd96c14 --node="http://localhost:9501" --pretty
{
"id": "0",
"jsonrpc": "2.0",
"result": {
"blockHash": "0xed3b0bb70ab26e5c7db64ad0ccec6c13eef30e2739c5e48b6d676ff33f91e1ac",
"blockNumber": "0x4c0",
"contractAddress": null,
"cumulativeGasUsed": "0x5208",
"from": "0x514650ca30b3c79f693e14220115434236d44aeb",
"gasUsed": "0x5208",
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"shardID": 1,
"status": "0x1",
"to": "0x6a87346f3ba9958d08d09484a2b7fdbbe42b0df6",
"transactionHash": "0x8a67a436eadf827faf17bfff18570c1c6c5c63f993d90511364f3adc9dd96c14",
"transactionIndex": "0x0"
}
}
```
$ hmy cookbook
```

# Debugging

The go-sdk code respects `HMY_RPC_DEBUG HMY_TX_DEBUG` as debugging
based environment variables.

```bash
HMY_RPC_DEBUG=true ./hmy_cli blockchain protocol-version
HMY_RPC_DEBUG=true HMY_TX_DEBUG=true ./hmy blockchain protocol-version
```
10 changes: 5 additions & 5 deletions cmd/subcommands/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra"
)

func checkAllShards(node, addr string, pretty bool) string {
func checkAllShards(node, addr string, noPretty bool) string {
var out bytes.Buffer
out.WriteString("[")
params := []interface{}{addr, "latest"}
Expand All @@ -29,10 +29,10 @@ func checkAllShards(node, addr string, pretty bool) string {
}
}
out.WriteString("]")
if pretty {
return common.JSONPrettyFormat(out.String())
if noPretty {
return out.String()
}
return out.String()
return common.JSONPrettyFormat(out.String())
}

func init() {
Expand All @@ -42,7 +42,7 @@ func init() {
Long: `Query for the latest account balance given a Harmony Address`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(checkAllShards(node, args[0], prettyPrintJSONOutput))
fmt.Println(checkAllShards(node, args[0], noPrettyOutput))
},
}

Expand Down
45 changes: 24 additions & 21 deletions cmd/subcommands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import (
)

var (
useLatestInParamsForRPC bool
prettyPrintJSONOutput bool
useOneAddressInsteadOfHex bool
node string
keyStoreDir string
request = func(method rpc.RPCMethod, params []interface{}) {
useLatestInParamsForRPC bool
noPrettyOutput bool
node string
keyStoreDir string
request = func(method rpc.RPCMethod, params []interface{}) {
if useLatestInParamsForRPC {
params = append(params, "latest")
}
Expand All @@ -29,48 +28,52 @@ var (
os.Exit(-1)
}
asJSON, _ := json.Marshal(success)
if prettyPrintJSONOutput {
fmt.Print(common.JSONPrettyFormat(string(asJSON)))
if noPrettyOutput {
fmt.Print(string(asJSON))
return
}
fmt.Print(string(asJSON))
fmt.Print(common.JSONPrettyFormat(string(asJSON)))
}
RootCmd = &cobra.Command{
Use: "hmy_cli",
Use: "hmy",
Short: "Harmony blockchain",
SilenceUsage: true,
Long: `
CLI interface to the Harmony blockchain
`,
See "hmy cookbook" for examples of the most common, important usages`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
)

const (
HMY_CLI_DOCS_DIR = "hmy_cli-docs"
DEFAULT_NODE_ADDR = "http://localhost:9500"
)

func init() {
RootCmd.PersistentFlags().StringVarP(
&node,
"node",
"n",
DEFAULT_NODE_ADDR,
defaultNodeAddr,
"<host>:<port>",
)
RootCmd.PersistentFlags().BoolVarP(&useLatestInParamsForRPC, "latest", "l", false, "Add 'latest' to RPC params")
RootCmd.PersistentFlags().BoolVarP(&prettyPrintJSONOutput, "pretty", "p", false, "pretty print JSON outputs")
RootCmd.PersistentFlags().BoolVarP(&useOneAddressInsteadOfHex, "one-address", "o", false, "use one address for RPC calls")
RootCmd.PersistentFlags().BoolVar(&noPrettyOutput, "no-pretty", false, "disable pretty print JSON outputs")
RootCmd.PersistentFlags().StringVar(&keyStoreDir, "key-store-dir", "k", "What directory to use as the keystore")

RootCmd.AddCommand(&cobra.Command{
Use: "cookbook",
Short: "Example usages of the most important, frequently used commands",
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(cookbookDoc)
},
})

RootCmd.AddCommand(&cobra.Command{
Use: "docs",
Short: fmt.Sprintf("Generate docs to a local %s directory", HMY_CLI_DOCS_DIR),
Short: fmt.Sprintf("Generate docs to a local %s directory", defaultNodeAddr),
Run: func(cmd *cobra.Command, args []string) {
cwd, _ := os.Getwd()
docDir := path.Join(cwd, HMY_CLI_DOCS_DIR)
docDir := path.Join(cwd, defaultNodeAddr)
os.Mkdir(docDir, 0700)
doc.GenMarkdownTree(RootCmd, docDir)
},
Expand Down
32 changes: 32 additions & 0 deletions cmd/subcommands/values.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"fmt"

color "github.com/fatih/color"
)

const (
hmyDocsDir = "hmycli-docs"
defaultNodeAddr = "http://localhost:9500"
)

var (
g = color.New(color.FgGreen).SprintFunc()
cookbookDoc = fmt.Sprintf(`
Cookbook of usage, note that every subcommand recognizes a '--help' flag
%s
hmy --node="https://api.s1.b.hmny.io/" --pretty balance <SOME_ONE_ADDRESS>
%s
hmy --node="https://api.s1.b.hmny.io" --pretty blockchain transaction-by-hash <SOME_TRANSACTION_HASH>
%s
hmy keys list
`,
g("1. Check Balances"),
g("2. Check local keys"),
g("3. List local keys"),
)
)

0 comments on commit c805656

Please sign in to comment.