From d7592e487cd611dfac96eaf0595d02498aac75a3 Mon Sep 17 00:00:00 2001 From: "Nicklas H." Date: Wed, 22 May 2024 14:11:13 +0200 Subject: [PATCH] feat: Add function to retrieve the value of a field from 1Password CLI --- cogs/utils/utils.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cogs/utils/utils.py b/cogs/utils/utils.py index a44a4a6..98f1e90 100644 --- a/cogs/utils/utils.py +++ b/cogs/utils/utils.py @@ -11,6 +11,8 @@ from io import BytesIO from base64 import b64encode from pathlib import Path +from subprocess import check_output +from shutil import which from QuantumKat import discord_helper @@ -121,6 +123,28 @@ class UnsupportedImageFormatError(Exception): encoding = encoding_for_model("gpt-4o") +def get_field_from_1password(reference: str) -> str: + """ + Retrieves the value of a field from 1Password and returns it as a string. + + Requires the 1Password CLI to be installed and configured. + + Args: + reference (str): The reference to the token in 1Password. + + Returns: + str: The token retrieved from 1Password. + + Raises: + CalledProcessError: If the token cannot be retrieved from 1Password. + EnvironmentError: If the 1Password CLI is not installed. + """ + if which("op") is None: + raise EnvironmentError("The 1Password CLI is not installed. Please install it.") + token = check_output(["op", "read", reference]).decode("utf-8").strip() + return token + + def download_file( url: str, amount_or_limit: int = None,