提交 1f4074e9 编写于 作者: C Charles Hooper 提交者: antirez

override histfile from env - fixes #831 and copies #833

上级 9e83d2d2
...@@ -61,6 +61,8 @@ ...@@ -61,6 +61,8 @@
#define OUTPUT_CSV 2 #define OUTPUT_CSV 2
#define REDIS_CLI_KEEPALIVE_INTERVAL 15 /* seconds */ #define REDIS_CLI_KEEPALIVE_INTERVAL 15 /* seconds */
#define REDIS_CLI_DEFAULT_PIPE_TIMEOUT 30 /* seconds */ #define REDIS_CLI_DEFAULT_PIPE_TIMEOUT 30 /* seconds */
#define REDIS_CLI_HISTFILE_ENV "REDISCLI_HISTFILE"
#define REDIS_CLI_HISTFILE_DEFAULT ".rediscli_history"
static redisContext *context; static redisContext *context;
static struct config { static struct config {
...@@ -142,6 +144,30 @@ static void cliRefreshPrompt(void) { ...@@ -142,6 +144,30 @@ static void cliRefreshPrompt(void) {
snprintf(config.prompt+len,sizeof(config.prompt)-len,"> "); snprintf(config.prompt+len,sizeof(config.prompt)-len,"> ");
} }
static sds getHistoryPath() {
char *path = NULL;
sds historyPath = NULL;
/* check the env for a histfile override */
path = getenv(REDIS_CLI_HISTFILE_ENV);
if (path != NULL && *path != '\0') {
if (!strcmp("/dev/null", path)) {
return NULL;
}
/* if the env is set, return it */
historyPath = sdscatprintf(sdsempty(), "%s", path);
} else {
char *home = getenv("HOME");
if (home != NULL && *home != '\0') {
/* otherwise, return the default */
historyPath = sdscatprintf(sdsempty(), "%s/%s", home, REDIS_CLI_HISTFILE_DEFAULT);
}
}
return historyPath;
}
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* Help functions * Help functions
*--------------------------------------------------------------------------- */ *--------------------------------------------------------------------------- */
...@@ -906,10 +932,9 @@ static void repl(void) { ...@@ -906,10 +932,9 @@ static void repl(void) {
/* Only use history when stdin is a tty. */ /* Only use history when stdin is a tty. */
if (isatty(fileno(stdin))) { if (isatty(fileno(stdin))) {
history = 1; historyfile = getHistoryPath();
if (historyfile != NULL) {
if (getenv("HOME") != NULL) { history = 1;
historyfile = sdscatprintf(sdsempty(),"%s/.rediscli_history",getenv("HOME"));
linenoiseHistoryLoad(historyfile); linenoiseHistoryLoad(historyfile);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册