]> git.ktnx.net Git - mpd-feeder.git/blob - lib/App/MPD/Feeder/Options.pm
21bd6467a537e54dbdc8c85cae21f7a35eca204d
[mpd-feeder.git] / lib / App / MPD / Feeder / Options.pm
1 package App::MPD::Feeder::Options;
2
3 use strict;
4 use warnings;
5 use utf8;
6 use feature 'say';
7 use Object::Pad;
8
9 class App::MPD::Feeder::Options {
10     use Log::Any qw($log);
11     use Time::Duration qw(duration_exact);
12     use Time::Duration::Parse qw(parse_duration);
13     has $log_level           :reader = 'warn';
14     has $target_queue_length :reader = 10;
15     has $mpd_host            :reader = undef;
16     has $mpd_port            :reader = undef;
17     has $db_path             :reader = 'mpd-feeder';
18     has $db_user             :reader = undef;
19     has $db_password         :reader = undef;
20     has $min_album_interval  :reader = parse_duration('5h');
21     has $min_song_interval   :reader = parse_duration('13d');
22     has $min_artist_interval :reader = parse_duration('1h 15m');
23     has $skip_db_update      :reader = 0;
24
25     method parse_command_line {
26         Getopt::Long::GetOptions(
27             'log-level=s'               => \$log_level,
28             'skip-db-update!'           => \$skip_db_update,
29             'tql|target-queue-length=n' => sub {
30                 $target_queue_length = parse_integer(pop);
31             },
32             'mpd-host=s'           => \$mpd_host,
33             'mpd-port=s'           => \$mpd_port,
34             'db-path=s'            => \$db_path,
35             'db-user=s'            => \$db_user,
36             'min-album-interval=s' => sub {
37                 $min_album_interval = parse_duration(pop);
38             },
39             'min-sing-interval=s' => sub {
40                 $min_song_interval = parse_duration(pop);
41             },
42             'min-artist-interval=s' => sub {
43                 $min_artist_interval = parse_duration(pop);
44             },
45         ) or exit 1;
46     }
47
48     sub handle_config_option( $ini, $section, $option, $target_ref,
49         $converter = undef )
50     {
51         return undef unless exists $ini->{$section}{$option};
52
53         my $value = $ini->{$section}{$option};
54
55         $value = $converter->($value) if $converter;
56
57         $$target_ref = $value;
58
59         $log->trace("Option $section.$option = $value");
60     }
61
62     method dump {
63         say "[mpd-feeder]";
64         say "log_level = $log_level";
65         say "";
66         say "[mpd]";
67         say "host = " . ( $mpd_host // '' );
68         say "port = " . ( $mpd_port // '' );
69         say "";
70         say "[queue]";
71         say "target-length = $target_queue_length";
72         say "min-song-interval = " . duration_exact($min_song_interval);
73         say "min-album-interval = " . duration_exact($min_album_interval);
74         say "min-artist-interval = " . duration_exact($min_artist_interval);
75         say "";
76         say "[db]";
77         say "path = " .     ( $db_path     // '' );
78         say "user = " .     ( $db_user     // '' );
79         say "password = " . ( $db_password // '' );
80     }
81
82     sub parse_integer($input) {
83         die "Invalid integer value '$input'" unless $input =~ /^\+?\d{1,18}$/;
84         return $input + 0;
85     }
86
87     method parse_config_file($path) {
88         $log->trace("Parsing configuration file $path");
89
90         use Config::INI::Reader;
91         my $ini = Config::INI::Reader->read_file($path);
92
93         handle_config_option( $ini => mpd => host => \$mpd_host );
94         handle_config_option( $ini => mpd => port => \$mpd_port );
95
96         handle_config_option( $ini => 'mpd-feeder' => log_level => \$log_level );
97
98         handle_config_option(
99             $ini => queue => 'target-length' => \$target_queue_length,
100             \&parse_integer
101         );
102         handle_config_option(
103             $ini => queue => 'min-song-interval' => \$min_song_interval,
104             \&parse_duration
105         );
106         handle_config_option(
107             $ini => queue => 'min-album-interval' => \$min_album_interval,
108             \&parse_duration
109         );
110         handle_config_option(
111             $ini => queue => 'min-artist-interval' => \$min_artist_interval,
112             \&parse_duration
113         );
114
115         handle_config_option( $ini => db => path     => \$db_path );
116         handle_config_option( $ini => db => user     => \$db_user );
117         handle_config_option( $ini => db => password => \$db_password );
118
119         # FIXME: complain about unknown sections/parameters
120     }
121 }