From 19ed8b28aa673949f2498aeec2debfebe41f8874 Mon Sep 17 00:00:00 2001 From: laixintao Date: Thu, 15 Oct 2020 19:06:35 +0800 Subject: [PATCH 1/3] doc: add history_file per dsn config, and --history option. --- pgcli/main.py | 4 ++++ pgcli/pgclirc | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pgcli/main.py b/pgcli/main.py index b1468985f..e949d24d9 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -1194,6 +1194,9 @@ def echo_via_pager(self, text, color=None): @click.option( "--warn/--no-warn", default=None, help="Warn before running a destructive query." ) +@click.option( + "--history", default=None, help="Specify history file location." +) @click.argument("dbname", default=lambda: None, envvar="PGDATABASE", nargs=1) @click.argument("username", default=lambda: None, envvar="PGUSER", nargs=1) def cli( @@ -1217,6 +1220,7 @@ def cli( auto_vertical_output, list_dsn, warn, + history, ): if version: print("Version:", __version__) diff --git a/pgcli/pgclirc b/pgcli/pgclirc index e97afdab4..60b79a202 100644 --- a/pgcli/pgclirc +++ b/pgcli/pgclirc @@ -58,7 +58,8 @@ generate_casing_file = False # Casing of column headers based on the casing_file described above case_column_headers = True -# history_file location. +# default history_file location. You can also set history file location for +# specific database. see [dsn_history_file] section. # In Unix/Linux: ~/.config/pgcli/history # In Windows: %USERPROFILE%\AppData\Local\dbcli\pgcli\history # %USERPROFILE% is typically C:\Users\{username} @@ -187,6 +188,11 @@ output.null = "#808080" [alias_dsn] # example_dsn = postgresql://[user[:password]@][netloc][:port][/dbname] +# Specify sperate history file for alias_dsn. +# If not set, will use history_file settings. +[dsn_history_file] +# example_dsn = ~/.config/pgcli/example_dsn.history + # Format for number representation # for decimal "d" - 12345678, ",d" - 12,345,678 # for float "g" - 123456.78, ",g" - 123,456.78 From 031325a0ac2d3a83c517f70465e9aeeb9ee860ee Mon Sep 17 00:00:00 2001 From: laixintao Date: Sun, 25 Oct 2020 14:45:57 +0800 Subject: [PATCH 2/3] [wip] featu: supprt --history option. I added two args for PGCli init, because I think we need to figure out which history file to use on `__init__`. And we will need `histfile` `alias_dsn` for that. PS: I don't think change PGCli's property after initializing is good. --- pgcli/main.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/pgcli/main.py b/pgcli/main.py index e949d24d9..ac57e389b 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -164,6 +164,8 @@ def __init__( prompt_dsn=None, auto_vertical_output=False, warn=None, + histfile=None, + alias_dsn=None ): self.force_passwd_prompt = force_passwd_prompt @@ -227,6 +229,16 @@ def __init__( self.completion_refresher = CompletionRefresher() + # history file location: --hisfile > pgclirc:history + if histfile: + self.history_file = histfile + else: + self.history_file = self.config["main"]["history_file"] + if self.history_file == "default": + self.history_file = config_location() + "history" + + if alias_dsn: + self.dsn_alias = alias_dsn self.query_history = [] # Initialize completer @@ -718,10 +730,7 @@ def execute_command(self, text): def run_cli(self): logger = self.logger - history_file = self.config["main"]["history_file"] - if history_file == "default": - history_file = config_location() + "history" - history = FileHistory(os.path.expanduser(history_file)) + history = FileHistory(os.path.expanduser(self.history_file)) self.refresh_completions(history=history, persist_priorities="none") self.prompt_app = self._build_cli(history) @@ -1195,7 +1204,7 @@ def echo_via_pager(self, text, color=None): "--warn/--no-warn", default=None, help="Warn before running a destructive query." ) @click.option( - "--history", default=None, help="Specify history file location." + "--histfile", default=None, help="Specify history file location." ) @click.argument("dbname", default=lambda: None, envvar="PGDATABASE", nargs=1) @click.argument("username", default=lambda: None, envvar="PGUSER", nargs=1) @@ -1220,7 +1229,7 @@ def cli( auto_vertical_output, list_dsn, warn, - history, + histfile, ): if version: print("Version:", __version__) @@ -1268,6 +1277,8 @@ def cli( prompt_dsn=prompt_dsn, auto_vertical_output=auto_vertical_output, warn=warn, + histfile=histfile, + alias_dsn=dsn, ) # Choose which ever one has a valid value. @@ -1306,7 +1317,6 @@ def cli( ) exit(1) pgcli.connect_uri(dsn_config) - pgcli.dsn_alias = dsn elif "://" in database: pgcli.connect_uri(database) elif "=" in database and service is None: From 25880681716ccf7bd802c85d08a419ac84b0d9e9 Mon Sep 17 00:00:00 2001 From: laixintao Date: Wed, 2 Dec 2020 20:07:01 +0800 Subject: [PATCH 3/3] fix black format. --- pgcli/main.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pgcli/main.py b/pgcli/main.py index ac57e389b..469293c36 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -165,7 +165,7 @@ def __init__( auto_vertical_output=False, warn=None, histfile=None, - alias_dsn=None + alias_dsn=None, ): self.force_passwd_prompt = force_passwd_prompt @@ -1203,9 +1203,7 @@ def echo_via_pager(self, text, color=None): @click.option( "--warn/--no-warn", default=None, help="Warn before running a destructive query." ) -@click.option( - "--histfile", default=None, help="Specify history file location." -) +@click.option("--histfile", default=None, help="Specify history file location.") @click.argument("dbname", default=lambda: None, envvar="PGDATABASE", nargs=1) @click.argument("username", default=lambda: None, envvar="PGUSER", nargs=1) def cli(