|
8 | 8 |
|
9 | 9 |
|
10 | 10 | def get_env_value(key, default=None): |
11 | | - """Get value from environment variable with Chronicle prefix.""" |
12 | | - return os.getenv(f"CHRONICLE_{key}", default) |
| 11 | + """Get value from environment variable with Chronicle prefix.""" |
| 12 | + return os.getenv(f"CHRONICLE_{key}", default) |
13 | 13 |
|
14 | 14 |
|
15 | 15 | def add_common_options(func): |
16 | | - """Add common options to a command.""" |
17 | | - @wraps(func) |
18 | | - def wrapper(*args, **kwargs): |
19 | | - # Load environment variables from .env file |
20 | | - load_dotenv() |
| 16 | + """Add common options to a command.""" |
21 | 17 |
|
22 | | - # If options not provided via CLI, try to get from environment |
23 | | - if not kwargs.get("credentials_file"): |
24 | | - kwargs["credentials_file"] = get_env_value("CREDENTIALS_FILE") |
25 | | - if not kwargs.get("project_id"): |
26 | | - kwargs["project_id"] = get_env_value("PROJECT_ID") |
27 | | - if not kwargs.get("project_instance"): |
28 | | - kwargs["project_instance"] = get_env_value("INSTANCE") |
29 | | - if not kwargs.get("region"): |
30 | | - kwargs["region"] = get_env_value("REGION") |
| 18 | + # Add CLI options first |
| 19 | + @click.option( |
| 20 | + "--region", |
| 21 | + required=False, |
| 22 | + help="Region in which the target project is located. Can also be set via CHRONICLE_REGION env var.", |
| 23 | + ) |
| 24 | + @click.option( |
| 25 | + "--project-instance", |
| 26 | + required=False, |
| 27 | + help="Customer ID (uuid with dashes) for the Chronicle instance. Can also be set via CHRONICLE_INSTANCE env var.", |
| 28 | + ) |
| 29 | + @click.option( |
| 30 | + "--project-id", |
| 31 | + required=False, |
| 32 | + help="GCP project id or number. Can also be set via CHRONICLE_PROJECT_ID env var.", |
| 33 | + ) |
| 34 | + @click.option( |
| 35 | + "--credentials-file", |
| 36 | + required=False, |
| 37 | + help="Path to service account credentials file. Can also be set via CHRONICLE_CREDENTIALS_FILE env var.", |
| 38 | + ) |
| 39 | + @click.option( |
| 40 | + "--env-file", |
| 41 | + required=False, |
| 42 | + help="Path to .env file containing configuration variables.", |
| 43 | + ) |
| 44 | + @wraps(func) |
| 45 | + def wrapper(*args, **kwargs): |
| 46 | + # Load environment variables from .env file |
| 47 | + env_file = kwargs.pop("env_file", None) |
| 48 | + if env_file: |
| 49 | + load_dotenv(env_file) |
| 50 | + else: |
| 51 | + load_dotenv() |
31 | 52 |
|
32 | | - return func(*args, **kwargs) |
| 53 | + # Now validate required options |
| 54 | + missing = [] |
| 55 | + if not kwargs.get("credentials_file") and not get_env_value("CREDENTIALS_FILE"): |
| 56 | + missing.append("credentials-file (or CHRONICLE_CREDENTIALS_FILE)") |
| 57 | + if not kwargs.get("project_id") and not get_env_value("PROJECT_ID"): |
| 58 | + missing.append("project-id (or CHRONICLE_PROJECT_ID)") |
| 59 | + if not kwargs.get("project_instance") and not get_env_value("INSTANCE"): |
| 60 | + missing.append("project-instance (or CHRONICLE_INSTANCE)") |
| 61 | + if not kwargs.get("region") and not get_env_value("REGION"): |
| 62 | + missing.append("region (or CHRONICLE_REGION)") |
33 | 63 |
|
34 | | - # Add CLI options |
35 | | - wrapper = click.option( |
36 | | - "--credentials-file", |
37 | | - required=False, |
38 | | - help="Path to service account credentials file. Can also be set via CHRONICLE_CREDENTIALS_FILE env var.", |
39 | | - )(wrapper) |
40 | | - wrapper = click.option( |
41 | | - "--project-id", |
42 | | - required=False, |
43 | | - help="GCP project id or number. Can also be set via CHRONICLE_PROJECT_ID env var.", |
44 | | - )(wrapper) |
45 | | - wrapper = click.option( |
46 | | - "--project-instance", |
47 | | - required=False, |
48 | | - help="Customer ID (uuid with dashes) for the Chronicle instance. Can also be set via CHRONICLE_INSTANCE env var.", |
49 | | - )(wrapper) |
50 | | - wrapper = click.option( |
51 | | - "--region", |
52 | | - required=False, |
53 | | - help="Region in which the target project is located. Can also be set via CHRONICLE_REGION env var.", |
54 | | - )(wrapper) |
| 64 | + if missing: |
| 65 | + raise click.UsageError( |
| 66 | + f"Missing required options: {', '.join(missing)}\n" |
| 67 | + "These can be provided via command line options or environment variables." |
| 68 | + ) |
55 | 69 |
|
56 | | - # Ensure required options are provided either via CLI or environment |
57 | | - @wraps(wrapper) |
58 | | - def validate_options(*args, **kwargs): |
59 | | - missing = [] |
60 | | - if not kwargs.get("credentials_file"): |
61 | | - missing.append("credentials-file (or CHRONICLE_CREDENTIALS_FILE)") |
62 | | - if not kwargs.get("project_id"): |
63 | | - missing.append("project-id (or CHRONICLE_PROJECT_ID)") |
64 | | - if not kwargs.get("project_instance"): |
65 | | - missing.append("project-instance (or CHRONICLE_INSTANCE)") |
66 | | - if not kwargs.get("region"): |
67 | | - missing.append("region (or CHRONICLE_REGION)") |
| 70 | + # If options not provided via CLI, get from environment |
| 71 | + if not kwargs.get("credentials_file"): |
| 72 | + kwargs["credentials_file"] = get_env_value("CREDENTIALS_FILE") |
| 73 | + if not kwargs.get("project_id"): |
| 74 | + kwargs["project_id"] = get_env_value("PROJECT_ID") |
| 75 | + if not kwargs.get("project_instance"): |
| 76 | + kwargs["project_instance"] = get_env_value("INSTANCE") |
| 77 | + if not kwargs.get("region"): |
| 78 | + kwargs["region"] = get_env_value("REGION") |
68 | 79 |
|
69 | | - if missing: |
70 | | - raise click.UsageError( |
71 | | - f"Missing required options: {', '.join(missing)}\n" |
72 | | - "These can be provided via command line options or environment variables." |
73 | | - ) |
| 80 | + return func(*args, **kwargs) |
74 | 81 |
|
75 | | - return wrapper(*args, **kwargs) |
76 | | - |
77 | | - return validate_options |
| 82 | + return wrapper |
0 commit comments