Load .env from home, git, cwd, cmd line arg

This commit is contained in:
Paul Gauthier 2024-07-06 21:59:20 -03:00
parent 8a0480a56d
commit 46374fb07a
5 changed files with 32 additions and 11 deletions

View file

@ -220,9 +220,9 @@ def generate_search_path_list(default_fname, git_root, command_line_file):
files.append(Path.home() / default_file) # homedir
if git_root:
files.append(Path(git_root) / default_file) # git root
files.append(default_file.resolve())
if command_line_file:
files.append(command_line_file)
files.append(default_file.resolve())
files = [Path(fn).resolve() for fn in files]
files.reverse()
uniq = []
@ -255,6 +255,20 @@ def register_models(git_root, model_settings_fname, io):
return None
def load_dotenv_files(git_root, dotenv_fname):
dotenv_files = generate_search_path_list(
".env",
git_root,
dotenv_fname,
)
loaded = []
for fname in dotenv_files:
if Path(fname).exists():
loaded.append(fname)
load_dotenv(fname)
return loaded
def register_litellm_models(git_root, model_metadata_fname, io):
model_metatdata_files = generate_search_path_list(
".aider.model.metadata.json", git_root, model_metadata_fname
@ -294,8 +308,7 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F
args, unknown = parser.parse_known_args(argv)
# Load the .env file specified in the arguments
if hasattr(args, "env_file"):
load_dotenv(args.env_file)
loaded_dotenvs = load_dotenv_files(git_root, args.env_file)
# Parse again to include any arguments that might have been defined in .env
args = parser.parse_args(argv)
@ -342,6 +355,9 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F
editingmode=editing_mode,
)
for fname in loaded_dotenvs:
io.tool_output(f"Loaded {fname}")
fnames = [str(Path(fn).resolve()) for fn in args.files]
if len(args.files) > 1:
good = True

View file

@ -6,4 +6,4 @@ have their keys and settings
specified in environment variables.
This can be done in your shell,
or by using a
[`.env` file](/docs/config/dotenv.html).
[.env file](/docs/config/dotenv.html).

View file

@ -12,12 +12,12 @@ your git repo.
{% include special-keys.md %}
Below is a sample of the file, which you
{% include env-keys-tip.md %}
Below is a sample of the YAML config file, which you
can also
[download from GitHub](https://github.com/paul-gauthier/aider/blob/main/aider/website/assets/sample.aider.conf.yml).
{% include env-keys-tip.md %}
<!--[[[cog
from aider.args import get_sample_yaml
from pathlib import Path

View file

@ -13,9 +13,14 @@ in the `.env` file.
{% include special-keys.md %}
Aider will look for a `.env` file in the
root of your git repo or in the current directory.
You can give it an explicit file to load with the `--env-file <filename>` parameter.
Aider will look for a `.env` file in these locations:
- Your home directory.
- The root of your git repo.
- The current directory.
- As specified with the `--env-file <filename>` parameter.
If the files above exist, they will be loaded in that order. Files loaded last will take priority.
Below is a sample `.env` file, which you
can also

View file

@ -14,7 +14,7 @@ The steps below are completely optional.
## Store your api keys
You can place your [api keys in a `.env` file](/docs/config/dotenv.html)
You can [store your api keys in a .env file](/docs/config/dotenv.html)
and they will be loaded automatically whenever you run aider.
## Enable Playwright