Skip to content

Commit

Permalink
fix(cli): replace the "run" command by "fix" and "check" (andreoliwa#359
Browse files Browse the repository at this point in the history
)

* refactor: rename "apply" to "fix" everywhere
  • Loading branch information
andreoliwa authored Apr 25, 2021
1 parent 7a9578f commit 34d2499
Show file tree
Hide file tree
Showing 22 changed files with 201 additions and 170 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ repos:
hooks:
# Run nitpick also with tox, because local repos don't seem to work well with https://pre-commit.ci/
- id: local-nitpick
name: nitpick run
entry: poetry run nitpick run
name: "nitpick fix (modify files directly, local hook)"
entry: poetry run nitpick fix
language: system
always_run: true
pass_filenames: false
Expand Down
16 changes: 8 additions & 8 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# https://pre-commit.com/#creating-new-hooks
- id: nitpick
name: "Nitpick Flake8 plugin (check only)"
description: "Run as a flake8 plugin and only check configuration files, according to the Nitpick style"
name: "nitpick flake8 plugin (check files only)"
description: "Run as a flake8 plugin and only check configuration files (TOML/INI/JSON/etc.), according to the Nitpick style"
entry: flake8 --select=NIP
language: python
types: [python]
always_run: true
stages: [commit]

- id: nitpick-fix
name: "Nitpick CLI (fix files)"
description: "Fix configuration files automatically, according to the Nitpick style"
entry: nitpick run
name: "nitpick fix (modify files directly)"
description: "Fix configuration files (TOML/INI/JSON/etc.) directly, according to the Nitpick style"
entry: nitpick fix
language: python
stages: [commit]

- id: nitpick-check
name: "Nitpick CLI (check files only)"
description: "Only check configuration files, according to the Nitpick style"
entry: nitpick run --check
name: "nitpick check (check files only)"
description: "Only check configuration files (TOML/INI/JSON/etc.) and print the violations, according to the Nitpick style"
entry: nitpick check
language: python
stages: [commit]
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

### Features

