shell_gpt icon indicating copy to clipboard operation
shell_gpt copied to clipboard

Add support for reading `OPENAI_API_KEY` from a file path specified in the config

Open giladbarnea opened this issue 1 year ago • 1 comments

My sgpt config, including the .sgptrc file, is backed up via source control. This means I constantly need to manually make sure the OPENAI_API_KEY line is kept out of commits.

This is cumbersome and only takes one lapse in concentration to accidentally push my key.

This PR provides a simple fix by letting OPENAI_API_KEY point to a file path. This separate file would hold the key in plaintext, making it easier to keep out of version control.

image

Changes:

  • Added POTENTIAL_PATH_KEYS = ("OPENAI_API_KEY",) at the module level in sgpt/config.py. When reading the config, this determines if the value should be checked for referencing an existing file. If so, it reads the file's contents.
  • Extracted the logic for reading a config line, including file path support, into a new method: def _read_from_line(self, line: str) -> None.
    • This method now skips lines starting with # after stripping them, a minor out-of-scope change.
  • Added a new tests/test_config.py with three tests to cover both regression and the new behavior.

Corresponding issue: #588


Please let me know if you'd like me to remove or modify anything, add an integration test, or make any other adjustments. Thanks!

giladbarnea avatar Jul 07 '24 07:07 giladbarnea

You can put dummy value (or comment out OPENAI_API_KEY) and put into your .bashrc:

export OPENAI_API_KEY=<your key>

AlexanderYastrebov avatar Aug 30 '24 17:08 AlexanderYastrebov