-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: add auth_options decorator for CLI commands #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
16c1191
refactor: add auth_options decorator for CLI commands
mdesmet 73bbbb3
style: format auth_options decorator
mdesmet 6b4334b
refactor: remove click context usage in favor of direct parameter pas…
mdesmet 34a829c
refactor: remove unused config mapping in auth decorator
mdesmet ee8d1a5
fix: return instead of raising click.Abort in serve
mdesmet ace1521
fix: raise click.Abort on missing token and instance name
mdesmet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import json | ||
| import os | ||
| import re | ||
| from functools import wraps | ||
| from pathlib import Path | ||
|
|
||
| import click | ||
| from dotenv import load_dotenv | ||
|
|
||
|
|
||
| def load_config_from_file(): | ||
| """Load configuration from ~/.altimate/altimate.json if it exists.""" | ||
| config_path = Path.home() / ".altimate" / "altimate.json" | ||
|
|
||
| if not config_path.exists(): | ||
| return {} | ||
|
|
||
| try: | ||
| with config_path.open() as f: | ||
| config = json.load(f) | ||
| return config | ||
| except (OSError, json.JSONDecodeError) as e: | ||
| click.echo(f"Warning: Failed to load config from {config_path}: {e}", err=True) | ||
| return {} | ||
|
|
||
|
|
||
| def substitute_env_vars(value): | ||
| """Replace ${env:ENV_VARIABLE} patterns with actual environment variable values.""" | ||
| if not isinstance(value, str): | ||
| return value | ||
|
|
||
| # Pattern to match ${env:VARIABLE_NAME} | ||
| pattern = r"\$\{env:([^}]+)\}" | ||
|
|
||
| def replacer(match): | ||
| env_var = match.group(1) | ||
| return os.environ.get(env_var, match.group(0)) | ||
|
|
||
| return re.sub(pattern, replacer, value) | ||
|
|
||
|
|
||
| def process_config(config): | ||
| """Process configuration dictionary to substitute environment variables.""" | ||
| processed = {} | ||
| for key, value in config.items(): | ||
| processed[key] = substitute_env_vars(value) | ||
| return processed | ||
|
|
||
|
|
||
| def auth_options(f): | ||
| """Decorator to add authentication options to commands.""" | ||
|
|
||
| @click.option("--token", required=False, help="Your API token for authentication.", hide_input=True) | ||
| @click.option("--instance-name", required=False, help="Your tenant ID.") | ||
| @click.option("--backend-url", required=False, help="Altimate's Backend URL", default="https://api.myaltimate.com") | ||
| @wraps(f) | ||
| def wrapper(token, instance_name, backend_url, *args, **kwargs): | ||
| # Load .env file from current directory if it exists | ||
| load_dotenv() | ||
|
|
||
| # Load configuration from file | ||
| file_config = load_config_from_file() | ||
| file_config = process_config(file_config) | ||
|
|
||
| # Apply file config first, then override with CLI arguments if provided | ||
| final_token = token | ||
| final_instance_name = instance_name | ||
| final_backend_url = backend_url | ||
|
|
||
| # Use file config if CLI argument not provided | ||
| if final_token is None and "altimateApiKey" in file_config: | ||
| final_token = file_config["altimateApiKey"] | ||
| if final_instance_name is None and "altimateInstanceName" in file_config: | ||
| final_instance_name = file_config["altimateInstanceName"] | ||
| if final_backend_url == "https://api.myaltimate.com" and "altimateUrl" in file_config: | ||
| final_backend_url = file_config["altimateUrl"] | ||
|
|
||
| # Set defaults if nothing was provided | ||
| if final_token is None: | ||
| final_token = None | ||
| if final_instance_name is None: | ||
| final_instance_name = None | ||
| if final_backend_url is None: | ||
| final_backend_url = "https://api.myaltimate.com" | ||
|
|
||
| return f(final_token, final_instance_name, final_backend_url, *args, **kwargs) | ||
|
|
||
| return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.