summaryrefslogtreecommitdiff
path: root/src/module.c
diff options
context:
space:
mode:
authorJason Elbaum <Jason.elbaum@redis.com>2023-03-30 19:03:56 +0300
committerGitHub <noreply@github.com>2023-03-30 19:03:56 +0300
commit1f76bb17ddcb2adc484bf82f1b839c45e264524f (patch)
treeec1f4d09166d1c2bd783c3668f2a4913aa8bcf5e /src/module.c
parent971b177fa338fe06cb67a930c6e54467d29ec44f (diff)
downloadredis-1f76bb17ddcb2adc484bf82f1b839c45e264524f.tar.gz
Reimplement cli hints based on command arg docs (#10515)
Now that the command argument specs are available at runtime (#9656), this PR addresses #8084 by implementing a complete solution for command-line hinting in `redis-cli`. It correctly handles nearly every case in Redis's complex command argument definitions, including `BLOCK` and `ONEOF` arguments, reordering of optional arguments, and repeated arguments (even when followed by mandatory arguments). It also validates numerically-typed arguments. It may not correctly handle all possible combinations of those, but overall it is quite robust. Arguments are only matched after the space bar is typed, so partial word matching is not supported - that proved to be more confusing than helpful. When the user's current input cannot be matched against the argument specs, hinting is disabled. Partial support has been implemented for legacy (pre-7.0) servers that do not support `COMMAND DOCS`, by falling back to a statically-compiled command argument table. On startup, if the server does not support `COMMAND DOCS`, `redis-cli` will now issue an `INFO SERVER` command to retrieve the server version (unless `HELLO` has already been sent, in which case the server version will be extracted from the reply to `HELLO`). The server version will be used to filter the commands and arguments in the command table, removing those not supported by that version of the server. However, the static table only includes core Redis commands, so with a legacy server hinting will not be supported for module commands. The auto generated help.h and the scripts that generates it are gone. Command and argument tables for the server and CLI use different structs, due primarily to the need to support different runtime data. In order to generate code for both, macros have been added to `commands.def` (previously `commands.c`) to make it possible to configure the code generation differently for different use cases (one linked with redis-server, and one with redis-cli). Also adding a basic testing framework for the command hints based on new (undocumented) command line options to `redis-cli`: `--test_hint 'INPUT'` prints out the command-line hint for a given input string, and `--test_hint_file <filename>` runs a suite of test cases for the hinting mechanism. The test suite is in `tests/assets/test_cli_hint_suite.txt`, and it is run from `tests/integration/redis-cli.tcl`. Co-authored-by: Oran Agra <oran@redislabs.com> Co-authored-by: Viktor Söderqvist <viktor.soderqvist@est.tech>
Diffstat (limited to 'src/module.c')
-rw-r--r--src/module.c33
1 files changed, 21 insertions, 12 deletions
diff --git a/src/module.c b/src/module.c
index 5f65a2035..066786976 100644
--- a/src/module.c
+++ b/src/module.c
@@ -1295,10 +1295,9 @@ RedisModuleCommand *moduleCreateCommandProxy(struct RedisModule *module, sds dec
cp->rediscmd->proc = RedisModuleCommandDispatcher;
cp->rediscmd->flags = flags | CMD_MODULE;
cp->rediscmd->module_cmd = cp;
- cp->rediscmd->key_specs_max = STATIC_KEY_SPECS_NUM;
- cp->rediscmd->key_specs = cp->rediscmd->key_specs_static;
if (firstkey != 0) {
cp->rediscmd->key_specs_num = 1;
+ cp->rediscmd->key_specs = zcalloc(sizeof(keySpec));
cp->rediscmd->key_specs[0].flags = CMD_KEY_FULL_ACCESS;
if (flags & CMD_MODULE_GETKEYS)
cp->rediscmd->key_specs[0].flags |= CMD_KEY_VARIABLE_FLAGS;
@@ -1310,6 +1309,7 @@ RedisModuleCommand *moduleCreateCommandProxy(struct RedisModule *module, sds dec
cp->rediscmd->key_specs[0].fk.range.limit = 0;
} else {
cp->rediscmd->key_specs_num = 0;
+ cp->rediscmd->key_specs = NULL;
}
populateCommandLegacyRangeSpec(cp->rediscmd);
cp->rediscmd->microseconds = 0;
@@ -1425,6 +1425,21 @@ moduleCmdArgAt(const RedisModuleCommandInfoVersion *version,
return (RedisModuleCommandArg *)((char *)(args) + offset);
}
+/* Recursively populate the args structure (setting num_args to the number of
+ * subargs) and return the number of args. */
+int populateArgsStructure(struct redisCommandArg *args) {
+ if (!args)
+ return 0;
+ int count = 0;
+ while (args->name) {
+ serverAssert(count < INT_MAX);
+ args->num_args = populateArgsStructure(args->subargs);
+ count++;
+ args++;
+ }
+ return count;
+}
+
/* Helper for categoryFlagsFromString(). Attempts to find an acl flag representing the provided flag string
* and adds that flag to acl_categories_flags if a match is found.
*
@@ -1797,7 +1812,7 @@ int RM_SetCommandInfo(RedisModuleCommand *command, const RedisModuleCommandInfo
cmd->tips || cmd->args ||
!(cmd->key_specs_num == 0 ||
/* Allow key spec populated from legacy (first,last,step) to exist. */
- (cmd->key_specs_num == 1 && cmd->key_specs == cmd->key_specs_static &&
+ (cmd->key_specs_num == 1 &&
cmd->key_specs[0].begin_search_type == KSPEC_BS_INDEX &&
cmd->key_specs[0].find_keys_type == KSPEC_FK_RANGE))) {
errno = EEXIST;
@@ -1848,13 +1863,8 @@ int RM_SetCommandInfo(RedisModuleCommand *command, const RedisModuleCommandInfo
while (moduleCmdKeySpecAt(version, info->key_specs, count)->begin_search_type)
count++;
serverAssert(count < INT_MAX);
- if (count <= STATIC_KEY_SPECS_NUM) {
- cmd->key_specs_max = STATIC_KEY_SPECS_NUM;
- cmd->key_specs = cmd->key_specs_static;
- } else {
- cmd->key_specs_max = count;
- cmd->key_specs = zmalloc(sizeof(keySpec) * count);
- }
+ zfree(cmd->key_specs);
+ cmd->key_specs = zmalloc(sizeof(keySpec) * count);
/* Copy the contents of the RedisModuleCommandKeySpec array. */
cmd->key_specs_num = count;
@@ -11926,8 +11936,7 @@ int moduleFreeCommand(struct RedisModule *module, struct redisCommand *cmd) {
if (cmd->key_specs[j].begin_search_type == KSPEC_BS_KEYWORD)
zfree((char *)cmd->key_specs[j].bs.keyword.keyword);
}
- if (cmd->key_specs != cmd->key_specs_static)
- zfree(cmd->key_specs);
+ zfree(cmd->key_specs);
for (int j = 0; cmd->tips && cmd->tips[j]; j++)
zfree((char *)cmd->tips[j]);
zfree(cmd->tips);