Users should be able to pass CLI data arguments and use a `--data-file` at the same time. Let's just take inspiration from the [helm values file](https://helm.sh/docs/chart_template_guide/values_files/) and give highest priority to data values passed at the command line.
This change addresses a usability issue troubling users when they run 'copier update'. Previously, each time 'copier update' was run, the user had to review all responses given in the answers file. This was a repetitive and laborious process, particularly for users dealing with extensive templates.
This PR introduces a new flag --skip-answered to improve the user experience and reduce friction during updates. The new flag allows users to bypass the review of questions already answered in the past. It automatically pulls responses from the answers file and presents only the newly added template questions to the user for their review.
`skip-answered` will skip all answered questions and ask user input only for new inputs if they are present.
Fixes#1178
Co-authored-by: Jairo Llopis <973709+yajo@users.noreply.github.com>
This new command allows to reapply a template, keeping old answers but discarding subproject evolution.
It is useful when there are bugs replaying an old version of the template, or when the subproject has drifted too much from the template and you need to reset it.
BREAKING CHANGE: All CLI calls to Copier must now include the subcommand as the 1st argument. For example, `copier` must become now `copier update`; also `copier ./tpl ./dst` must become `copier copy ./tpl ./dst`.
BREAKING CHANGE: All flags must go after the subcommand now. For example, `copier -r HEAD update ./dst` must now become `copier update -r HEAD ./dst` or `copier update ./dst -r HEAD`.
BREAKING CHANGE: Automatic mode removed. Since now subcommands are required, the automatic mode is removed.
BREAKING CHANGE: Deprecated `copier.copy` function is removed. Use `copier.run_copy`, `copier.run_update` or `copier.run_recopy` explicitly as needed.
Fix https://github.com/copier-org/copier/issues/1081
Close https://github.com/copier-org/copier/issues/1082
Use the `decorator` library for decorating `main()` in subcommands. This way, the method signature is properly inspected and explained by plumbum.
Fix https://github.com/copier-org/copier/issues/1090.
* refactor(tests): add type hints and clean up
* fix: import `Protocol` from `typing-extensions` when using Python 3.7
* fix: import `Literal` from `typing-extensions` when using Python 3.7
* refactor(tests): create template and subproject directory using `tmp_path_factory` fixture
* refactor(tests): use OS-agnostic file path separator
* refactor(tests): use `/` separator for `skip_if_exists` paths
* style: fix formatting error
* refactor(tests): expect POSIX path for local repo URL
* use a new toolkit for user prompting: questionary (patched).
* multiline questions.
* conditional questions.
* new toolkit for ui tests: pexpect.
* fix lots of tests.
* fix windows builds with newer poetry-dynamic-versioning.
* linters and mypy are now tests, to have faster ci.
* update deprecated ci commands.
* removed dependencies from old times.
* Remove toml 0.5+ syntax (dotted keys)
* Use powershell where syntax is compatible
* Change skip to xfail
* xfail pexpect tests on windows. I'm tired of trying to make it work
* more docs
* possibly something more.
This is a switch attribute, not a flag.
Without this patch, using this feature yields this error:
```
➤ copier -fa .altered-copier-answers.yml update
Traceback (most recent call last):
File "/home/yajo/.local/bin/copier", line 8, in <module>
sys.exit(CopierApp.run())
File "/home/yajo/.local/pipx/venvs/copier/lib64/python3.7/site-packages/plumbum/cli/application.py", line 577, in run
inst, retcode = subapp.run(argv, exit=False)
File "/home/yajo/.local/pipx/venvs/copier/lib64/python3.7/site-packages/plumbum/cli/application.py", line 572, in run
retcode = inst.main(*tailargs)
File "/home/yajo/.local/pipx/venvs/copier/lib64/python3.7/site-packages/copier/cli.py", line 17, in _wrapper
return method(*args, **kwargs)
File "/home/yajo/.local/pipx/venvs/copier/lib64/python3.7/site-packages/copier/cli.py", line 172, in main
dst_path=destination_path, only_diff=self.only_diff,
File "/home/yajo/.local/pipx/venvs/copier/lib64/python3.7/site-packages/copier/cli.py", line 111, in _copy
**kwargs,
File "/home/yajo/.local/pipx/venvs/copier/lib64/python3.7/site-packages/copier/main.py", line 123, in copy
conf = make_config(**locals())
File "/home/yajo/.local/pipx/venvs/copier/lib64/python3.7/site-packages/copier/config/factory.py", line 59, in make_config
answers_data.update(load_answersfile_data(dst_path, answers_file))
File "/home/yajo/.local/pipx/venvs/copier/lib64/python3.7/site-packages/copier/config/user_data.py", line 90, in load_answersfile_data
with open(Path(dst_path) / (answers_file or ".copier-answers.yml")) as fd:
File "/usr/lib64/python3.7/pathlib.py", line 920, in __truediv__
return self._make_child((key,))
File "/usr/lib64/python3.7/pathlib.py", line 699, in _make_child
drv, root, parts = self._parse_args(args)
File "/usr/lib64/python3.7/pathlib.py", line 653, in _parse_args
a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not bool
```
@Tecnativa TT20357
- Add ignore comment on `BaseModel` instances that are correctly following what `pydantic` wants, to let flake8-bugbear be happy.
- Remove unused import.
This is a totally breaking PR. It had to come sooner or later...
Before this patch, copier had some ugly problems:
- If a file was called `--exclude`, you couldn't add it to `--extra-paths` due to using `nargs='*'` in an option. It is a weird thing that `ArgumentParser` allows to do, but can potentially lead to unfixable problems.
- Argument parsing was a little bit handycraft work.
Meet [Plumbum](https://plumbum.readthedocs.io/), the swiss army knife for CLI packages:
- CLI has been refactored as a plumbum application.
- Tests for CLI parsing removed, since they were incompatible and we are using a well-tested CLI parsing library now after all.
- The application now includes 2 subcommands: `copy` and `update`.
- If called without args, or with 1, it will use `update`.
- If called with 2 args, it will use `copy` (this preserves old usage unchanged, unless you are copying from a template called `copy` or `update`; in such corner case, just call it as `copier copy copy destination` instead).
- `copier --help` and `copier --help-all` are more useful now.
And you might be wondering... Adding such a big dependency just for that? Well, actually plumbum supports more than just CLI applications: supports sane execution of local & remote commands, sane output coloring, sane user prompting... Possibly it will make Copier go to the next level as we keep on using it more. Just take a look at its docs! You'll love it 😉
Of course, all that new CLI had to be reflected in API changes:
- `src_path` is now optional (`None` by default). In such case, copier will try to guess the `src_path` from the `.copier-answers.yml` file in the copy, or fail otherwise.
- `dst_path` is also now optional (`.` by default), meaning that without a `src_path`, you'll be trying to copy anything else to current `$PWD`.
- The builtin `_log` variable, which contains all necessary data for copy updates, now has also a `_src_path` key that points to the original source. This only happens when it was copied from a git repo or from an absolute path in the local disk, as relative paths are not easily reproducible in next updates (they depend on the user's CWD when running copier).
- A new `original_src_path` variable goes around, indicating the git repo or abs system path passed to `src_path` on the initial call. This is just to be stored in `_log` as explained.
```bash
copier gh:example/template destination
cd destination
echo my changes >> anywhere
git commit -m changing .
copier # Equals to `copier update .`
git commit -m 'copier update' .
```
🎉 You're updated!
This is an importan step to complete #88.
@Tecnativa TT20357