From 7f5a01ffe5b4c4feb09aea13fe7cddc6c89a4ab2 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 12 Nov 2021 07:12:23 +0000 Subject: [PATCH] split command execution into App::MPD::Feeder::Command avoids having dead code in RAM when the main loop is operating --- bin/mpd-feeder | 58 ++----------------------- lib/App/MPD/Feeder/Command.pm | 79 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 55 deletions(-) create mode 100644 lib/App/MPD/Feeder/Command.pm diff --git a/bin/mpd-feeder b/bin/mpd-feeder index bc73a73..ffb7994 100755 --- a/bin/mpd-feeder +++ b/bin/mpd-feeder @@ -11,62 +11,10 @@ use Log::Any::Adapter Stderr => log_level => 'error'; my $feeder = App::MPD::Feeder->new(); if (@ARGV) { - my $cmd = shift @ARGV; + require App::MPD::Feeder::Command; + bless $feeder, 'App::MPD::Feeder::Command'; - if ($cmd eq 'dump-config') { - die "dump-config command accepts no arguments\n" if @ARGV; - - $feeder->opt->dump; - exit; - } - - if ( $cmd eq 'add-unwanted-artist' ) { - die "Missing command arguments\n" unless @ARGV; - $feeder->set_db_needs_update(0); - for my $artist (@ARGV) { - if ( $feeder->db_add_unwanted_artist($artist) ) { - $log->info("Artist '$artist' added to the unwanted list\n"); - } - else { - $log->warn("Artist '$artist' already in the unwanted list\n"); - } - } - exit; - } - - if ( $cmd eq 'del-unwanted-artist' ) { - die "Missing command arguments\n" unless @ARGV; - $feeder->set_db_needs_update(0); - for my $artist (@ARGV) { - if ( $feeder->db_del_unwanted_artist($artist) ) { - $log->info("Artist '$artist' deleted from the unwanted list\n"); - } - else { - $log->warn("Artist '$artist' is not in the unwanted list\n"); - } - } - exit; - } - - if ( $cmd eq 'add-unwanted-album' ) { - die "NOT IMPLEMENTED\n"; - } - - if ( $cmd eq 'one-shot' ) { - die "one-shot command accepts no arguments\n" if @ARGV; - - $feeder->queue_songs(undef, sub { exit }); - $feeder->mpd->loop->run; - } - elsif ( $cmd eq 'single' ) { - die "single command accepts no arguments\n" if @ARGV; - - $feeder->queue_songs(1, sub { exit }); - $feeder->mpd->loop->run; - } - else { - die "Unknown command '$cmd'"; - } + exit $feeder->run(@ARGV); } $feeder->run_loop; diff --git a/lib/App/MPD/Feeder/Command.pm b/lib/App/MPD/Feeder/Command.pm new file mode 100644 index 0000000..37b8ee1 --- /dev/null +++ b/lib/App/MPD/Feeder/Command.pm @@ -0,0 +1,79 @@ +package App::MPD::Feeder::Command; + +use strict; +use warnings; +use utf8; + +use Log::Any qw($log); +use Object::Pad; + +class App::MPD::Feeder::Command +isa App::MPD::Feeder { + method run(@args) { + my $cmd = shift @args; + + if ( $cmd eq 'dump-config' ) { + die "dump-config command accepts no arguments\n" if @args; + + $self->opt->dump; + + return 0; + } + + if ( $cmd eq 'add-unwanted-artist' ) { + die "Missing command arguments\n" unless @args; + $self->set_db_needs_update(0); + for my $artist (@args) { + if ( $self->db_add_unwanted_artist($artist) ) { + $log->info( + "Artist '$artist' added to the unwanted list\n"); + } + else { + $log->warn( + "Artist '$artist' already in the unwanted list\n"); + } + } + + return 0; + } + + if ( $cmd eq 'del-unwanted-artist' ) { + die "Missing command arguments\n" unless @args; + $self->set_db_needs_update(0); + for my $artist (@args) { + if ( $self->db_del_unwanted_artist($artist) ) { + $log->info( + "Artist '$artist' deleted from the unwanted list\n"); + } + else { + $log->warn( + "Artist '$artist' is not in the unwanted list\n"); + } + } + + return 0; + } + + if ( $cmd eq 'add-unwanted-album' ) { + die "NOT IMPLEMENTED\n"; + } + + if ( $cmd eq 'one-shot' ) { + die "one-shot command accepts no arguments\n" if @args; + + $self->queue_songs( undef, sub {$self->mpd->loop->stop} ); + $self->mpd->loop->run; + return 0; + } + elsif ( $cmd eq 'single' ) { + die "single command accepts no arguments\n" if @args; + + $self->queue_songs( 1, sub {$self->mpd->loop->stop} ); + $self->mpd->loop->run; + return 0; + } + else { + die "Unknown command '$cmd'"; + } + } +} -- 2.39.5