github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/crypto/keys/client/query.go (about) 1 package client 2 3 import ( 4 "context" 5 "flag" 6 7 "github.com/gnolang/gno/tm2/pkg/bft/rpc/client" 8 ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types" 9 "github.com/gnolang/gno/tm2/pkg/commands" 10 "github.com/gnolang/gno/tm2/pkg/errors" 11 ) 12 13 type QueryCfg struct { 14 RootCfg *BaseCfg 15 16 Data string 17 Height int64 18 Prove bool 19 20 Path string 21 } 22 23 func NewQueryCmd(rootCfg *BaseCfg, io commands.IO) *commands.Command { 24 cfg := &QueryCfg{ 25 RootCfg: rootCfg, 26 } 27 28 return commands.NewCommand( 29 commands.Metadata{ 30 Name: "query", 31 ShortUsage: "query [flags] <path>", 32 ShortHelp: "makes an ABCI query", 33 }, 34 cfg, 35 func(_ context.Context, args []string) error { 36 return execQuery(cfg, args, io) 37 }, 38 ) 39 } 40 41 func (c *QueryCfg) RegisterFlags(fs *flag.FlagSet) { 42 fs.StringVar( 43 &c.Data, 44 "data", 45 "", 46 "query data bytes", 47 ) 48 49 fs.Int64Var( 50 &c.Height, 51 "height", 52 0, 53 "query height (not yet supported)", 54 ) 55 56 fs.BoolVar( 57 &c.Prove, 58 "prove", 59 false, 60 "prove query result (not yet supported)", 61 ) 62 } 63 64 func execQuery(cfg *QueryCfg, args []string, io commands.IO) error { 65 if len(args) != 1 { 66 return flag.ErrHelp 67 } 68 69 cfg.Path = args[0] 70 71 qres, err := QueryHandler(cfg) 72 if err != nil { 73 return err 74 } 75 76 if qres.Response.Error != nil { 77 io.Printf("Log: %s\n", 78 qres.Response.Log) 79 return qres.Response.Error 80 } 81 82 resdata := qres.Response.Data 83 // XXX in general, how do we know what to show? 84 // proof := qres.Response.Proof 85 height := qres.Response.Height 86 io.Printf("height: %d\ndata: %s\n", 87 height, 88 string(resdata)) 89 return nil 90 } 91 92 func QueryHandler(cfg *QueryCfg) (*ctypes.ResultABCIQuery, error) { 93 remote := cfg.RootCfg.Remote 94 if remote == "" { 95 return nil, errors.New("missing remote url") 96 } 97 98 data := []byte(cfg.Data) 99 opts2 := client.ABCIQueryOptions{ 100 // Height: height, XXX 101 // Prove: false, XXX 102 } 103 cli, err := client.NewHTTPClient(remote) 104 if err != nil { 105 return nil, errors.Wrap(err, "new http client") 106 } 107 108 qres, err := cli.ABCIQueryWithOptions( 109 cfg.Path, data, opts2) 110 if err != nil { 111 return nil, errors.Wrap(err, "querying") 112 } 113 114 return qres, nil 115 }