From 3b5c647cbd3d19a1c26e94e53a75527d828ea917 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Sun, 12 Jan 2025 13:28:10 +0200 Subject: [PATCH] feat(fal): support basic fal profiles --- projects/fal/src/fal/auth/__init__.py | 9 ++++++--- projects/fal/src/fal/config.py | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 projects/fal/src/fal/config.py diff --git a/projects/fal/src/fal/auth/__init__.py b/projects/fal/src/fal/auth/__init__.py index de83a6e1..618f745f 100644 --- a/projects/fal/src/fal/auth/__init__.py +++ b/projects/fal/src/fal/auth/__init__.py @@ -6,6 +6,7 @@ import click from fal.auth import auth0, local +from fal.config import Config from fal.console import console from fal.console.icons import CHECK_ICON from fal.exceptions.auth import UnauthenticatedException @@ -16,9 +17,11 @@ def key_credentials() -> tuple[str, str] | None: if os.environ.get("FAL_FORCE_AUTH_BY_USER") == "1": return None - if "FAL_KEY" in os.environ: - key = os.environ["FAL_KEY"] - key_id, key_secret = key.split(":", 1) + config = Config() + + fal_key = os.environ.get("FAL_KEY") or config.get("key") + if fal_key: + key_id, key_secret = fal_key.split(":", 1) return (key_id, key_secret) elif "FAL_KEY_ID" in os.environ and "FAL_KEY_SECRET" in os.environ: return (os.environ["FAL_KEY_ID"], os.environ["FAL_KEY_SECRET"]) diff --git a/projects/fal/src/fal/config.py b/projects/fal/src/fal/config.py new file mode 100644 index 00000000..d1ef2b74 --- /dev/null +++ b/projects/fal/src/fal/config.py @@ -0,0 +1,23 @@ +import os + +import tomli + + +class Config: + DEFAULT_CONFIG_PATH = "~/.fal/config.toml" + DEFAULT_PROFILE = "default" + + def __init__(self): + self.config_path = os.path.expanduser( + os.getenv("FAL_CONFIG_PATH", self.DEFAULT_CONFIG_PATH) + ) + self.profile = os.getenv("FAL_PROFILE", self.DEFAULT_PROFILE) + + try: + with open(self.config_path, "rb") as file: + self.config = tomli.load(file) + except FileNotFoundError: + self.config = {} + + def get(self, key): + return self.config.get(self.profile, {}).get(key)