Skip to content

Commit

Permalink
Improve formatting in display_debug_info function
Browse files Browse the repository at this point in the history
  • Loading branch information
alighazi288 committed Jan 5, 2025
1 parent 60255b3 commit 6463ad6
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/borg/helpers/passphrase.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def verification(cls, passphrase):
env_var_override="BORG_DISPLAY_PASSPHRASE",
):
pw_msg = textwrap.dedent(
f"""
f"""\
Your passphrase (between double-quotes): "{passphrase}"
Make sure the passphrase displayed above is exactly what you wanted.
Your passphrase (UTF-8 encoding in hex): {bin_to_hex(passphrase.encode("utf-8"))}
Expand All @@ -123,20 +123,24 @@ def verification(cls, passphrase):

@staticmethod
def display_debug_info(passphrase):
def fmt_var(env_var):
env_var_value = os.environ.get(env_var)
if env_var_value is not None:
return f'{env_var} = "{env_var_value}"'
else:
return f"# {env_var} is not set"

Check warning on line 131 in src/borg/helpers/passphrase.py

View check run for this annotation

Codecov / codecov/patch

src/borg/helpers/passphrase.py#L131

Added line #L131 was not covered by tests

if os.environ.get("BORG_DEBUG_PASSPHRASE") == "YES":
env_vars_str = ""
for env_var in ["BORG_PASSPHRASE", "BORG_PASSCOMMAND", "BORG_PASSPHRASE_FD"]:
env_var_value = os.environ.get(env_var)
if env_var_value is not None:
env_vars_str += f'{env_var} = "{env_var_value}"\n'
else:
env_vars_str += f"# {env_var} is not set\n"
passphrase_info = (
f"Incorrect passphrase!\n"
f'Passphrase used (between double-quotes): "{passphrase}"\n'
f"Same, UTF-8 encoded, in hex: {bin_to_hex(passphrase.encode('utf-8'))}\n"
f"Relevant Environment Variables:\n"
f"{env_vars_str}"
passphrase_info = textwrap.dedent(
f"""\
Incorrect passphrase!
Passphrase used (between double-quotes): "{passphrase}"
Same, UTF-8 encoded, in hex: {bin_to_hex(passphrase.encode('utf-8'))}
Relevant Environment Variables:
{fmt_var("BORG_PASSPHRASE")}
{fmt_var("BORG_PASSCOMMAND")}
{fmt_var("BORG_PASSPHRASE_FD")}
"""
)
print(passphrase_info, file=sys.stderr)

Expand Down

0 comments on commit 6463ad6

Please sign in to comment.