File Coverage

blib/lib/System/Introspector/Probe/Sudoers.pm
Criterion Covered Total %
statement 34 34 100.0
branch 6 8 75.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 0 1 0.0
total 48 53 90.5


line stmt bran cond sub pod time code
1             package System::Introspector::Probe::Sudoers;
2 1     1   42967 use Moo;
  1         14008  
  1         6  
3              
4 1         1132 use System::Introspector::Util qw(
5             handle_from_command
6             files_from_dir
7             output_from_file
8             transform_exceptions
9 1     1   2143 );
  1         5  
10              
11             has sudoers_file => (
12             is => 'ro',
13             default => sub { '/etc/sudoers' },
14             );
15              
16             has hostname => (
17             is => 'ro',
18             default => sub { scalar `hostname` },
19             );
20              
21             sub gather {
22 1     1 0 15342 my ($self) = @_;
23 1         22 my %file = $self->_gather_files($self->sudoers_file);
24 1         13 return \%file;
25             }
26              
27             sub _gather_files {
28 3     3   6 my ($self, $file) = @_;
29             my $result = transform_exceptions {
30 3     3   14 my @lines = output_from_file $file;
31 3         12 my @result = ({ body => join '', @lines });
32 3         5 for my $line (@lines) {
33 6         12 chomp $line;
34 6 100       36 if ($line =~ m{^#include\s+(.+)$}) {
    100          
35 1         35 my $inc_file = $self->_insert_hostname($1);
36 1         9 push @result, $self->_gather_files($inc_file);
37             }
38             elsif ($line =~ m{^#includedir\s+(.+)$}) {
39 1         2 my $inc_dir = $self->_insert_hostname($1);
40 1         3 push @result, $self->_gather_from_dir($inc_dir);
41             }
42             }
43 3         12 return \@result;
44 3         50 };
45 3 50       22 return $file => $result
46             if ref $result eq 'HASH';
47 3         12 return $file => @$result;
48             }
49              
50             sub _gather_from_dir {
51 1     1   2 my ($self, $dir) = @_;
52 1         5 my @files = files_from_dir $dir;
53 1         1 my %file;
54 1         5 for my $file (@files) {
55 1 50 33     14 next if $file =~ m{\.} or $file =~ m{~$};
56 1         5 %file = (%file, $self->_gather_files("$dir/$file"));
57             }
58 1         5 return %file;
59             }
60              
61             sub _insert_hostname {
62 2     2   4 my ($self, $value) = @_;
63 2         6 my $hostname = $self->hostname;
64 2         10 $value =~ s{\%h}{$hostname}g;
65 2         9 return $value;
66             }
67              
68             1;
69              
70             __END__