Skip to content

Commit

Permalink
Added sample-config feature
Browse files Browse the repository at this point in the history
- Initial implementation
  • Loading branch information
coordt committed Jan 14, 2024
1 parent 2aa2b36 commit 3d0f67d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
81 changes: 80 additions & 1 deletion bumpversion/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""bump-my-version Command line interface."""
from pathlib import Path
from typing import List, Optional

import rich_click as click
Expand All @@ -11,7 +12,7 @@
from bumpversion.config.files import find_config_file
from bumpversion.files import ConfiguredFile, modify_files
from bumpversion.show import do_show, log_list
from bumpversion.ui import get_indented_logger, print_warning, setup_logging
from bumpversion.ui import get_indented_logger, print_info, print_warning, setup_logging
from bumpversion.utils import get_context, get_overrides

logger = get_indented_logger(__name__)
Expand Down Expand Up @@ -516,3 +517,81 @@ def replace(
ctx = get_context(config, version, next_version)

modify_files(configured_files, version, next_version, ctx, dry_run)


@cli.command()
@click.option(
"--prompt/--no-prompt",
default=True,
help="Ask the user questions about the configuration.",
)
@click.option(
"--destination",
default="stdout",
help="Where to write the sample configuration.",
type=click.Choice(["stdout", ".bumpversion.toml", "pyproject.toml"]),
)
def sample_config(prompt: bool, destination: str) -> None:
"""Print a sample configuration file."""
import questionary
from tomlkit import document, dumps, parse

Check warning on line 537 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L536-L537

Added lines #L536 - L537 were not covered by tests

from bumpversion.config import DEFAULTS

Check warning on line 539 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L539

Added line #L539 was not covered by tests

config = DEFAULTS.copy()

Check warning on line 541 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L541

Added line #L541 was not covered by tests
if prompt:
destination = questionary.select(

Check warning on line 543 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L543

Added line #L543 was not covered by tests
"Destination", choices=["stdout", ".bumpversion.toml", "pyproject.toml"], default=destination
).ask()
destination_path = None

Check warning on line 546 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L546

Added line #L546 was not covered by tests

if destination != "stdout":
destination_path = Path(destination)
destination_path.touch(exist_ok=True)
destination_config = parse(destination_path.read_text())
existing_config = destination_config.get("tool", {}).get("bumpversion", {})

Check warning on line 552 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L549-L552

Added lines #L549 - L552 were not covered by tests
if existing_config:
logger.info("Found existing configuration in %s. Loading as defaults.", destination_path)
config.update(existing_config)

Check warning on line 555 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L554-L555

Added lines #L554 - L555 were not covered by tests
else:
destination_config = document()
destination_config.update({"tool": {"bumpversion": {}}})

Check warning on line 558 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L557-L558

Added lines #L557 - L558 were not covered by tests

config["current_version"] = config["current_version"] or destination_config.get("project", {}).get(

Check warning on line 560 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L560

Added line #L560 was not covered by tests
"version", "0.1.0"
)
del config["scm_info"]
del config["parts"]
del config["files"]

Check warning on line 565 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L563-L565

Added lines #L563 - L565 were not covered by tests

if prompt:
allow_dirty_default = "(Y/n)" if config["allow_dirty"] else "(y/N)"
answers = questionary.form(

Check warning on line 569 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L568-L569

Added lines #L568 - L569 were not covered by tests
current_version=questionary.text("What is the current version?", default=config["current_version"]),
commit=questionary.confirm(
"Commit changes made when bumping to version control?", default=config["commit"]
),
allow_dirty=questionary.confirm(
"Allow dirty working directory when bumping?",
default=config["allow_dirty"],
instruction=(
"If you are also creating or modifying other files (e.g. a CHANGELOG), say Yes. "
f"{allow_dirty_default} "
),
),
tag=questionary.confirm("Tag changes made when bumping in version control?", default=config["tag"]),
commit_args=questionary.text(
"Any extra arguments to pass to the commit command?",
default=config["commit_args"] or "",
instruction="For example, `--no-verify` is useful if you have a pre-commit hook. ",
),
).ask()
config.update(answers)

Check warning on line 589 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L589

Added line #L589 was not covered by tests

for key, val in config.items():
destination_config["tool"]["bumpversion"][key] = val

Check warning on line 592 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L592

Added line #L592 was not covered by tests

if destination_path:
destination_path.write_text(dumps(destination_config))

Check warning on line 595 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L595

Added line #L595 was not covered by tests
else:
print_info(dumps(destination_config))

Check warning on line 597 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L597

Added line #L597 was not covered by tests
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies = [
"click",
"pydantic>=2.0.0",
"pydantic-settings",
"questionary",
"rich-click",
"rich",
"tomlkit",
Expand Down

0 comments on commit 3d0f67d

Please sign in to comment.