jackMort / ChatGPT.nvim

ChatGPT Neovim Plugin: Effortless Natural Language Generation with OpenAI's ChatGPT API
Apache License 2.0
3.75k stars 312 forks source link

feat(api): added api_key_cmd configuration option #185

Closed MadsRC closed 1 year ago

MadsRC commented 1 year ago

This PR adds a feature to the plugin that introduces an alternative method of providing the OpenAI API key. In addition to providing the key via an OPENAI_API_KEY environment variable, the plugin also supports getting the API key from the output of arbitrary commands, configured via the api_key_cmd configuration parameter.

This allows users to securely provide the API key, without having to write it in clear-text to disk. It also prevents the API key from being readable by processes with access to the initial environment variables of neovim.

The default behaviour is to use the OPENAI_API_KEY environment variable, so as to not break backwards compatibility.

The README includes a couple of examples of how to use op or gpg to inject the secret into neovim.

Disclaimer: This is my first lua code since... 2005 or something, so I'm sure there's a prettier way of doing what I did here. Critique is welcome ;)

Since configuration wasn't initialized when the the api.lua file was being run, I had to introduce a setup() function to the api.lua file.

jackMort commented 1 year ago

Great work, thanks! Could you just fix the styling problem to pass checks?

MadsRC commented 1 year ago

Great work, thanks! Could you just fix the styling problem to pass checks?

Absolutely, just pushed a fix!