diff --git a/pgcli/main.py b/pgcli/main.py index b1468985f..469293c36 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) @@ -1194,6 +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.argument("dbname", default=lambda: None, envvar="PGDATABASE", nargs=1) @click.argument("username", default=lambda: None, envvar="PGUSER", nargs=1) def cli( @@ -1217,6 +1227,7 @@ def cli( auto_vertical_output, list_dsn, warn, + histfile, ): if version: print("Version:", __version__) @@ -1264,6 +1275,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. @@ -1302,7 +1315,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: 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