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