From: Robert 'phaylon' Sedlacek Date: Thu, 10 May 2012 18:42:01 +0000 (+0000) Subject: use reusable I/O utils, more solid error handling X-Git-Tag: v0.001_001~81 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=da47168ae5cf20d1b2874d6a1ee2295342e3efb9;p=scpubgit%2FSystem-Introspector.git use reusable I/O utils, more solid error handling --- diff --git a/lib/System/Introspector/Sudoers.pm b/lib/System/Introspector/Sudoers.pm index 56afd04..89683d7 100644 --- a/lib/System/Introspector/Sudoers.pm +++ b/lib/System/Introspector/Sudoers.pm @@ -1,6 +1,13 @@ package System::Introspector::Sudoers; use Moo; +use System::Introspector::Util qw( + handle_from_command + files_from_dir + output_from_file + transform_exceptions +); + has sudoers_file => ( is => 'ro', default => sub { '/etc/sudoers' }, @@ -19,31 +26,30 @@ sub gather { sub _gather_files { my ($self, $file) = @_; - open my $fh, '<', $file - or return $file => { error => "Unable to read: $!" }; - my @lines = <$fh>; - my %file = ($file => { body => join '', @lines }); - for my $line (@lines) { - chomp $line; - if ($line =~ m{^#include\s+(.+)$}) { - my $inc_file = $self->_insert_hostname($1); - %file = (%file, $self->_gather_files($inc_file)); - } - elsif ($line =~ m{^#includedir\s+(.+)$}) { - my $inc_dir = $self->_insert_hostname($1); - %file = (%file, $self->_gather_from_dir($inc_dir)); + my $result = transform_exceptions { + my @lines = output_from_file $file; + my @result = ({ body => join '', @lines }); + for my $line (@lines) { + chomp $line; + if ($line =~ m{^#include\s+(.+)$}) { + my $inc_file = $self->_insert_hostname($1); + push @result, $self->_gather_files($inc_file); + } + elsif ($line =~ m{^#includedir\s+(.+)$}) { + my $inc_dir = $self->_insert_hostname($1); + push @result, $self->_gather_from_dir($inc_dir); + } } - } - return %file; + return \@result; + }; + return $file => @$result; } sub _gather_from_dir { my ($self, $dir) = @_; - opendir(my $dh, $dir); - return $dir => { error => "Unable to read dir $dir: $!" } - unless $dh; + my @files = files_from_dir $dir; my %file; - while (my $file = readdir $dh) { + for my $file (@files) { next if $file =~ m{\.} or $file =~ m{~$}; %file = (%file, $self->_gather_files("$dir/$file")); }