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 3 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
21 changes: 18 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,21 @@ 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.log_file = config["main"]["log_file"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this being used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

History file used for history, log file - for logging errors. For example, if config has incorrect path to the history file.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it - do you need to pass this in anywhere like you do the history file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No actually. Good point, thank you. self.log_file removed in f79e5a9.

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))

if self.log_file == "default":
self.log_file = os.path.join(config_location(), "log")
else:
self.log_file = expandvars(expanduser(self.log_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 +90,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