fix tx search CLI

This commit is contained in:
Federico Kunze 2018-11-27 14:37:03 +01:00
parent 9fdd4300f2
commit a2189bc672
2 changed files with 16 additions and 17 deletions

View File

@ -20,7 +20,7 @@ import (
) )
const ( const (
flagTags = "tag" flagTags = "tags"
flagAny = "any" flagAny = "any"
) )
@ -31,38 +31,37 @@ func SearchTxCmd(cdc *codec.Codec) *cobra.Command {
Short: "Search for all transactions that match the given tags.", Short: "Search for all transactions that match the given tags.",
Long: strings.TrimSpace(` Long: strings.TrimSpace(`
Search for transactions that match the given tags. By default, transactions must match ALL tags Search for transactions that match the given tags. By default, transactions must match ALL tags
passed to the --tags option. To match any transaction, use the --any option. passed to the --tag option. To match any transaction, use the --any option.
For example: For example:
$ gaiacli query txs --tag test1,test2 $ gaiacli query txs --tags <key1>:<value1>&<key2>:<value2>
will match any transaction tagged with both test1,test2. To match a transaction tagged with either will match any transaction tagged with both <key1>=<value1> and <key2>=<value2>.
test1 or test2, use: To match a transaction tagged with either value1 or value2, use:
$ gaiacli query txs --tag <key1>=<value1>&<key2>=<value2> --any $ gaiacli query txs --tags <key1>:<value1>&<key2>:<value2> --any
`), `),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
tagsStr := viper.GetString(flagTags) tagsStr := viper.GetString(flagTags)
tagsStr = strings.Trim(tagsStr, "'") tagsStr = strings.Trim(tagsStr, "'")
var tags []string var tags []string
switch strings.Contains(tagsStr, "&") { switch strings.Contains(tagsStr, "&") {
case true: case true:
tags = strings.Split(tagsStr, "&") tags = strings.Split(tagsStr, "&")
case false: case false:
tags = []string{tagsStr} tags = append(tags, tagsStr)
} }
var tmTags []string var tmTags []string
for _, tag := range tags { for _, tag := range tags {
if !strings.Contains(tag, "=") { if !strings.Contains(tag, ":") {
return fmt.Errorf("%s should be of the format <key>=<value>", tagsStr) return fmt.Errorf("%s should be of the format <key>:<value>", tagsStr)
} }
keyValue := strings.Split(tag, "=") keyValue := strings.Split(tag, ":")
key, value, errMsg := getKeyFromBechPrefix(keyValue[0], keyValue[1]) key, value, errMsg := getKeyFromBechPrefix(keyValue[0], keyValue[1])
if errMsg == "" { if errMsg != "" {
return fmt.Errorf(errMsg) return errors.New(errMsg)
} }
tag = fmt.Sprintf("%s='%s'", key, value) tag = fmt.Sprintf("%s='%s'", key, value)
@ -98,7 +97,7 @@ $ gaiacli query txs --tag <key1>=<value1>&<key2>=<value2> --any
viper.BindPFlag(client.FlagChainID, cmd.Flags().Lookup(client.FlagChainID)) viper.BindPFlag(client.FlagChainID, cmd.Flags().Lookup(client.FlagChainID))
cmd.Flags().Bool(client.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)") cmd.Flags().Bool(client.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
viper.BindPFlag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode)) viper.BindPFlag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode))
cmd.Flags().StringSlice(flagTags, nil, "Comma-separated list of tags that must match") cmd.Flags().String(flagTags, "", "key:value list of tags that must match")
cmd.Flags().Bool(flagAny, false, "Return transactions that match ANY tag, rather than ALL") cmd.Flags().Bool(flagAny, false, "Return transactions that match ANY tag, rather than ALL")
return cmd return cmd
} }
@ -205,11 +204,11 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.
} }
key, value, errMsg := getKeyFromBechPrefix(key, value) key, value, errMsg := getKeyFromBechPrefix(key, value)
if errMsg == "" { if errMsg != "" {
utils.WriteErrorResponse(w, http.StatusBadRequest, errMsg) utils.WriteErrorResponse(w, http.StatusBadRequest, errMsg)
return return
} }
fmt.Println(fmt.Sprintf("%s='%s'", key, value))
tag := fmt.Sprintf("%s='%s'", key, value) tag := fmt.Sprintf("%s='%s'", key, value)
tags = append(tags, tag) tags = append(tags, tag)
} }

View File

@ -383,7 +383,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
executeWrite(t, spStr, app.DefaultKeyPass) executeWrite(t, spStr, app.DefaultKeyPass)
tests.WaitForNextNBlocksTM(2, port) tests.WaitForNextNBlocksTM(2, port)
txs := executeGetTxs(t, fmt.Sprintf("gaiacli query txs --tag='action=submit-proposal&proposer=%s' %v", fooAddr, flags)) txs := executeGetTxs(t, fmt.Sprintf("gaiacli query txs --tags='action:submit-proposal&proposer:%s' %v", fooAddr, flags))
require.Len(t, txs, 1) require.Len(t, txs, 1)
fooAcc = executeGetAccount(t, fmt.Sprintf("gaiacli query account %s %v", fooAddr, flags)) fooAcc = executeGetAccount(t, fmt.Sprintf("gaiacli query account %s %v", fooAddr, flags))