Skip to content

Commit

Permalink
Fix history file usage in SQL CLI tool. (opensearch-project#1077)
Browse files Browse the repository at this point in the history
* Fix saving history into a file. Fixed config reading.

Signed-off-by: Yury-Fridlyand <[email protected]>

* Update config comments to match code.

Signed-off-by: Yury-Fridlyand <[email protected]>

* Code cleanup.

Signed-off-by: Yury-Fridlyand <[email protected]>

Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand authored Nov 23, 2022
1 parent 42143b9 commit 334cd46
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions sql-cli/src/opensearch_sql_cli/conf/clirc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions sql-cli/src/opensearch_sql_cli/opensearchsql_cli.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down

0 comments on commit 334cd46

Please sign in to comment.