Use API key from environment if that exists

This commit is contained in:
Jonas Widen 2025-03-16 13:06:59 +01:00
parent b66b616d61
commit 116c8deef7

View File

@ -2,12 +2,28 @@
local M = {} local M = {}
local api_key = vim.g.gemini_api_key -- Get API key from global variable local function get_api_key()
-- Check for environment variable
local api_key = os.getenv("GEMINI_API_KEY")
if api_key then
return api_key
end
-- Check for Neovim global variable
api_key = vim.g.gemini_api_key
if api_key then
return api_key
end
return nil -- API key not found
end
local function make_request(prompt) local function make_request(prompt)
local api_key = get_api_key() -- Get API key from environment or global variable
if not api_key then if not api_key then
vim.notify( vim.notify(
"Google AI API key not set. Set g.gemini_api_key in your init.vim or init.lua.", "Google AI API key not set. Set GEMINI_API_KEY environment variable or g.gemini_api_key in your init.vim or init.lua.",
vim.log.levels.ERROR vim.log.levels.ERROR
) )
return nil return nil