26 Commits

Author SHA1 Message Date
Sigurd Spieckermann
ef5ea4b212 refactor: rename internal modules with a _ prefix 2025-04-22 13:52:51 +02:00
Sigurd Spieckermann
ede686561d test: use helper function to load answers file data 2025-03-07 12:21:36 +01:00
Iwasa Kazmi
8b0ff719c7
fix(cli): auto-detect encoding when reading unicode-encoded file specified with --data-file 2025-03-02 13:16:52 +00:00
Sigurd Spieckermann
0258635da7 fix: support Git config without user identity 2024-07-30 13:40:17 +02:00
danieleades
0c6b0b96fe
build(typing): enable 'strict' mypy linting (#1527)
---------

Co-authored-by: Timothée Mazzucotelli <dev@pawamoy.fr>
2024-03-23 12:58:20 +01:00
danieleades
878c4d4f40
style: add 'flake-future-annotations' lint group (#1506)
* add ruff 'flake8-future-annotations' (FA) lint group
2024-02-22 06:19:17 +00:00
mspiegel31
61cbe798a7
feat: allow overriding data file with CLI arguments (#1332)
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.
2023-09-29 12:28:37 +01:00
mspiegel31
a674e64a42
feat(cli): read answers from yaml file (#1325)
Co-authored-by: Sigurd Spieckermann <2206639+sisp@users.noreply.github.com>
2023-09-20 07:12:04 +02:00
Oleksii Siechko
8619fc8a68
feat: add --skip-answered flag to avoid repeating recorded answers
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>
2023-09-03 21:15:49 +00:00
Jairo Llopis
6996b9cc7a feat: add recopy command and function
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
2023-05-17 11:56:13 +01:00
Jairo Llopis
0a93f05d76 fix(cli): display subcommand args meaning
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.
2023-05-17 11:56:13 +01:00
Sigurd Spieckermann
b352841046 test: add return type hints 2023-03-31 06:27:36 +01:00
Sigurd Spieckermann
5029b6e7a9
test: add type hints and clean up (#985)
* 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
2023-03-21 16:31:11 +00:00
Sigurd Spieckermann
ba514b424f fix: parse CLI data using question's answer parser 2023-02-27 18:48:58 +01:00
Sigurd Spieckermann
c9e9c100ec fix(cli): use --conflict flag only in copier update subcommand 2023-02-21 21:21:01 +00:00
Nils de Bruin
2d4c472e8a
feat: let answers file exist in a subdirectory
See discussion: https://github.com/copier-org/copier/discussions/859

Co-authored-by: Jairo Llopis <yajo.sk8@gmail.com>
2023-02-20 20:56:45 +00:00
Thierry Gaugry
468b9ff78c
fix: --skip option was ignored (#966)
* Fix missing skip_if_exists param

* Add tests for skip cli flag

---------

Co-authored-by: Thierry Gaugry <thierry.gaugry@ariadnext.com>
2023-02-17 19:38:50 +00:00
Timothée Mazzucotelli
534a888a0a
feat: Add __main__ module allowing to run python -m copier (#384)
* feat: Add `__main__` module allowing to run `python -m copier`

* tests: Add test for `python -m copier`
2021-04-06 08:46:06 +01:00
Jairo Llopis
1482de4972
wip 2021-03-05 17:33:11 +00:00
Jairo Llopis
cda93273c1
Enhanced user input (#260)
* 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.
2020-10-12 08:56:46 +01:00
Jairo Llopis
823f3a1935 Remove dst fixture
This is redundant with pytest's native `tmp_path` fixture, so we use that one instead now.
2020-06-23 07:58:03 +00:00
Jairo Llopis
58210e6ed6
Fix --answers-file
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
2020-03-25 10:34:58 +00:00
Jairo Llopis
1d8a2a47c3
Fix copier --help
A bug introduced in #121 made copier --help stop working.

Test added.
2020-02-24 11:24:00 +00:00
Jairo Llopis
5b48f352a4 Fix new checks
- Add ignore comment on `BaseModel` instances that are correctly following what `pydantic` wants, to let flake8-bugbear be happy.
- Remove unused import.
2019-12-10 11:52:05 +00:00
Jairo Llopis
899f5d6a66
Basic support for automatic copy updates
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
2019-12-05 13:51:55 +00:00
Ben Felder
79a1d04020 Refactored parse_args for better testability.
Added test_cli.py.

Removed unused dynamic import from cli.py.

Added tests for cli flags.

Added test for cli.run.

Improved test_good_cli_run.

Refactored parser generation for better testability.

test_cli_args replacing test_cli_flags.

Parametrized test_invalid_cli_args.

Added test_make_parser_bad_config.
2019-09-22 20:47:36 +02:00