- apply changes to pyproject.toml ([#287](https://github.com/andreoliwa/nitpick/issues/287)) ([4b79f81](https://github.com/andreoliwa/nitpick/commit/4b79f81f98430b749e7fa9ee5c192506d6ed5cf7))
- apply changes to setup.cfg ([#288](https://github.com/andreoliwa/nitpick/issues/288)) ([f878630](https://github.com/andreoliwa/nitpick/commit/f87863066642cdab112d3145c488c9a780e7c98d))
- fix pyproject.toml directly ([#287](https://github.com/andreoliwa/nitpick/issues/287)) ([4b79f81](https://github.com/andreoliwa/nitpick/commit/4b79f81f98430b749e7fa9ee5c192506d6ed5cf7))
- fix setup.cfg directly ([#288](https://github.com/andreoliwa/nitpick/issues/288)) ([f878630](https://github.com/andreoliwa/nitpick/commit/f87863066642cdab112d3145c488c9a780e7c98d))
- **cli:** add 'ls' command to list configured files ([cfc031b](https://github.com/andreoliwa/nitpick/commit/cfc031bdf30105dec9a8952bfb9657aec939b3b6))
- **cli:** add 'run' command to display violations ([a67bfa8](https://github.com/andreoliwa/nitpick/commit/a67bfa8bdaef2461853a237819cd35622c5935e9))
- **cli:** experimental CLI interface (alpha version) ([#255](https://github.com/andreoliwa/nitpick/issues/255)) ([c9ca5dc](https://github.com/andreoliwa/nitpick/commit/c9ca5dc3cc4586b459e2c58fb2e61d80aa3f1e5d))
Expand Down
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Command-line tool and [flake8](https://github.com/PyCQA/flake8) plugin to enforc

Useful if you maintain multiple projects and are tired of copying/pasting the same INI/TOML/YAML/JSON keys and values over and over, in all of them.

The tool now has an "apply" feature that modifies configuration files directly (pretty much like [black](https://github.com/psf/black) and [isort](https://github.com/PyCQA/isort) do with Python files).
The CLI now has a `nitpick fix` command that modifies configuration files directly (pretty much like [black](https://github.com/psf/black) and [isort](https://github.com/PyCQA/isort) do with Python files).
See the [CLI docs for more info](https://nitpick.rtfd.io/en/latest/cli.html).

Many more features are planned for the future, check [the roadmap](https://github.com/andreoliwa/nitpick/projects/1).
Expand Down Expand Up @@ -59,34 +59,34 @@ This style will assert that:
These are the file types currently handled by Nitpick.

- Some files are only being checked and have to be modified manually;
- Some files can already be fixed automatically (with the [`nitpick run`](#run) command);
- Some files can already be fixed automatically (with the [`nitpick fix`](#run) command);
- Others are still under construction; the ticket numbers are shown in the table (upvote the ticket with 👍🏻 if you would like to prioritise development).

### Implemented

<!-- auto-generated-start-implemented -->

| File type | Check | Fix ([`nitpick run`](#run)) |
| -------------------------------------------------------------------------------------------------- | ----- | --------------------------------------------------------------------- |
| [Any `.ini` file](https://nitpick.rtfd.io/en/latest/plugins.html#ini-files) |||
| [Any `.json` file](https://nitpick.rtfd.io/en/latest/plugins.html#json-files) || 🚧&nbsp;&nbsp;[#358](https://github.com/andreoliwa/nitpick/issues/358) |
| [Any `.toml` file](https://nitpick.rtfd.io/en/latest/plugins.html#toml-files) |||
| [Any text file](https://nitpick.rtfd.io/en/latest/plugins.html#text-files) |||
| [`.editorconfig`](https://nitpick.rtfd.io/en/latest/examples.html#example-editorconfig) |||
| [`.pre-commit-config.yaml`](https://nitpick.rtfd.io/en/latest/plugins.html#pre-commit-config-yaml) || 🚧&nbsp;&nbsp;[#282](https://github.com/andreoliwa/nitpick/issues/282) |
| [`.pylintrc`](https://nitpick.rtfd.io/en/latest/plugins.html#ini-files) |||
| [`package.json`](https://nitpick.rtfd.io/en/latest/examples.html#example-package-json) || 🚧&nbsp;&nbsp;[#358](https://github.com/andreoliwa/nitpick/issues/358) |
| [`pyproject.toml`](https://nitpick.rtfd.io/en/latest/plugins.html#toml-files) |||
| [`requirements.txt`](https://nitpick.rtfd.io/en/latest/plugins.html#text-files) |||
| [`setup.cfg`](https://nitpick.rtfd.io/en/latest/plugins.html#ini-files) |||
| File type | [`nitpick check`](#run) | [`nitpick fix`](#run) |
| -------------------------------------------------------------------------------------------------- | ----------------------- | --------------------------------------------------------------------- |
| [Any `.ini` file](https://nitpick.rtfd.io/en/latest/plugins.html#ini-files) | ||
| [Any `.json` file](https://nitpick.rtfd.io/en/latest/plugins.html#json-files) | | 🚧&nbsp;&nbsp;[#358](https://github.com/andreoliwa/nitpick/issues/358) |
| [Any `.toml` file](https://nitpick.rtfd.io/en/latest/plugins.html#toml-files) | ||
| [Any text file](https://nitpick.rtfd.io/en/latest/plugins.html#text-files) | ||
| [`.editorconfig`](https://nitpick.rtfd.io/en/latest/examples.html#example-editorconfig) | ||
| [`.pre-commit-config.yaml`](https://nitpick.rtfd.io/en/latest/plugins.html#pre-commit-config-yaml) | | 🚧&nbsp;&nbsp;[#282](https://github.com/andreoliwa/nitpick/issues/282) |
| [`.pylintrc`](https://nitpick.rtfd.io/en/latest/plugins.html#ini-files) | ||
| [`package.json`](https://nitpick.rtfd.io/en/latest/examples.html#example-package-json) | | 🚧&nbsp;&nbsp;[#358](https://github.com/andreoliwa/nitpick/issues/358) |
| [`pyproject.toml`](https://nitpick.rtfd.io/en/latest/plugins.html#toml-files) | ||
| [`requirements.txt`](https://nitpick.rtfd.io/en/latest/plugins.html#text-files) | ||
| [`setup.cfg`](https://nitpick.rtfd.io/en/latest/plugins.html#ini-files) | ||

<!-- auto-generated-end-implemented -->

### Planned

<!-- auto-generated-start-planned -->

| File type | Check | Fix ([`nitpick run`](#run)) |
| File type | [`nitpick check`](#run) | [`nitpick fix`](#run) |
| -------------------------- | --------------------------------------------------------------------- | --------------------------------------------------------------------- |
| Any `.md` (Markdown) file | 🚧&nbsp;&nbsp;[#280](https://github.com/andreoliwa/nitpick/issues/280) ||
| Any `.tf` (Terraform) file | 🚧&nbsp;&nbsp;[#318](https://github.com/andreoliwa/nitpick/issues/318) ||
Expand Down Expand Up @@ -134,11 +134,11 @@ Or install it with pip:

To fix and modify your files directly:

nitpick run
nitpick fix

To check for errors only:

nitpick run --check
nitpick check

Nitpick is also a `flake8` plugin, so you can run this on a project with at least one Python (`.py`) file:

Expand Down
45 changes: 31 additions & 14 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Command-line interface

The CLI is experimental, still under active development.

Nitpick_ has a CLI to apply changes to files automatically.
Nitpick_ has a CLI command to fix files automatically.

1. It doesn't work for all the plugins yet. Currently, it works for:

Expand All @@ -18,8 +18,8 @@ Nitpick_ has a CLI to apply changes to files automatically.

2. It tries to preserve the comments and the formatting of the original file.
3. Some changes still have to be done manually; Nitpick_ cannot guess how to make certain changes automatically.
4. On the CLI, the "apply mode" is the default: changes to files will be made automatically, when possible.
5. The flake8_ plugin only checks the files and doesn't make changes. This is the default for now; once the CLI becomes more stable, the "apply mode" will become the default.
4. Run ``nitpick fix`` to modify files directly, or ``nitpick check`` to only display the violations.
5. The flake8_ plugin only checks the files and doesn't make changes. This is the default for now; once the CLI becomes more stable, the "fix mode" will become the default.
6. The output format aims to follow `pycodestyle (pep8) default output format <https://github.com/PyCQA/pycodestyle/blob/master/pycodestyle.py#L108>`_.

If you use Git, you can review the files before committing.
Expand Down Expand Up @@ -48,14 +48,15 @@ Main options
--help Show this message and exit.
Commands:
init Create a configuration file if it doesn't exist already.
ls List of files configured in the Nitpick style.
run Apply suggestions to configuration files.
check Don't modify files, just print the differences.
fix Fix files, modifying them directly.
init Create a configuration file if it doesn't exist already.
ls List of files configured in the Nitpick style.
.. _cli_cmd_run:
.. _cli_cmd_fix:

``run``: Apply style to files
-----------------------------
``fix``: Modify files directly
------------------------------

At the end of execution, this command displays:

Expand All @@ -65,17 +66,33 @@ At the end of execution, this command displays:

.. code-block::
Usage: nitpick run [OPTIONS] [FILES]...
Usage: nitpick fix [OPTIONS] [FILES]...
Apply suggestions to configuration files.
Fix files, modifying them directly.
You can use partial and multiple file names in the FILES argument.
Options:
-c, --check Don't modify the configuration files, just print the
difference. Return code 0 means nothing would change. Return
code 1 means some files would be modified.
-v, --verbose Increase logging verbosity (-v = INFO, -vv = DEBUG)
--help Show this message and exit.
.. _cli_cmd_check:

``check``: Don't modify, just print the differences
---------------------------------------------------


.. code-block::
Usage: nitpick check [OPTIONS] [FILES]...
Don't modify files, just print the differences.
Return code 0 means nothing would change. Return code 1 means some files
would be modified. You can use partial and multiple file names in the
FILES argument.
Options:
-v, --verbose Increase logging verbosity (-v = INFO, -vv = DEBUG)
--help Show this message and exit.
Expand Down
7 changes: 4 additions & 3 deletions docs/generate_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@
CLI_MAPPING = [
("", "Main options", ""),
(
"run",
"Apply style to files",
"fix",
"Modify files directly",
"""
At the end of execution, this command displays:
- the number of fixed violations;
- the number of violations that have to be changed manually.
""",
),
("check", "Don't modify, just print the differences", ""),
("ls", "List configures files", ""),
("init", "Initialise a configuration file", ""),
]
Expand Down Expand Up @@ -346,7 +347,7 @@ def write_readme(file_types: Set[FileType], divider: str) -> int:
prettier will try to reformat the tables; to avoid that, README.md was added to .prettierignore.
"""
rows: List[Tuple[str, ...]] = [("File type", "Check", "Fix ([`nitpick run`](#run))")]
rows: List[Tuple[str, ...]] = [("File type", "[`nitpick check`](#run)", "[`nitpick fix`](#run)")]
max_length = [len(h) for h in rows[0]]
for file_type in sorted(file_types):
if max_length[0] < len(file_type.text_with_url):
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Command-line tool and flake8_ plugin to enforce the same settings across multipl

Useful if you maintain multiple projects and are tired of copying/pasting the same INI/TOML/YAML/JSON keys and values over and over, in all of them.

The tool now has an "apply" feature that modifies configuration files directly (pretty much like black_ and isort_ do with Python files).
The CLI now has a ``nitpick fix`` command that modifies configuration files directly (pretty much like black_ and isort_ do with Python files).
See :ref:`cli` for more info.

Many more features are planned for the future, check `the roadmap <https://github.com/andreoliwa/nitpick/projects/1>`_.
Expand Down
12 changes: 6 additions & 6 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ Run

To fix and modify your files directly::

nitpick run
nitpick fix

To check for errors only::

nitpick run --check
nitpick check

Nitpick is also a flake8_ plugin, so you can run this on a project with at least one Python (``.py``) file::

Expand Down Expand Up @@ -78,13 +78,13 @@ To start checking all your code against the default rules:
pre-commit run --all-files
Apply changes to files
----------------------
Modify files directly
---------------------

Nitpick_ includes a CLI to apply your style on the configuration files:
Nitpick_ includes a CLI to apply your style and modify the configuration files directly:

.. code-block:: shell
nitpick run
nitpick fix
Read more details here: :ref:`cli`.
54 changes: 34 additions & 20 deletions src/nitpick/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
from nitpick.generic import relative_to_current_dir
from nitpick.violations import Reporter

VERBOSE_OPTION = click.option(
"--verbose", "-v", count=True, default=False, help="Increase logging verbosity (-v = INFO, -vv = DEBUG)"
)
FILES_ARGUMENT = click.argument("files", nargs=-1)


@click.group()
@click.option(
Expand Down Expand Up @@ -57,24 +62,8 @@ def get_nitpick(context: click.Context) -> Nitpick:
return Nitpick.singleton().init(project_root, offline)


@nitpick_cli.command()
@click.option(
"--check",
"-c",
"check_only",
is_flag=True,
default=False,
help="Don't modify the configuration files, just print the difference."
" Return code 0 means nothing would change. Return code 1 means some files would be modified.",
)
@click.option("--verbose", "-v", count=True, default=False, help="Increase logging verbosity (-v = INFO, -vv = DEBUG)")
@click.pass_context
@click.argument("files", nargs=-1)
def run(context, check_only, verbose, files):
"""Apply suggestions to configuration files.
You can use partial and multiple file names in the FILES argument.
"""
def common_fix_or_check(context, verbose: int, files, check_only: bool) -> None:
"""Common CLI code for both fix and check commands."""
if verbose:
level = logging.INFO if verbose == 1 else logging.DEBUG

Expand All @@ -87,7 +76,7 @@ def run(context, check_only, verbose, files):

nit = get_nitpick(context)
try:
for fuss in nit.run(*files, apply=not check_only):
for fuss in nit.run(*files, fix=not check_only):
nit.echo(fuss.pretty)
except QuitComplainingError as err:
for fuss in err.violations:
Expand All @@ -101,7 +90,32 @@ def run(context, check_only, verbose, files):

@nitpick_cli.command()
@click.pass_context
@click.argument("files", nargs=-1)
@VERBOSE_OPTION
@FILES_ARGUMENT
def fix(context, verbose, files):
"""Fix files, modifying them directly.
You can use partial and multiple file names in the FILES argument.
"""
common_fix_or_check(context, verbose, files, False)


@nitpick_cli.command()
@click.pass_context
@VERBOSE_OPTION
@FILES_ARGUMENT
def check(context, verbose, files):
"""Don't modify files, just print the differences.
Return code 0 means nothing would change. Return code 1 means some files would be modified.
You can use partial and multiple file names in the FILES argument.
"""
common_fix_or_check(context, verbose, files, True)


@nitpick_cli.command()
@click.pass_context
@FILES_ARGUMENT
def ls(context, files): # pylint: disable=invalid-name
"""List of files configured in the Nitpick style.
Expand Down
Loading

0 comments on commit 34d2499

Please sign in to comment.