Commit a45f9a1a authored by antirez's avatar antirez
Browse files

redis-cli no longer aborts in repl-mode on error, and retries to reconncet...

redis-cli no longer aborts in repl-mode on error, and retries to reconncet with the server at every command issued if the state is not connected. Also the prompt shows the server we are connected to.
parent efc34087
...@@ -87,9 +87,11 @@ static long long mstime(void) { ...@@ -87,9 +87,11 @@ static long long mstime(void) {
static void cliRefreshPrompt(void) { static void cliRefreshPrompt(void) {
if (config.dbnum == 0) if (config.dbnum == 0)
snprintf(config.prompt,sizeof(config.prompt),"redis> "); snprintf(config.prompt,sizeof(config.prompt),"redis %s:%d> ",
config.hostip, config.hostport);
else else
snprintf(config.prompt,sizeof(config.prompt),"redis:%d> ",config.dbnum); snprintf(config.prompt,sizeof(config.prompt),"redis %s:%d[%d]> ",
config.hostip, config.hostport, config.dbnum);
} }
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
...@@ -314,10 +316,9 @@ static int cliConnect(int force) { ...@@ -314,10 +316,9 @@ static int cliConnect(int force) {
return REDIS_OK; return REDIS_OK;
} }
static void cliPrintContextErrorAndExit() { static void cliPrintContextError() {
if (context == NULL) return; if (context == NULL) return;
fprintf(stderr,"Error: %s\n",context->errstr); fprintf(stderr,"Error: %s\n",context->errstr);
exit(1);
} }
static sds cliFormatReplyTTY(redisReply *r, char *prefix) { static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
...@@ -436,7 +437,8 @@ static int cliReadReply(int output_raw_strings) { ...@@ -436,7 +437,8 @@ static int cliReadReply(int output_raw_strings) {
if (context->err == REDIS_ERR_EOF) if (context->err == REDIS_ERR_EOF)
return REDIS_ERR; return REDIS_ERR;
} }
cliPrintContextErrorAndExit(); cliPrintContextError();
exit(1);
return REDIS_ERR; /* avoid compiler warning */ return REDIS_ERR; /* avoid compiler warning */
} }
...@@ -462,10 +464,7 @@ static int cliSendCommand(int argc, char **argv, int repeat) { ...@@ -462,10 +464,7 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
size_t *argvlen; size_t *argvlen;
int j, output_raw; int j, output_raw;
if (context == NULL) { if (context == NULL) return REDIS_ERR;
printf("Not connected, please use: connect <host> <port>\n");
return REDIS_OK;
}
output_raw = 0; output_raw = 0;
if (!strcasecmp(command,"info") || if (!strcasecmp(command,"info") ||
...@@ -688,7 +687,7 @@ static void repl() { ...@@ -688,7 +687,7 @@ static void repl() {
/* If we still cannot send the command, /* If we still cannot send the command,
* print error and abort. */ * print error and abort. */
if (cliSendCommand(argc,argv,1) != REDIS_OK) if (cliSendCommand(argc,argv,1) != REDIS_OK)
cliPrintContextErrorAndExit(); cliPrintContextError();
} }
elapsed = mstime()-start_time; elapsed = mstime()-start_time;
if (elapsed >= 500) { if (elapsed >= 500) {
...@@ -741,11 +740,15 @@ int main(int argc, char **argv) { ...@@ -741,11 +740,15 @@ int main(int argc, char **argv) {
argc -= firstarg; argc -= firstarg;
argv += firstarg; argv += firstarg;
/* Try to connect */
if (cliConnect(0) != REDIS_OK) exit(1);
/* Start interactive mode when no command is provided */ /* Start interactive mode when no command is provided */
if (argc == 0) repl(); if (argc == 0) {
/* Note that in repl mode we don't abort on connection error.
* A new attempt will be performed for every command send. */
cliConnect(0);
repl();
}
/* Otherwise, we have some arguments to execute */ /* Otherwise, we have some arguments to execute */
if (cliConnect(0) != REDIS_OK) exit(1);
return noninteractive(argc,convertToSds(argc,argv)); return noninteractive(argc,convertToSds(argc,argv));
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment