]> git.ktnx.net Git - mpd-feeder.git/blob - lib/App/MPD/Feeder/Command.pm
d2c4130a5691a9373d2db1ee35a90ad09bd30d77
[mpd-feeder.git] / lib / App / MPD / Feeder / Command.pm
1 package App::MPD::Feeder::Command;
2
3 use strict;
4 use warnings;
5 use utf8;
6 use feature qw(fc say);
7
8 use Log::Any qw($log);
9 use Object::Pad;
10
11 class App::MPD::Feeder::Command
12 isa App::MPD::Feeder {
13     method run(@args) {
14         my $cmd = shift @args;
15
16         if ( $cmd eq 'dump-config' ) {
17             die "dump-config command accepts no arguments\n" if @args;
18
19             $self->opt->dump;
20
21             return 0;
22         }
23
24         if ( $cmd eq 'add-unwanted-artist' ) {
25             die "Missing command arguments\n" unless @args;
26             $self->set_db_needs_update(0);
27             for my $artist (@args) {
28                 if ( $self->db->add_unwanted_artist($artist) ) {
29                     $log->info(
30                         "Artist '$artist' added to the unwanted list\n");
31                 }
32                 else {
33                     $log->warn(
34                         "Artist '$artist' already in the unwanted list\n");
35                 }
36             }
37
38             return 0;
39         }
40
41         if ( $cmd eq 'del-unwanted-artist' ) {
42             die "Missing command arguments\n" unless @args;
43             $self->set_db_needs_update(0);
44             for my $artist (@args) {
45                 if ( $self->db->del_unwanted_artist($artist) ) {
46                     $log->info(
47                         "Artist '$artist' deleted from the unwanted list\n");
48                 }
49                 else {
50                     $log->warn(
51                         "Artist '$artist' is not in the unwanted list\n");
52                 }
53             }
54
55             return 0;
56         }
57
58         if ( $cmd eq 'list-unwanted-artists' ) {
59             die "This command has no arguments\n" if @args;
60             $self->set_db_needs_update(0);
61             my $count = $self->db->walk_unwanted_artists( sub { say @_ } );
62             say "Total unwanted artists: $count";
63
64             return 0;
65         }
66
67         if ( $cmd eq 'add-unwanted-album' ) {
68             die
69                 "Syntax: mpd-feeder add-unwanted-album «album name» by «artist name»\n"
70                 unless @args == 3 and $args[1] =~ /^by$/i;
71             $self->set_db_needs_update(0);
72             my ( $album, $artist ) = @args[ 0, 2 ];
73             if ( $self->db->add_unwanted_album( $album, $artist ) ) {
74                 $log->info(
75                     "Album «$album» by «$artist» added to the unwanted list\n"
76                 );
77             }
78             else {
79                 $log->warn(
80                     "Album «$album» by «$artist» already in the unwanted list\n"
81                 );
82             }
83
84             return 0;
85         }
86
87         if ( $cmd eq 'del-unwanted-album' ) {
88             die
89                 "Syntax: mpd-feeder del-unwanted-album «album name» by «artist name»\n"
90                 unless @args == 3 and $args[1] =~ /^by$/i;
91             $self->set_db_needs_update(0);
92             my ( $album, $artist ) = @args[ 0, 2 ];
93             if ( $self->db->del_unwanted_album( $album, $artist ) ) {
94                 $log->info(
95                     "Album «$album» by «$artist» deleted from the unwanted list\n"
96                 );
97             }
98             else {
99                 $log->warn(
100                     "Album «$album» by «$artist» is not in the unwanted list\n"
101                 );
102             }
103
104             return 0;
105         }
106
107         if ( $cmd eq 'list-unwanted-albums' ) {
108             die "This command has no arguments\n" if @args;
109             $self->set_db_needs_update(0);
110             my $count = $self->db->walk_unwanted_albums(
111                 sub ( $album, $artist ) { say "«$album» by «$artist»" } );
112             say "Total unwanted albums: $count";
113
114             return 0;
115         }
116
117         if ( $cmd eq 'one-shot' ) {
118             die "one-shot command accepts no arguments\n" if @args;
119
120             $self->queue_songs( undef, sub {$self->mpd->loop->stop} );
121             $self->mpd->loop->run;
122             return 0;
123         }
124         elsif ( $cmd eq 'single' ) {
125             die "single command accepts no arguments\n" if @args;
126
127             $self->queue_songs( 1, sub {$self->mpd->loop->stop} );
128             $self->mpd->loop->run;
129             return 0;
130         }
131         else {
132             die "Unknown command '$cmd'";
133         }
134     }
135 }