reworked gatherer to be more flexible, added sudo support
[scpubgit/System-Introspector.git] / lib / System / Introspector / Config.pm
1 package System::Introspector::Config;
2 use Moo;
3 use Config::General;
4
5 has config => (is => 'lazy');
6
7 has config_file => (is => 'ro', required => 1);
8
9 sub _build_config {
10     my ($self) = @_;
11     my $reader = Config::General->new($self->config_file);
12     my %config = $reader->getall;
13     return \%config;
14 }
15
16 sub sudo_user { $_[0]->config->{sudo_user} }
17
18 sub groups { keys %{ $_[0]->config->{group} || {} } }
19
20 sub has_group { exists $_[0]->config->{group}{ $_[1] } }
21
22 my $_get_inherited = sub {
23     my $data = shift;
24     $data ||= {};
25     return
26         map  { ($_ => $data->{$_}) }
27         grep { exists $data->{$_} }
28         qw( sudo );
29 };
30
31 sub config_for_group {
32     my ($self, $name) = @_;
33     my %common;
34     my $config = $self->config;
35     %common = (%common, $config->$_get_inherited);
36     my $group = $config->{group}{ $name };
37     %common = (%common, $group->$_get_inherited);
38     return {
39         introspect => {
40             map {
41                 ($_ => {
42                     %common,
43                     %{ $group->{introspect}{ $_ } || {} },
44                 });
45             } keys %{ $group->{introspect} || {} },
46         },
47     };
48 }
49
50 1;