-
Notifications
You must be signed in to change notification settings - Fork 31
Add config file support for config resolution #729
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
Open
ubaskota
wants to merge
2
commits into
smithy-lang:config_integration_implementation
Choose a base branch
from
ubaskota:config_file_support
base: config_integration_implementation
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
76 changes: 76 additions & 0 deletions
76
packages/smithy-aws-core/src/smithy_aws_core/config/__init__.py
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,76 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import os | ||
| from pathlib import Path | ||
|
|
||
| from smithy_aws_core.config.file_parser import ( | ||
| FileType, | ||
| parse_config_file, | ||
| standardize, | ||
| ) | ||
| from smithy_aws_core.config.merged_config import MergedConfig | ||
|
|
||
| _DEFAULT_CONFIG_FILE = "~/.aws/config" | ||
| _DEFAULT_CREDENTIALS_FILE = "~/.aws/credentials" | ||
| _CONFIG_FILE_ENV_VAR = "AWS_CONFIG_FILE" | ||
| _CREDENTIALS_FILE_ENV_VAR = "AWS_SHARED_CREDENTIALS_FILE" | ||
|
|
||
|
|
||
| def _resolve_config_paths( | ||
| config_file_path: Path | None = None, | ||
| credentials_file_path: Path | None = None, | ||
| ) -> tuple[Path, Path]: | ||
| """Resolve the final config and credentials file paths. | ||
|
|
||
| Resolution order for each path: | ||
| 1. Explicit argument (if provided) | ||
| 2. Environment variable (AWS_CONFIG_FILE / AWS_SHARED_CREDENTIALS_FILE) | ||
| 3. Default (~/.aws/config / ~/.aws/credentials) | ||
|
|
||
| The ~ is expanded to the user's home directory. | ||
|
|
||
| :param config_file_path: Override path for config file. | ||
| :param credentials_file_path: Override path for credentials file. | ||
| :returns: Tuple of (resolved_config_path, resolved_credentials_path). | ||
| """ | ||
| config_path = ( | ||
| config_file_path | ||
| or Path(os.environ.get(_CONFIG_FILE_ENV_VAR, _DEFAULT_CONFIG_FILE)) | ||
| ).expanduser() | ||
|
|
||
| credentials_path = ( | ||
| credentials_file_path | ||
| or Path(os.environ.get(_CREDENTIALS_FILE_ENV_VAR, _DEFAULT_CREDENTIALS_FILE)) | ||
| ).expanduser() | ||
|
|
||
| return config_path, credentials_path | ||
|
|
||
|
|
||
| async def load_config( | ||
| config_file_path: Path | None = None, | ||
| credentials_file_path: Path | None = None, | ||
| ) -> MergedConfig: | ||
| """Load and merge AWS config and credentials files. | ||
|
|
||
| Parses both files, standardizes them, and returns a merged | ||
| MergedConfig ready for querying. | ||
|
|
||
| :param config_file_path: Override path for config file. | ||
| Defaults to AWS_CONFIG_FILE env var or ~/.aws/config. | ||
| :param credentials_file_path: Override path for credentials file. | ||
| Defaults to AWS_SHARED_CREDENTIALS_FILE env var or ~/.aws/credentials. | ||
| :returns: A MergedConfig with merged profiles from both files. | ||
| """ | ||
|
|
||
| config_path, credentials_path = _resolve_config_paths( | ||
| config_file_path, credentials_file_path | ||
| ) | ||
|
|
||
| raw_config = await parse_config_file(str(config_path)) | ||
| raw_credentials = await parse_config_file(str(credentials_path)) | ||
|
|
||
| std_config = standardize(raw_config, FileType.CONFIG) | ||
| std_credentials = standardize(raw_credentials, FileType.CREDENTIALS) | ||
|
|
||
| return MergedConfig(std_config, std_credentials) | ||
8 changes: 8 additions & 0 deletions
8
packages/smithy-aws-core/src/smithy_aws_core/config/exceptions.py
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,8 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Config related exceptions""" | ||
|
|
||
|
|
||
| class ConfigParseError(Exception): | ||
| """Raised when a config file cannot be parsed due to invalid syntax.""" |
Oops, something went wrong.
Oops, something went wrong.
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.