|
3 | 3 | import re |
4 | 4 | from functools import wraps |
5 | 5 | from pathlib import Path |
| 6 | +from typing import Dict |
| 7 | +from typing import Optional |
6 | 8 |
|
7 | 9 | import click |
8 | 10 | from dotenv import load_dotenv |
9 | 11 |
|
10 | 12 |
|
11 | | -def load_config_from_file(): |
| 13 | +def load_config_from_file() -> Optional[Dict]: |
12 | 14 | """Load configuration from ~/.altimate/altimate.json if it exists.""" |
13 | 15 | config_path = Path.home() / ".altimate" / "altimate.json" |
14 | 16 |
|
15 | 17 | if not config_path.exists(): |
16 | | - return {} |
| 18 | + return None |
17 | 19 |
|
18 | 20 | try: |
19 | 21 | with config_path.open() as f: |
20 | 22 | config = json.load(f) |
21 | 23 | return config |
22 | 24 | except (OSError, json.JSONDecodeError) as e: |
23 | 25 | click.echo(f"Warning: Failed to load config from {config_path}: {e}", err=True) |
24 | | - return {} |
| 26 | + return None |
25 | 27 |
|
26 | 28 |
|
27 | 29 | def substitute_env_vars(value): |
@@ -58,30 +60,22 @@ def wrapper(token, instance_name, backend_url, *args, **kwargs): |
58 | 60 | # Load .env file from current directory if it exists |
59 | 61 | load_dotenv() |
60 | 62 |
|
61 | | - # Load configuration from file |
62 | | - file_config = load_config_from_file() |
63 | | - file_config = process_config(file_config) |
64 | | - |
65 | | - # Apply file config first, then override with CLI arguments if provided |
66 | 63 | final_token = token |
67 | 64 | final_instance_name = instance_name |
68 | 65 | final_backend_url = backend_url |
69 | 66 |
|
70 | | - # Use file config if CLI argument not provided |
71 | | - if final_token is None and "altimateApiKey" in file_config: |
72 | | - final_token = file_config["altimateApiKey"] |
73 | | - if final_instance_name is None and "altimateInstanceName" in file_config: |
74 | | - final_instance_name = file_config["altimateInstanceName"] |
75 | | - if final_backend_url == "https://api.myaltimate.com" and "altimateUrl" in file_config: |
76 | | - final_backend_url = file_config["altimateUrl"] |
77 | | - |
78 | | - # Set defaults if nothing was provided |
79 | | - if final_token is None: |
80 | | - final_token = None |
81 | | - if final_instance_name is None: |
82 | | - final_instance_name = None |
83 | | - if final_backend_url is None: |
84 | | - final_backend_url = "https://api.myaltimate.com" |
| 67 | + if final_token is None and final_instance_name is None: |
| 68 | + # Try to Load configuration from file if no CLI arguments are provided |
| 69 | + file_config = load_config_from_file() |
| 70 | + if file_config is not None: |
| 71 | + # File config is provided |
| 72 | + file_config = process_config(file_config) |
| 73 | + if "altimateApiKey" in file_config: |
| 74 | + final_token = file_config["altimateApiKey"] |
| 75 | + if "altimateInstanceName" in file_config: |
| 76 | + final_instance_name = file_config["altimateInstanceName"] |
| 77 | + if "altimateUrl" in file_config: |
| 78 | + final_backend_url = file_config["altimateUrl"] or final_backend_url |
85 | 79 |
|
86 | 80 | return f(final_token, final_instance_name, final_backend_url, *args, **kwargs) |
87 | 81 |
|
|
0 commit comments