feat: add cz dump-config command to output effective configuration by bearomorphism · Pull Request #1986 · commitizen-tools/commitizen · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions commitizen/cli.py
2 changes: 2 additions & 0 deletions commitizen/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .changelog import Changelog
from .check import Check
from .commit import Commit
from .dump_config import DumpConfig
from .example import Example
from .info import Info
from .init import Init
Expand All @@ -14,6 +15,7 @@
"Changelog",
"Check",
"Commit",
"DumpConfig",
"Example",
"Info",
"Init",
Expand Down
55 changes: 55 additions & 0 deletions commitizen/commands/dump_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from __future__ import annotations

import json
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any

import tomlkit
import yaml

from commitizen import out

if TYPE_CHECKING:
from commitizen.config import BaseConfig


class DumpConfig:
"""Output the current commitizen configuration."""

def __init__(self, config: BaseConfig, arguments: dict[str, Any]) -> None:
self.config: BaseConfig = config
self.format: str = arguments.get("format", "toml")

def __call__(self) -> None:
settings = dict(self.config.settings)

if self.format == "toml":
filtered = _filter_none_values(settings)
doc = tomlkit.document()
tool_table = tomlkit.table()
cz_table = tomlkit.table()
for key, value in filtered.items():
cz_table.add(key, value)
tool_table.add("commitizen", cz_table)
doc.add("tool", tool_table)
out.write(tomlkit.dumps(doc))
elif self.format == "yaml":
out.write(
yaml.safe_dump(
{"tool": {"commitizen": settings}},
default_flow_style=False,
allow_unicode=True,
)
)
else:
out.write(
json.dumps({"tool": {"commitizen": settings}}, indent=2, default=str)
)


def _filter_none_values(settings: Mapping[str, Any]) -> dict[str, Any]:
return {
key: _filter_none_values(value) if isinstance(value, Mapping) else value
for key, value in settings.items()
if value is not None
}
41 changes: 41 additions & 0 deletions docs/commands/dump_config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# dump-config

Output the current commitizen configuration (defaults merged with any project overrides) so you can paste it directly into a configuration file as a starting point.

## Usage

```
cz dump-config [--format {toml,yaml,json}]
```

## Options

| Option | Description |
|---|---|
| `--format`, `-f` | Output format: `toml` (default), `yaml`, or `json` |

## Examples

Dump the current configuration as TOML (default):

```bash
cz dump-config
```

Dump as YAML:

```bash
cz dump-config --format yaml
```

Dump as JSON:

```bash
cz dump-config --format json
```

Use the TOML output as a starting point for `pyproject.toml`:

```bash
cz dump-config >> pyproject.toml
```
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ nav:
- info: "commands/info.md"
- ls: "commands/ls.md"
- schema: "commands/schema.md"
- dump-config: "commands/dump_config.md"
- version: "commands/version.md"
- Configuration:
- Configuration File: "config/configuration_file.md"
Expand Down Expand Up @@ -137,6 +138,7 @@ plugins:
- commands/info.md: Show information about the active configuration.
- commands/ls.md: List supported commit message choices for the active rules.
- commands/schema.md: Show the commit message schema for the active convention.
- commands/dump_config.md: Output the current commitizen configuration.
- commands/version.md: Show the installed or project version.
Configuration:
- config/configuration_file.md: Where configuration can live and how it is loaded.
Expand Down
1 change: 1 addition & 0 deletions tests/commands/test_common_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"changelog",
"check",
"commit",
"dump-config",
"example",
"info",
"init",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
usage: cz dump-config [-h] [--format {toml,yaml,json}]

Output the current commitizen configuration

options:
-h, --help show this help message and exit
--format, -f {toml,yaml,json}
Output format (default: toml).
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
usage: cz dump-config [-h] [--format {toml,yaml,json}]

Output the current commitizen configuration

options:
-h, --help show this help message and exit
--format, -f {toml,yaml,json}
Output format (default: toml).
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
usage: cz dump-config [-h] [--format {toml,yaml,json}]

Output the current commitizen configuration

options:
-h, --help show this help message and exit
--format, -f {toml,yaml,json}
Output format (default: toml).
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
usage: cz dump-config [-h] [--format {toml,yaml,json}]

Output the current commitizen configuration

options:
-h, --help show this help message and exit
--format, -f {toml,yaml,json}
Output format (default: toml).
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
usage: cz dump-config [-h] [--format {toml,yaml,json}]

Output the current commitizen configuration

options:
-h, --help show this help message and exit
--format, -f {toml,yaml,json}
Output format (default: toml).
63 changes: 63 additions & 0 deletions tests/commands/test_dump_config_command.py
Loading