mirror of
https://github.com/Aider-AI/aider.git
synced 2025-06-03 11:14:59 +00:00
refactor: consolidate API key and environment variable handling
This commit is contained in:
parent
bdb08d7c78
commit
3efcd154f3
8 changed files with 239 additions and 208 deletions
103
aider/main.py
103
aider/main.py
|
@ -456,65 +456,9 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F
|
|||
# Load the .env file specified in the arguments
|
||||
loaded_dotenvs = load_dotenv_files(git_root, args.env_file, args.encoding)
|
||||
|
||||
dump(os.environ.get("ANTHROPIC_API_KEY"))
|
||||
|
||||
# Parse again to include any arguments that might have been defined in .env
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
dump(os.environ.get("ANTHROPIC_API_KEY"))
|
||||
|
||||
# Process any environment variables set via --set-env
|
||||
if args.set_env:
|
||||
for env_setting in args.set_env:
|
||||
try:
|
||||
name, value = env_setting.split("=", 1)
|
||||
os.environ[name.strip()] = value.strip()
|
||||
except ValueError:
|
||||
io.tool_error(f"Invalid --set-env format: {env_setting}")
|
||||
io.tool_output("Format should be: ENV_VAR_NAME=value")
|
||||
return 1
|
||||
|
||||
# Process any API keys set via --api-key
|
||||
if args.api_key:
|
||||
for api_setting in args.api_key:
|
||||
try:
|
||||
provider, key = api_setting.split("=", 1)
|
||||
env_var = f"{provider.strip().upper()}_API_KEY"
|
||||
os.environ[env_var] = key.strip()
|
||||
dump(env_var)
|
||||
except ValueError:
|
||||
io.tool_error(f"Invalid --api-key format: {api_setting}")
|
||||
io.tool_output("Format should be: provider=key")
|
||||
return 1
|
||||
|
||||
dump(os.environ.get("ANTHROPIC_API_KEY"))
|
||||
|
||||
if args.anthropic_api_key:
|
||||
io.tool_warning("--anthropic-api-key is deprecated, use --api-key anthropic=<key>")
|
||||
os.environ["ANTHROPIC_API_KEY"] = args.anthropic_api_key
|
||||
|
||||
if args.openai_api_key:
|
||||
io.tool_warning("--openai-api-key is deprecated, use --api-key openai=<key>")
|
||||
os.environ["OPENAI_API_KEY"] = args.openai_api_key
|
||||
if args.openai_api_base:
|
||||
io.tool_warning("--openai-api-base is deprecated, use --set-env OPENAI_API_BASE=<value>")
|
||||
os.environ["OPENAI_API_BASE"] = args.openai_api_base
|
||||
if args.openai_api_version:
|
||||
io.tool_warning(
|
||||
"--openai-api-version is deprecated, use --set-env OPENAI_API_VERSION=<value>"
|
||||
)
|
||||
os.environ["OPENAI_API_VERSION"] = args.openai_api_version
|
||||
if args.openai_api_type:
|
||||
io.tool_warning("--openai-api-type is deprecated, use --set-env OPENAI_API_TYPE=<value>")
|
||||
os.environ["OPENAI_API_TYPE"] = args.openai_api_type
|
||||
if args.openai_organization_id:
|
||||
io.tool_warning(
|
||||
"--openai-organization-id is deprecated, use --set-env OPENAI_ORGANIZATION=<value>"
|
||||
)
|
||||
os.environ["OPENAI_ORGANIZATION"] = args.openai_organization_id
|
||||
|
||||
dump(os.environ.get("ANTHROPIC_API_KEY"))
|
||||
|
||||
if args.analytics_disable:
|
||||
analytics = Analytics(permanently_disable=True)
|
||||
print("Analytics have been permanently disabled.")
|
||||
|
@ -584,6 +528,53 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F
|
|||
io = get_io(False)
|
||||
io.tool_warning("Terminal does not support pretty output (UnicodeDecodeError)")
|
||||
|
||||
# Process any environment variables set via --set-env
|
||||
if args.set_env:
|
||||
for env_setting in args.set_env:
|
||||
try:
|
||||
name, value = env_setting.split("=", 1)
|
||||
os.environ[name.strip()] = value.strip()
|
||||
except ValueError:
|
||||
io.tool_error(f"Invalid --set-env format: {env_setting}")
|
||||
io.tool_output("Format should be: ENV_VAR_NAME=value")
|
||||
return 1
|
||||
|
||||
# Process any API keys set via --api-key
|
||||
if args.api_key:
|
||||
for api_setting in args.api_key:
|
||||
try:
|
||||
provider, key = api_setting.split("=", 1)
|
||||
env_var = f"{provider.strip().upper()}_API_KEY"
|
||||
os.environ[env_var] = key.strip()
|
||||
except ValueError:
|
||||
io.tool_error(f"Invalid --api-key format: {api_setting}")
|
||||
io.tool_output("Format should be: provider=key")
|
||||
return 1
|
||||
|
||||
if args.anthropic_api_key:
|
||||
os.environ["ANTHROPIC_API_KEY"] = args.anthropic_api_key
|
||||
|
||||
if args.openai_api_key:
|
||||
os.environ["OPENAI_API_KEY"] = args.openai_api_key
|
||||
if args.openai_api_base:
|
||||
os.environ["OPENAI_API_BASE"] = args.openai_api_base
|
||||
if args.openai_api_version:
|
||||
io.tool_warning(
|
||||
"--openai-api-version will be deprecated soon, use --set-env OPENAI_API_VERSION=<value>"
|
||||
)
|
||||
os.environ["OPENAI_API_VERSION"] = args.openai_api_version
|
||||
if args.openai_api_type:
|
||||
io.tool_warning(
|
||||
"--openai-api-type will be deprecated soon, use --set-env OPENAI_API_TYPE=<value>"
|
||||
)
|
||||
os.environ["OPENAI_API_TYPE"] = args.openai_api_type
|
||||
if args.openai_organization_id:
|
||||
io.tool_warning(
|
||||
"--openai-organization-id will be deprecated soon, use --set-env"
|
||||
" OPENAI_ORGANIZATION=<value>"
|
||||
)
|
||||
os.environ["OPENAI_ORGANIZATION"] = args.openai_organization_id
|
||||
|
||||
analytics = Analytics(logfile=args.analytics_log, permanently_disable=args.analytics_disable)
|
||||
if args.analytics is not False:
|
||||
if analytics.need_to_ask(args.analytics):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue