From 8246eb80da0453299274e133b27407917643cbd4 Mon Sep 17 00:00:00 2001 From: Moe bot Date: Tue, 20 Dec 2022 14:16:50 -0800 Subject: [PATCH] feat: add `-d` option to `rm` command to delete files Use the `-d` or `--delete` option to remove the underlying files when removing an item from the library. --- docs/cli.rst | 4 +++- moe/remove/rm_cli.py | 13 +++++++++++++ tests/remove/test_rm_cli.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/docs/cli.rst b/docs/cli.rst index 6610533..5b870f0 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -129,7 +129,7 @@ Removes music from your library. .. code-block:: bash - moe remove [-h] [-a | -e] query + moe remove [-h] [-a | -e] [-d] query Positional Arguments -------------------- @@ -144,3 +144,5 @@ Optional Arguments Query for matching albums instead of tracks. ``-e, --extra`` Query for matching extras instead of tracks. +``-d, --delete`` + Delete the items from the filesystem. diff --git a/moe/remove/rm_cli.py b/moe/remove/rm_cli.py index 080266f..ae64539 100644 --- a/moe/remove/rm_cli.py +++ b/moe/remove/rm_cli.py @@ -6,6 +6,7 @@ import argparse import logging +import shutil from sqlalchemy.orm.session import Session @@ -29,6 +30,12 @@ def add_command(cmd_parsers: argparse._SubParsersAction): help="remove music from the library", parents=[query_parser], ) + rm_parser.add_argument( + "-d", + "--delete", + action="store_true", + help="delete the items from the filesystem", + ) rm_parser.set_defaults(func=_parse_args) @@ -46,3 +53,9 @@ def _parse_args(session: Session, args: argparse.Namespace): for item in items: moe_rm.remove_item(session, item) + + if args.delete: + if item.path.is_file(): + item.path.unlink() + else: + shutil.rmtree(item.path) diff --git a/tests/remove/test_rm_cli.py b/tests/remove/test_rm_cli.py index 44dbda9..3e932bf 100644 --- a/tests/remove/test_rm_cli.py +++ b/tests/remove/test_rm_cli.py @@ -33,7 +33,7 @@ def mock_query() -> Iterator[FunctionType]: @pytest.fixture def _tmp_rm_config(tmp_config): """A temporary config for the edit plugin with the cli.""" - tmp_config('default_plugins = ["cli", "remove"]') + tmp_config('default_plugins = ["cli", "remove", "write"]') @pytest.mark.usefixtures("_tmp_rm_config") @@ -85,6 +85,36 @@ def test_multiple_items(self, mock_query, mock_rm): mock_rm.assert_any_call(ANY, track) assert mock_rm.call_count == 2 + def test_delete_album(self, mock_query, mock_rm): + """Delete albums if `-d` passed.""" + cli_args = ["remove", "--delete", "*"] + album = album_factory(exists=True) + mock_query.return_value = [album] + + moe.cli.main(cli_args) + + assert not album.path.exists() + + def test_delete_extra(self, mock_query, mock_rm): + """Delete extras if `-d` passed.""" + cli_args = ["remove", "-d", "*"] + extra = extra_factory(exists=True) + mock_query.return_value = [extra] + + moe.cli.main(cli_args) + + assert not extra.path.exists() + + def test_delete_track(self, mock_query, mock_rm): + """Delete tracks if `-d` passed.""" + cli_args = ["remove", "-d", "*"] + track = track_factory(exists=True) + mock_query.return_value = [track] + + moe.cli.main(cli_args) + + assert not track.path.exists() + class TestPluginRegistration: """Test the `plugin_registration` hook implementation."""