Skip to content

Commit 48e88b7

Browse files
Saga4saga4
andauthored
Add env variable to shell config if present only in env variable (#740)
Co-authored-by: saga4 <saga4@codeflashs-MacBook-Air.local>
1 parent 9fdf45f commit 48e88b7

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

codeflash/code_utils/env_utils.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
from codeflash.cli_cmds.console import logger
1111
from codeflash.code_utils.code_utils import exit_with_message
1212
from codeflash.code_utils.formatter import format_code
13-
from codeflash.code_utils.shell_utils import read_api_key_from_shell_config
14-
from codeflash.lsp.helpers import is_LSP_enabled
13+
from codeflash.code_utils.shell_utils import read_api_key_from_shell_config, save_api_key_to_rc
1514

1615

1716
def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool = True) -> bool: # noqa
@@ -35,12 +34,21 @@ def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool =
3534

3635
@lru_cache(maxsize=1)
3736
def get_codeflash_api_key() -> str:
38-
# prefer shell config over env var in lsp mode
39-
api_key = (
40-
read_api_key_from_shell_config()
41-
if is_LSP_enabled()
42-
else os.environ.get("CODEFLASH_API_KEY") or read_api_key_from_shell_config()
43-
)
37+
# Check environment variable first
38+
env_api_key = os.environ.get("CODEFLASH_API_KEY")
39+
shell_api_key = read_api_key_from_shell_config()
40+
41+
# If we have an env var but it's not in shell config, save it for persistence
42+
if env_api_key and not shell_api_key:
43+
try:
44+
from codeflash.either import is_successful
45+
result = save_api_key_to_rc(env_api_key)
46+
if is_successful(result):
47+
logger.debug(f"Automatically saved API key from environment to shell config: {result.unwrap()}")
48+
except Exception as e:
49+
logger.debug(f"Failed to automatically save API key to shell config: {e}")
50+
51+
api_key = env_api_key or shell_api_key
4452

4553
api_secret_docs_message = "For more information, refer to the documentation at [https://docs.codeflash.ai/getting-started/codeflash-github-actions#add-your-api-key-to-your-repository-secrets]." # noqa
4654
if not api_key:

0 commit comments

Comments
 (0)