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