File Coverage

blib/lib/System/Introspector/Config.pm
Criterion Covered Total %
statement 32 32 100.0
branch 5 12 41.6
condition n/a
subroutine 10 10 100.0
pod 0 6 0.0
total 47 60 78.3


line stmt bran cond sub pod time code
1             package System::Introspector::Config;
2 1     1   56126 use Moo;
  1         37898  
  1         7  
3 1     1   3083 use Config::General;
  1         54334  
  1         101  
4 1     1   15 use File::Basename;
  1         3  
  1         771  
5              
6             has config => (is => 'lazy');
7              
8             has config_file => (is => 'ro', required => 1);
9              
10             sub _build_config {
11 1     1   371 my ($self) = @_;
12 1         14 my $reader = Config::General->new($self->config_file);
13 1         1766 my %config = $reader->getall;
14 1         35 return \%config;
15             }
16              
17 1     1 0 1143 sub sudo_user { $_[0]->config->{sudo_user} }
18              
19 1 50   1 0 2 sub groups { sort keys %{ $_[0]->config->{group} || {} } }
  1         33  
20              
21 2     2 0 395 sub has_group { exists $_[0]->config->{group}{ $_[1] } }
22              
23             my $_load_host_file = sub {
24             my ($self, $path) = @_;
25             my $full_path = join '/', dirname($self->config_file), $path;
26             open my $fh, '<:utf8', $full_path
27             or die "Unable to read host_file '$full_path': $!\n";
28             my @hosts = <$fh>;
29             chomp @hosts;
30             return grep { m{\S} and not m{^\s*#} } @hosts;
31             };
32              
33             sub hosts {
34 1     1 0 291 my ($self) = @_;
35 1         31 my $host_spec = $self->config->{host};
36 1         34 my $host_file = $self->config->{host_file};
37             return(
38 1 0       15 ref($host_spec)
    50          
    50          
39             ? @$host_spec
40             : defined($host_spec) ? $host_spec : (),
41             defined($host_file)
42             ? $self->$_load_host_file($host_file)
43             : (),
44             );
45             }
46              
47 1     1 0 30 sub user { $_[0]->config->{user} }
48              
49             my $_get_inherited = sub {
50             my $data = shift;
51             $data ||= {};
52             return
53             map { ($_ => $data->{$_}) }
54             grep { exists $data->{$_} }
55             qw( sudo );
56             };
57              
58             sub config_for_group {
59 2     2 0 388 my ($self, $name) = @_;
60 2         3 my %common;
61 2         61 my $config = $self->config;
62 2         18 %common = (%common, $config->$_get_inherited);
63 2         4 my $group = $config->{group}{ $name };
64 2         6 %common = (%common, $group->$_get_inherited);
65             return {
66 3 50       34 introspect => {
67             map {
68 2 50       10 ($_ => {
69             %common,
70 3         5 %{ $group->{introspect}{ $_ } || {} },
71             });
72 2         5 } keys %{ $group->{introspect} || {} },
73             },
74             };
75             }
76              
77             1;
78              
79             =head1 NAME
80              
81             System::Introspector::Config - Configuration file access
82              
83             =head1 SEE ALSO
84              
85             =over
86              
87             =item L
88              
89             =back
90              
91             =head1 COPYRIGHT
92              
93             Copyright (c) 2012 the L
94             L,
95             L and
96             L.
97              
98             =head1 LICENSE
99              
100             This library is free software and may be distributed under the same terms
101             as perl itself.
102              
103             =cut