simulated lsof
[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
b079a95d 22sub hosts {
23 my ($self) = @_;
24 my $host_spec = $self->config->{host};
25 return ref($host_spec) ? @$host_spec : $host_spec;
26}
27
28sub user { $_[0]->config->{user} }
29
a5e1e1c6 30my $_get_inherited = sub {
31 my $data = shift;
32 $data ||= {};
33 return
34 map { ($_ => $data->{$_}) }
35 grep { exists $data->{$_} }
36 qw( sudo );
37};
38
39sub config_for_group {
40 my ($self, $name) = @_;
41 my %common;
42 my $config = $self->config;
43 %common = (%common, $config->$_get_inherited);
44 my $group = $config->{group}{ $name };
45 %common = (%common, $group->$_get_inherited);
46 return {
47 introspect => {
48 map {
49 ($_ => {
50 %common,
51 %{ $group->{introspect}{ $_ } || {} },
52 });
53 } keys %{ $group->{introspect} || {} },
54 },
55 };
56}
60e1cc39 57
581;