simulated lsof
[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 sub hosts {
23     my ($self) = @_;
24     my $host_spec = $self->config->{host};
25     return ref($host_spec) ? @$host_spec : $host_spec;
26 }
27
28 sub user { $_[0]->config->{user} }
29
30 my $_get_inherited = sub {
31     my $data = shift;
32     $data ||= {};
33     return
34         map  { ($_ => $data->{$_}) }
35         grep { exists $data->{$_} }
36         qw( sudo );
37 };
38
39 sub 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 }
57
58 1;