/
cli.go
46 lines (40 loc) · 1.15 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package utils
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/spf13/cobra"
)
func CLIQueryJSONPrint(cdc *codec.Codec, uri string, data []byte, rstTypeFactory func() interface{}) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(uri, data)
if err != nil {
fmt.Printf("Failed to Query and Print: %s, because %s", uri, err)
return nil
}
rst := rstTypeFactory()
cdc.MustUnmarshalJSON(res, rst)
out, err := cdc.MarshalJSONIndent(rst, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
func SimpleQueryCmd(use, short, route, store string, nargs int, rstPointer interface{}) func(*codec.Codec) *cobra.Command {
return func(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: use,
Short: short,
Args: cobra.ExactArgs(nargs),
RunE: func(cmd *cobra.Command, args []string) error {
uri := fmt.Sprintf("custom/%s/%s", route, store)
for i := 0; i < nargs; i++ {
uri += "/" + args[i]
}
return CLIQueryJSONPrint(cdc, uri, nil,
func() interface{} { return rstPointer })
},
}
}
}