4 class App::MPD::Feeder::Options;
7 use Time::Duration qw(duration_exact);
8 use Time::Duration::Parse qw(parse_duration);
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');
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);
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);
38 'min-sing-interval=s' => sub {
39 $min_song_interval = parse_duration(pop);
41 'min-artist-interval=s' => sub {
42 $min_artist_interval = parse_duration(pop);
47 sub handle_config_option( $ini, $section, $option, $target_ref,
50 return undef unless exists $ini->{$section}{$option};
52 my $value = $ini->{$section}{$option};
54 $value = $converter->($value) if $converter;
56 $$target_ref = $value;
58 $log->trace("Option $section.$option = $value");
63 say "log_level = $log_level";
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);
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);
79 say "path = " . ( $db_path // '' );
80 say "user = " . ( $db_user // '' );
81 say "password = " . ( $db_password // '' );
84 sub parse_integer($input) {
85 die "Invalid integer value '$input'" unless $input =~ /^\+?\d{1,18}$/;
89 method parse_config_file($path) {
90 $log->trace("Parsing configuration file $path");
92 use Config::INI::Reader;
93 my $ini = Config::INI::Reader->read_file($path);
95 handle_config_option( $ini => mpd => host => \$mpd_host );
96 handle_config_option( $ini => mpd => port => \$mpd_port );
98 $ini => mpd => 'initial-reconnect-delay' => \$initial_reconnect_delay,
101 handle_config_option(
102 $ini => mpd => 'max-reconnect-delay' => \$max_reconnect_delay,
106 handle_config_option( $ini => 'mpd-feeder' => log_level => \$log_level );
108 handle_config_option(
109 $ini => queue => 'target-length' => \$target_queue_length,
112 handle_config_option(
113 $ini => queue => 'min-song-interval' => \$min_song_interval,
116 handle_config_option(
117 $ini => queue => 'min-album-interval' => \$min_album_interval,
120 handle_config_option(
121 $ini => queue => 'min-artist-interval' => \$min_artist_interval,
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 );
129 # FIXME: complain about unknown sections/parameters