|
82 | 82 |
|
83 | 83 | from getpass import getuser |
84 | 84 |
|
85 | | -from psycopg import OperationalError, InterfaceError, Notify |
| 85 | +from psycopg import DatabaseError, OperationalError, InterfaceError, Notify |
86 | 86 | from psycopg.conninfo import make_conninfo, conninfo_to_dict |
87 | 87 | from psycopg.errors import Diagnostic |
88 | 88 |
|
@@ -237,6 +237,7 @@ def __init__( |
237 | 237 | self.never_passwd_prompt = never_passwd_prompt |
238 | 238 | self.pgexecute = pgexecute |
239 | 239 | self.dsn_alias = None |
| 240 | + self.service_name = None |
240 | 241 | self.watch_command = None |
241 | 242 | self.force_destructive = force_destructive |
242 | 243 |
|
@@ -466,6 +467,18 @@ def refresh_callback(): |
466 | 467 | arg_type=NO_QUERY, |
467 | 468 | ) |
468 | 469 | self.pgspecial.register(self.execute_from_file, "\\i", "\\i filename", "Execute commands from file.") |
| 470 | + self.pgspecial.register( |
| 471 | + self.change_directory, |
| 472 | + "\\cd", |
| 473 | + "\\cd [directory]", |
| 474 | + "Change the current working directory.", |
| 475 | + ) |
| 476 | + self.pgspecial.register( |
| 477 | + self.list_directory, |
| 478 | + "\\ls", |
| 479 | + "\\ls [path]", |
| 480 | + "List files in a directory.", |
| 481 | + ) |
469 | 482 | self.pgspecial.register( |
470 | 483 | self.write_to_file, |
471 | 484 | "\\o", |
@@ -611,6 +624,28 @@ def execute_from_file(self, pattern, **_): |
611 | 624 | explain_mode=self.explain_mode, |
612 | 625 | ) |
613 | 626 |
|
| 627 | + def change_directory(self, pattern, **_): |
| 628 | + try: |
| 629 | + directory = pathlib.Path(pattern or "~").expanduser() |
| 630 | + os.chdir(directory) |
| 631 | + except (OSError, RuntimeError) as e: |
| 632 | + return [(None, None, None, str(e), "", False, True)] |
| 633 | + |
| 634 | + return [(None, None, None, str(pathlib.Path.cwd()), "", True, True)] |
| 635 | + |
| 636 | + def list_directory(self, pattern, **_): |
| 637 | + try: |
| 638 | + path = pathlib.Path(pattern or ".").expanduser() |
| 639 | + path.stat() |
| 640 | + entries = list(path.iterdir()) if path.is_dir() else [path] |
| 641 | + entries_with_types = [(entry, entry.is_dir()) for entry in entries] |
| 642 | + entries_with_types.sort(key=lambda item: (not item[1], item[0].name.casefold(), item[0].name)) |
| 643 | + output = "\n".join(entry.name + (os.sep if is_directory else "") for entry, is_directory in entries_with_types) |
| 644 | + except (OSError, RuntimeError) as e: |
| 645 | + return [(None, None, None, str(e), "", False, True)] |
| 646 | + |
| 647 | + return [(None, None, None, output, "", True, True)] |
| 648 | + |
614 | 649 | def write_to_logfile(self, pattern, **_): |
615 | 650 | if not pattern: |
616 | 651 | self.log_file = None |
@@ -703,6 +738,7 @@ def connect_service(self, service, user): |
703 | 738 | port=service_config.get("port"), |
704 | 739 | passwd=service_config.get("password"), |
705 | 740 | ) |
| 741 | + self.service_name = service or os.getenv("PGSERVICE") |
706 | 742 |
|
707 | 743 | def connect_uri(self, uri): |
708 | 744 | kwargs = conninfo_to_dict(uri) |
@@ -869,6 +905,7 @@ def should_ask_for_password(exc): |
869 | 905 | sys.exit(1) |
870 | 906 |
|
871 | 907 | self.pgexecute = pgexecute |
| 908 | + self.service_name = None |
872 | 909 |
|
873 | 910 | def handle_editor_command(self, text): |
874 | 911 | r""" |
@@ -1133,7 +1170,7 @@ def run_cli(self): |
1133 | 1170 |
|
1134 | 1171 | try: |
1135 | 1172 | text = self.handle_editor_command(text) |
1136 | | - except RuntimeError as e: |
| 1173 | + except (RuntimeError, DatabaseError) as e: |
1137 | 1174 | logger.error("sql: %r, error: %r", text, e) |
1138 | 1175 | logger.error("traceback: %r", traceback.format_exc()) |
1139 | 1176 | click.secho(str(e), err=True, fg="red") |
@@ -1494,6 +1531,7 @@ def get_completions(self, text, cursor_positition): |
1494 | 1531 | def get_prompt(self, string): |
1495 | 1532 | # should be before replacing \\d |
1496 | 1533 | string = string.replace("\\dsn_alias", self.dsn_alias or "") |
| 1534 | + string = string.replace("\\service", self.service_name or "") |
1497 | 1535 | string = string.replace("\\t", self.now.strftime("%x %X")) |
1498 | 1536 | string = string.replace("\\u", self.pgexecute.user or "(none)") |
1499 | 1537 | string = string.replace("\\H", self.pgexecute.host or "(none)") |
|
0 commit comments