mirror of
https://github.com/usestrix/strix.git
synced 2026-08-28 05:25:00 +00:00
fix(cli): report a leftover temporary secret file instead of hiding it
This commit is contained in:
parent
8d66c66968
commit
268769fc45
1 changed files with 18 additions and 6 deletions
|
|
@ -23,14 +23,26 @@ def write_secret_text(path: Path, text: str) -> None:
|
|||
try:
|
||||
with os.fdopen(fd, "w", encoding="utf-8") as handle:
|
||||
handle.write(text)
|
||||
except BaseException:
|
||||
with contextlib.suppress(OSError):
|
||||
tmp.unlink()
|
||||
except BaseException as exc:
|
||||
_cleanup_tmp(tmp, exc)
|
||||
raise
|
||||
|
||||
try:
|
||||
tmp.replace(path)
|
||||
except BaseException:
|
||||
with contextlib.suppress(OSError):
|
||||
tmp.unlink()
|
||||
except BaseException as exc:
|
||||
_cleanup_tmp(tmp, exc)
|
||||
raise
|
||||
|
||||
|
||||
def _cleanup_tmp(tmp: Path, cause: BaseException) -> None:
|
||||
"""Delete the temporary secret file. A failed delete must not stay silent."""
|
||||
try:
|
||||
tmp.unlink()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
except OSError:
|
||||
message = (
|
||||
f"could not store the secret, and the temporary file {tmp} "
|
||||
f"still holds it. Delete the file manually."
|
||||
)
|
||||
raise OSError(message) from cause
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue