]> git.ktnx.net Git - mpd-feeder.git/commitdiff
rework command line handling - commands are commands, options are --options
authorDamyan Ivanov <dmn@debian.org>
Thu, 11 Nov 2021 10:39:08 +0000 (10:39 +0000)
committerDamyan Ivanov <dmn@debian.org>
Thu, 11 Nov 2021 10:39:08 +0000 (10:39 +0000)
also, initializa connections only if/when needed

bin/mpd-feeder

index 843fbbec7938c726cfce356fb784817781a3c18e..5760c751633483dd6f02fe95fe908ae6876e42c1 100755 (executable)
@@ -21,24 +21,18 @@ class Options {
     has $min_album_interval  :reader = parse_duration('5h');
     has $min_song_interval   :reader = parse_duration('13d');
     has $min_artist_interval :reader = parse_duration('1h 15m');
-    has $single              :reader = 0;
-    has $one_shot            :reader = 0;
     has $skip_db_update      :reader = 0;
-    has $dump_config         :reader = 0;
 
     method parse_command_line {
         Getopt::Long::GetOptions(
             'log-level=s'               => \$log_level,
-            'dump-config!'              => \$dump_config,
-            's|single!'                 => \$single,
-            'one-shot!'                 => \$one_shot,
             'skip-db-update!'           => \$skip_db_update,
             'tql|target-queue-length=n' => \$target_queue_length,
             'mpd-host=s'                => \$mpd_host,
             'mpd-port=s'                => \$mpd_port,
             'db-path=s'                 => \$db_path,
             'db-user=s'                 => \$db_user,
-            'min-album-interval=s'        => sub {
+            'min-album-interval=s'      => sub {
                 $min_album_interval = parse_duration(pop);
             },
             'min-sing-interval=s' => sub {
@@ -145,30 +139,29 @@ use Net::Async::MPD;
         $opt->parse_command_line;
 
         Log::Any::Adapter->set( Stderr => log_level => $opt->log_level );
+    }
 
-        unless ($opt->dump_config) {
-            my %conn = ( auto_connect => 1 );
-            $conn{host} = $opt->mpd_host if $opt->mpd_host;
-            $conn{port} = $opt->mpd_port if $opt->mpd_port;
+    method connect_mpd {
+        return if $mpd;
 
-            $mpd = Net::Async::MPD->new(%conn);
+        my %conn = ( auto_connect => 1 );
+        $conn{host} = $opt->mpd_host if $opt->mpd_host;
+        $conn{port} = $opt->mpd_port if $opt->mpd_port;
 
-            $self->connect_db;
-            $self->update_db unless $self->opt->skip_db_update;
-        }
+        $mpd = Net::Async::MPD->new(%conn);
     }
 
     method connect_db {
         return if $db;
 
-        $db =
-            DBI->connect( "dbi:Pg:dbname=" . $opt->db_path,
+        $db = DBI->connect( "dbi:Pg:dbname=" . $opt->db_path,
             $opt->db_user, $opt->db_password,
             { RaiseError => 1, AutoCommit => 1 } );
 
-        $log->info("Connected to ".$opt->db_path);
+        $log->info( "Connected to database " . $opt->db_path );
         $db_generation = $self->db_get_option('generation');
         $log->debug("DB generation is $db_generation");
+        $self->update_db unless $opt->skip_db_update;
     }
 
     method db_get_option($name) {
@@ -279,6 +272,8 @@ SQL
     }
 
     method db_find_suitable_songs($num) {
+        $self->connect_db;
+
         my @result;
         my $sth = $db->prepare_cached(<<SQL);
 SELECT s.path, s.artist, s.album
@@ -294,9 +289,9 @@ ORDER BY random()
 LIMIT ?
 SQL
         $sth->execute(
-            $self->opt->min_song_interval,
-            $self->opt->min_artist_interval,
-            $self->opt->min_album_interval,
+            $opt->min_song_interval,
+            $opt->min_artist_interval,
+            $opt->min_album_interval,
             $num,
         );
         while ( my @row = $sth->fetchrow_array ) {
@@ -308,6 +303,7 @@ SQL
     }
 
     method queue_songs($num = undef, $callback = undef) {
+        $self->connect_mpd;
         if (!defined $num) {
             $mpd->send('playlist')->on_done(
                 sub {
@@ -407,27 +403,35 @@ SQL
 
 my $feeder = Feeder->new();
 
-$feeder->opt->dump, exit if $feeder->opt->dump_config;
-
-$feeder->queue_songs(1), exit if $feeder->opt->single;
-
-# FIXME: handle blacklist manipulation
+sub usage {
+    die "Usage: mpd-feeder [option...] [command]\n";
+}
 
-$feeder->queue_songs;
+if (@ARGV) {
+    usage if @ARGV > 1;
 
-exit if $feeder->opt->one_shot;
+    my $cmd = shift @ARGV;
 
-$feeder->mpd->on(
-    database => sub {
-        $feeder->update_db;
+    if ($cmd eq 'dump-config') {
+        $feeder->opt->dump;
+        exit;
     }
-);
+# FIXME: handle blacklist manipulation
 
-$feeder->mpd->on(
-    playlist => sub {
-        $feeder->queue_songs;
+    if ( $cmd eq 'one-shot' ) {
+        $feeder->queue_songs(undef, sub { exit });
+        $feeder->mpd->loop->run;
+    }
+    elsif ( $cmd eq 'single' ) {
+        $feeder->queue_songs(1, sub { exit });
+        $feeder->mpd->loop->run;
+    }
+    else {
+        die "Unknown command '$cmd'";
     }
-);
+}
+
+
+$feeder->queue_songs( undef, sub { $feeder->run } );
 
-$feeder->mpd->idle(qw(database playlist));
-$feeder->mpd->get;
+$feeder->mpd->loop->run;