1 package App::MPD::Feeder::Options;
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;
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);
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);
39 'min-sing-interval=s' => sub {
40 $min_song_interval = parse_duration(pop);
42 'min-artist-interval=s' => sub {
43 $min_artist_interval = parse_duration(pop);
48 sub handle_config_option( $ini, $section, $option, $target_ref,
51 return undef unless exists $ini->{$section}{$option};
53 my $value = $ini->{$section}{$option};
55 $value = $converter->($value) if $converter;
57 $$target_ref = $value;
59 $log->trace("Option $section.$option = $value");
64 say "log_level = $log_level";
67 say "host = " . ( $mpd_host // '' );
68 say "port = " . ( $mpd_port // '' );
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);
77 say "path = " . ( $db_path // '' );
78 say "user = " . ( $db_user // '' );
79 say "password = " . ( $db_password // '' );
82 sub parse_integer($input) {
83 die "Invalid integer value '$input'" unless $input =~ /^\+?\d{1,18}$/;
87 method parse_config_file($path) {
88 $log->trace("Parsing configuration file $path");
90 use Config::INI::Reader;
91 my $ini = Config::INI::Reader->read_file($path);
93 handle_config_option( $ini => mpd => host => \$mpd_host );
94 handle_config_option( $ini => mpd => port => \$mpd_port );
96 handle_config_option( $ini => 'mpd-feeder' => log_level => \$log_level );
99 $ini => queue => 'target-length' => \$target_queue_length,
102 handle_config_option(
103 $ini => queue => 'min-song-interval' => \$min_song_interval,
106 handle_config_option(
107 $ini => queue => 'min-album-interval' => \$min_album_interval,
110 handle_config_option(
111 $ini => queue => 'min-artist-interval' => \$min_artist_interval,
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 );
119 # FIXME: complain about unknown sections/parameters