enable bug-bear linting rules

This commit is contained in:
Jörg Thalheim
2024-09-02 13:26:07 +02:00
parent b313f2d066
commit 109d1faf9e
33 changed files with 214 additions and 104 deletions

View File

@@ -38,8 +38,8 @@ def remove_object(path: Path, name: str) -> list[Path]:
try:
shutil.rmtree(path / name)
paths_to_commit.append(path / name)
except FileNotFoundError:
raise ClanError(f"{name} not found in {path}")
except FileNotFoundError as e:
raise ClanError(f"{name} not found in {path}") from e
if not os.listdir(path):
os.rmdir(path)
return paths_to_commit

View File

@@ -22,10 +22,12 @@ def extract_public_key(filepath: Path) -> str:
if line.startswith("# public key:"):
# Extract and return the public key part after the prefix
return line.strip().split(": ")[1]
except FileNotFoundError:
raise ClanError(f"The file at {filepath} was not found.")
except Exception as e:
raise ClanError(f"An error occurred while extracting the public key: {e}")
except FileNotFoundError as e:
raise ClanError(f"The file at {filepath} was not found.") from e
except OSError as e:
raise ClanError(
f"An error occurred while extracting the public key: {e}"
) from e
raise ClanError(f"Could not find the public key in the file at {filepath}.")

View File

@@ -85,11 +85,19 @@ def encrypt_secret(
flake_dir: Path,
secret_path: Path,
value: IO[str] | str | bytes | None,
add_users: list[str] = [],
add_machines: list[str] = [],
add_groups: list[str] = [],
meta: dict = {},
add_users: list[str] | None = None,
add_machines: list[str] | None = None,
add_groups: list[str] | None = None,
meta: dict | None = None,
) -> None:
if meta is None:
meta = {}
if add_groups is None:
add_groups = []
if add_machines is None:
add_machines = []
if add_users is None:
add_users = []
key = ensure_sops_key(flake_dir)
recipient_keys = set([])

View File

@@ -147,8 +147,10 @@ def encrypt_file(
secret_path: Path,
content: IO[str] | str | bytes | None,
pubkeys: list[str],
meta: dict = {},
meta: dict | None = None,
) -> None:
if meta is None:
meta = {}
folder = secret_path.parent
folder.mkdir(parents=True, exist_ok=True)
@@ -225,10 +227,10 @@ def write_key(path: Path, publickey: str, overwrite: bool) -> None:
if not overwrite:
flags |= os.O_EXCL
fd = os.open(path / "key.json", flags)
except FileExistsError:
except FileExistsError as e:
raise ClanError(
f"{path.name} already exists in {path}. Use --force to overwrite."
)
) from e
with os.fdopen(fd, "w") as f:
json.dump({"publickey": publickey, "type": "age"}, f, indent=2)
@@ -238,7 +240,7 @@ def read_key(path: Path) -> str:
try:
key = json.load(f)
except json.JSONDecodeError as e:
raise ClanError(f"Failed to decode {path.name}: {e}")
raise ClanError(f"Failed to decode {path.name}: {e}") from e
if key["type"] != "age":
raise ClanError(
f"{path.name} is not an age key but {key['type']}. This is not supported"