Skip to content
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
6 changes: 3 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Upcoming (TBD)
==============
Upcoming Release (TBD)
======================

Features
--------

* Added explicit error handle to get_password_from_file with EAFP.

Internal
--------
Expand Down
1 change: 1 addition & 0 deletions mycli/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Contributors:
* Abirami P
* Adam Chainz
* Aljosha Papsch
* Allrob
* Andy Teijelo Pérez
* Angelo Lupo
* Artem Bezsmertnyi
Expand Down
20 changes: 14 additions & 6 deletions mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@

SUPPORT_INFO = "Home: http://mycli.net\n" "Bug tracker: https://github.com/dbcli/mycli/issues"

class PasswordFileError(Exception):
"""Base exception for errors related to reading password files."""
pass

class MyCli(object):
default_prompt = "\\t \\u@\\h:\\d> "
Expand Down Expand Up @@ -536,14 +539,19 @@ def _connect():
sys.exit(1)

def get_password_from_file(self, password_file):
password_from_file = None
if password_file:
if (os.path.isfile(password_file) or stat.S_ISFIFO(os.stat(password_file).st_mode)) and os.access(password_file, os.R_OK):
try:
with open(password_file) as fp:
password_from_file = fp.readline()
password_from_file = password_from_file.rstrip().lstrip()

return password_from_file
password = fp.readline().strip()
return password
except FileNotFoundError:
raise PasswordFileError(f"Password file '{password_file}' not found") from None
except PermissionError:
raise PasswordFileError(f"Permission denied reading password file '{password_file}'") from None
except IsADirectoryError:
raise PasswordFileError(f"Path '{password_file}' is a directory, not a file") from None
except Exception as e:
raise PasswordFileError(f"Error reading password file '{password_file}': {str(e)}") from None

def handle_editor_command(self, text):
r"""Editor command is any query that is prefixed or suffixed by a '\e'.
Expand Down