Skip to content

Commit

Permalink
feat: add shortcut function get
Browse files Browse the repository at this point in the history
  • Loading branch information
nikaro committed Nov 30, 2023
1 parent 54224ac commit a3a90cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ pip install sopsy
```python
from sopsy import Sops

secrets = Sops("secrets.yml").decrypt()
sops = Sops("secrets.yml")

my_secret_key = sops.get("my_secret_key")
# or
secrets = sops.decrypt()
my_secret_key = secrets.get("my_secret_key")

print(my_secret_key)
Expand Down
7 changes: 7 additions & 0 deletions src/sopsy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ def encrypt(self: Self, *, to_dict: bool = True) -> SopsyCmdOutput:
cmd = ["sops", "--encrypt", *self.global_args, str(self.file)]
return self._run_cmd(cmd, to_dict=to_dict)

def get(self: Self, key: str, *, default: str | None = None) -> str | None:
"""Get a specific key from a SOPS encrypted file."""
data = self.decrypt()
if isinstance(data, dict):
return data.get(key) or default
return default

def rotate(self: Self, *, to_dict: bool = True) -> SopsyCmdOutput:
"""Rotate encryption keys and re-encrypt values from SOPS file."""
cmd = ["sops", "--rotate", *self.global_args, str(self.file)]
Expand Down

0 comments on commit a3a90cd

Please sign in to comment.