Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix history file usage in SQL CLI tool. #1077

Merged
merged 4 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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