reworked gatherer to be more flexible, added sudo support
[scpubgit/System-Introspector.git] / lib / System / Introspector / Config.pm
CommitLineData
60e1cc39 1package System::Introspector::Config;
2use Moo;
3use Config::General;
4
5has config => (is => 'lazy');
6
7has config_file => (is => 'ro', required => 1);
8
9sub _build_config {
10 my ($self) = @_;
11 my $reader = Config::General->new($self->config_file);
12 my %config = $reader->getall;
13 return \%config;
14}
15
a5e1e1c6 16sub sudo_user { $_[0]->config->{sudo_user} }
17
60e1cc39 18sub groups { keys %{ $_[0]->config->{group} || {} } }
19
20sub has_group { exists $_[0]->config->{group}{ $_[1] } }
21
a5e1e1c6 22my $_get_inherited = sub {
23 my $data = shift;
24 $data ||= {};
25 return
26 map { ($_ => $data->{$_}) }
27 grep { exists $data->{$_} }
28 qw( sudo );
29};
30
31sub 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}
60e1cc39 49
501;