Skip to content

Commit

Permalink
Refactored _git.remove_files
Browse files Browse the repository at this point in the history
    Code making decision to remove prompt or keep news fragments
    is being kept in new intermediate function _remover.remove_news_fragment_files
  • Loading branch information
fizyk committed Dec 1, 2022
1 parent bf049f7 commit 40624ac
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
28 changes: 3 additions & 25 deletions src/towncrier/_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,9 @@

from subprocess import STDOUT, call, check_output

import click


def remove_files(
fragment_filenames: list[str], answer_yes: bool, answer_keep: bool
) -> None:
if not fragment_filenames:
return

try:
if answer_keep:
click.echo("Keeping the following files:")
# Not proceeding with the removal of the files.
return

if answer_yes:
click.echo("Removing the following files:")
else:
click.echo("I want to remove the following files:")
finally:
# Will always be printed, even for answer_keep to help with possible troubleshooting
for filename in fragment_filenames:
click.echo(filename)

if answer_yes or click.confirm("Is it okay if I remove those files?", default=True):

def remove_files(fragment_filenames: list[str]) -> None:
if fragment_filenames:
call(["git", "rm", "--quiet"] + fragment_filenames)


Expand Down
30 changes: 30 additions & 0 deletions src/towncrier/_remover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) Amber Brown, 2015
# See LICENSE for details.

from __future__ import annotations

import click

from towncrier._git import remove_files


def remove_news_fragment_files(
fragment_filenames: list[str], answer_yes: bool, answer_keep: bool
) -> None:
try:
if answer_keep:
click.echo("Keeping the following files:")
# Not proceeding with the removal of the files.
return

if answer_yes:
click.echo("Removing the following files:")
else:
click.echo("I want to remove the following files:")
finally:
# Will always be printed, even for answer_keep to help with possible troubleshooting
for filename in fragment_filenames:
click.echo(filename)

if answer_yes or click.confirm("Is it okay if I remove those files?", default=True):
remove_files(fragment_filenames)
6 changes: 4 additions & 2 deletions src/towncrier/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

from click import Context, Option

from towncrier._remover import remove_news_fragment_files

from ._builder import find_fragments, render_fragments, split_fragments
from ._git import remove_files, stage_newsfile
from ._git import stage_newsfile
from ._project import get_project_name, get_version
from ._settings import ConfigError, config_option_help, load_config_from_options
from ._writer import append_to_newsfile
Expand Down Expand Up @@ -263,7 +265,7 @@ def __main(
stage_newsfile(base_directory, news_file)

click.echo("Removing news fragments...", err=to_err)
remove_files(fragment_filenames, answer_yes, answer_keep)
remove_news_fragment_files(fragment_filenames, answer_yes, answer_keep)

click.echo("Done!", err=to_err)

Expand Down

0 comments on commit 40624ac

Please sign in to comment.