-
-
Notifications
You must be signed in to change notification settings - Fork 247
feat(settings): add user settings support with defaults values and trusts
#1940
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 all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """User settings models and helper functions.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import warnings | ||
| from os.path import expanduser | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import yaml | ||
| from platformdirs import user_config_path | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| from .errors import MissingSettingsWarning | ||
|
|
||
| ENV_VAR = "COPIER_SETTINGS_PATH" | ||
|
|
||
|
|
||
| class Settings(BaseModel): | ||
| """User settings model.""" | ||
|
|
||
| defaults: dict[str, Any] = Field( | ||
| default_factory=dict, description="Default values for questions" | ||
| ) | ||
| trust: set[str] = Field( | ||
| default_factory=set, description="List of trusted repositories or prefixes" | ||
| ) | ||
|
|
||
| @classmethod | ||
| def from_file(cls, settings_path: Path | None = None) -> Settings: | ||
| """Load settings from a file.""" | ||
| env_path = os.getenv(ENV_VAR) | ||
| if settings_path is None: | ||
| if env_path: | ||
| settings_path = Path(env_path) | ||
| else: | ||
| settings_path = user_config_path("copier") / "settings.yml" | ||
| if settings_path.is_file(): | ||
| data = yaml.safe_load(settings_path.read_text()) | ||
| return cls.model_validate(data) | ||
| elif env_path: | ||
| warnings.warn( | ||
| f"Settings file not found at {env_path}", MissingSettingsWarning | ||
| ) | ||
| return cls() | ||
noirbizarre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def is_trusted(self, repository: str) -> bool: | ||
| """Check if a repository is trusted.""" | ||
| return any( | ||
| repository.startswith(self.normalize(trusted)) | ||
| if trusted.endswith("/") | ||
| else repository == self.normalize(trusted) | ||
| for trusted in self.trust | ||
| ) | ||
|
|
||
| def normalize(self, url: str) -> str: | ||
| """Normalize an URL using user settings.""" | ||
| if url.startswith("~"): # Only expand on str to avoid messing with URLs | ||
| url = expanduser(url) | ||
| return url | ||
sisp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ::: copier.settings |
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,56 @@ | ||
| # Settings | ||
|
|
||
| Copier settings are stored in `<CONFIG_ROOT>/settings.yml` where `<CONFIG_ROOT>` is the | ||
noirbizarre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| standard configuration directory for your platform: | ||
|
|
||
| - `$XDG_CONFIG_HOME/copier` (`~/.config/copier ` in most cases) on Linux as defined by | ||
| [XDG Base Directory Specifications](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) | ||
| - `~/Library/Application Support/copier` on macOS as defined by | ||
| [Apple File System Basics](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html) | ||
| - `%USERPROFILE%\AppData\Local\copier` on Windows as defined in | ||
| [Known folders](https://docs.microsoft.com/en-us/windows/win32/shell/known-folders) | ||
|
|
||
| This location can be overridden by setting the `COPIER_SETTINGS_PATH` environment | ||
| variable. | ||
|
|
||
| ## User defaults | ||
|
|
||
| Users may define some reusable default variables in the `defaults` section of the | ||
| configuration file. | ||
|
|
||
| ```yaml title="<CONFIG_ROOT>/settings.yml" | ||
| defaults: | ||
| user_name: "John Doe" | ||
| user_email: john.doe@acme.com | ||
| ``` | ||
|
|
||
| This user data will replace the default value of fields of the same name. | ||
|
|
||
| ### Well-known variables | ||
|
|
||
| To ensure templates efficiently reuse user-defined variables, we invite template authors | ||
| to use the following well-known variables: | ||
|
|
||
| | Variable name | Type | Description | | ||
| | ------------- | ----- | ---------------------- | | ||
| | `user_name` | `str` | User's full name | | ||
| | `user_email` | `str` | User's email address | | ||
| | `github_user` | `str` | User's GitHub username | | ||
| | `gitlab_user` | `str` | User's GitLab username | | ||
|
|
||
| ## Trusted locations | ||
|
|
||
| Users may define trusted locations in the `trust` setting. It should be a list of Copier | ||
| template repositories, or repositories prefix. | ||
|
|
||
| ```yaml | ||
| trust: | ||
| - https://github.com/your_account/your_template.git | ||
| - https://github.com/your_account/ | ||
| - ~/templates/ | ||
| ``` | ||
|
|
||
| !!! warning "Security considerations" | ||
|
|
||
| Locations ending with `/` will be matched as prefixes, trusting all templates starting with that path. | ||
| Locations not ending with `/` will be matched exactly. | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.