Skip to content

Commit 334cd46

Browse files
Fix history file usage in SQL CLI tool. (opensearch-project#1077)
* Fix saving history into a file. Fixed config reading. Signed-off-by: Yury-Fridlyand <yuryf@bitquilltech.com> * Update config comments to match code. Signed-off-by: Yury-Fridlyand <yuryf@bitquilltech.com> * Code cleanup. Signed-off-by: Yury-Fridlyand <yuryf@bitquilltech.com> Signed-off-by: Yury-Fridlyand <yuryf@bitquilltech.com>
1 parent 42143b9 commit 334cd46

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

sql-cli/src/opensearch_sql_cli/conf/clirc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ multi_line = True
3131
multi_line_mode = opensearchsql_cli
3232

3333
# log_file location.
34-
# In Unix/Linux: ~/.conf/opensearchsql-cli/log
34+
# In Unix/Linux: ~/.config/opensearchsql-cli/log
3535
# In Windows: %USERPROFILE%\AppData\Local\dbcli\opensearchsql-cli\log
3636
# %USERPROFILE% is typically C:\Users\{username}
3737
log_file = default
3838

3939
# history_file location.
40-
# In Unix/Linux: ~/.conf/opensearchsql-cli/history
40+
# In Unix/Linux: ~/.config/opensearchsql-cli/history
4141
# In Windows: %USERPROFILE%\AppData\Local\dbcli\opensearchsql-cli\history
4242
# %USERPROFILE% is typically C:\Users\{username}
4343
history_file = default

sql-cli/src/opensearch_sql_cli/opensearchsql_cli.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from __future__ import unicode_literals
22

3+
from os.path import expanduser, expandvars
4+
5+
from prompt_toolkit.history import FileHistory
6+
37
"""
48
Copyright OpenSearch Contributors
59
SPDX-License-Identifier: Apache-2.0
@@ -21,7 +25,7 @@
2125
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
2226
from pygments.lexers.sql import SqlLexer
2327

24-
from .config import get_config
28+
from .config import get_config, config_location
2529
from .opensearch_connection import OpenSearchConnection
2630
from .opensearch_buffer import opensearch_is_multiline
2731
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
5761
self.multiline_continuation_char = config["main"]["multiline_continuation_char"]
5862
self.multi_line = config["main"].as_bool("multi_line")
5963
self.multiline_mode = config["main"].get("multi_line_mode", "src")
64+
self.history_file = config["main"]["history_file"]
6065
self.null_string = config["main"].get("null_string", "null")
6166
self.style_output = style_factory_output(self.syntax_style, self.cli_style)
6267

68+
if self.history_file == "default":
69+
self.history_file = os.path.join(config_location(), "history")
70+
else:
71+
self.history_file = expandvars(expanduser(self.history_file))
72+
6373
def build_cli(self):
6474
# TODO: Optimize index suggestion to serve indices options only at the needed position, such as 'from'
6575
indices_list = self.opensearch_executor.indices_list
@@ -74,8 +84,7 @@ def get_continuation(width, *_):
7484
lexer=PygmentsLexer(SqlLexer),
7585
completer=sql_completer,
7686
complete_while_typing=True,
77-
# TODO: add history, refer to pgcli approach
78-
# history=history,
87+
history=FileHistory(self.history_file),
7988
style=style_factory(self.syntax_style, self.cli_style),
8089
prompt_continuation=get_continuation,
8190
multiline=opensearch_is_multiline(self),

0 commit comments

Comments
 (0)