From 116c8deef7e75b7c40bf3a978fe61e5fe9f1f40e Mon Sep 17 00:00:00 2001 From: Jonas Widen Date: Sun, 16 Mar 2025 13:06:59 +0100 Subject: [PATCH] Use API key from environment if that exists --- plugin/gemini/api.lua | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/plugin/gemini/api.lua b/plugin/gemini/api.lua index 00bb741..d219e29 100644 --- a/plugin/gemini/api.lua +++ b/plugin/gemini/api.lua @@ -2,12 +2,28 @@ 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 api_key = get_api_key() -- Get API key from environment or global variable + if not api_key then 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 ) return nil