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