From 334cd46d3df1fea10e482645e207f096845dfca0 Mon Sep 17 00:00:00 2001 From: Yury-Fridlyand Date: Wed, 23 Nov 2022 15:07:28 -0800 Subject: [PATCH] Fix history file usage in SQL CLI tool. (#1077) * Fix saving history into a file. Fixed config reading. Signed-off-by: Yury-Fridlyand * Update config comments to match code. Signed-off-by: Yury-Fridlyand * Code cleanup. Signed-off-by: Yury-Fridlyand Signed-off-by: Yury-Fridlyand --- sql-cli/src/opensearch_sql_cli/conf/clirc | 4 ++-- .../src/opensearch_sql_cli/opensearchsql_cli.py | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/sql-cli/src/opensearch_sql_cli/conf/clirc b/sql-cli/src/opensearch_sql_cli/conf/clirc index e5b972438b..83fd0e5d3a 100644 --- a/sql-cli/src/opensearch_sql_cli/conf/clirc +++ b/sql-cli/src/opensearch_sql_cli/conf/clirc @@ -31,13 +31,13 @@ multi_line = True multi_line_mode = opensearchsql_cli # log_file location. -# In Unix/Linux: ~/.conf/opensearchsql-cli/log +# In Unix/Linux: ~/.config/opensearchsql-cli/log # In Windows: %USERPROFILE%\AppData\Local\dbcli\opensearchsql-cli\log # %USERPROFILE% is typically C:\Users\{username} log_file = default # history_file location. -# In Unix/Linux: ~/.conf/opensearchsql-cli/history +# In Unix/Linux: ~/.config/opensearchsql-cli/history # In Windows: %USERPROFILE%\AppData\Local\dbcli\opensearchsql-cli\history # %USERPROFILE% is typically C:\Users\{username} history_file = default diff --git a/sql-cli/src/opensearch_sql_cli/opensearchsql_cli.py b/sql-cli/src/opensearch_sql_cli/opensearchsql_cli.py index e490938cd8..20a3cb111d 100644 --- a/sql-cli/src/opensearch_sql_cli/opensearchsql_cli.py +++ b/sql-cli/src/opensearch_sql_cli/opensearchsql_cli.py @@ -1,5 +1,9 @@ from __future__ import unicode_literals +from os.path import expanduser, expandvars + +from prompt_toolkit.history import FileHistory + """ Copyright OpenSearch Contributors SPDX-License-Identifier: Apache-2.0 @@ -21,7 +25,7 @@ from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from pygments.lexers.sql import SqlLexer -from .config import get_config +from .config import get_config, config_location from .opensearch_connection import OpenSearchConnection from .opensearch_buffer import opensearch_is_multiline from .opensearch_style import style_factory, style_factory_output @@ -57,9 +61,15 @@ def __init__(self, clirc_file=None, always_use_pager=False, use_aws_authenticati self.multiline_continuation_char = config["main"]["multiline_continuation_char"] self.multi_line = config["main"].as_bool("multi_line") self.multiline_mode = config["main"].get("multi_line_mode", "src") + self.history_file = config["main"]["history_file"] self.null_string = config["main"].get("null_string", "null") self.style_output = style_factory_output(self.syntax_style, self.cli_style) + if self.history_file == "default": + self.history_file = os.path.join(config_location(), "history") + else: + self.history_file = expandvars(expanduser(self.history_file)) + def build_cli(self): # TODO: Optimize index suggestion to serve indices options only at the needed position, such as 'from' indices_list = self.opensearch_executor.indices_list @@ -74,8 +84,7 @@ def get_continuation(width, *_): lexer=PygmentsLexer(SqlLexer), completer=sql_completer, complete_while_typing=True, - # TODO: add history, refer to pgcli approach - # history=history, + history=FileHistory(self.history_file), style=style_factory(self.syntax_style, self.cli_style), prompt_continuation=get_continuation, multiline=opensearch_is_multiline(self),