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