From: Robert 'phaylon' Sedlacek Date: Thu, 10 May 2012 18:53:48 +0000 (+0000) Subject: use reusable I/O utils, more solid error handling for Puppet probe X-Git-Tag: v0.001_001~79 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3b52886962afb2c3a4b46902e09d9e35233ad61b;p=scpubgit%2FSystem-Introspector.git use reusable I/O utils, more solid error handling for Puppet probe --- diff --git a/lib/System/Introspector/Puppet.pm b/lib/System/Introspector/Puppet.pm index 9e65114..a764b30 100644 --- a/lib/System/Introspector/Puppet.pm +++ b/lib/System/Introspector/Puppet.pm @@ -1,6 +1,11 @@ package System::Introspector::Puppet; use Moo; +use System::Introspector::Util qw( + output_from_file + transform_exceptions +); + has classes_file => ( is => 'ro', default => sub { '/var/lib/puppet/state/classes.txt' }, @@ -21,26 +26,24 @@ sub gather { sub _gather_resources { my ($self) = @_; - my $file = $self->resources_file; - open my $fh, '<', $file - or return { error => "Unable to read $file: $!" }; - my @lines = <$fh>; - chomp @lines; - return [ map { - m{^(\w+)\[(.*)\]$} - ? [$1, $2] - : [error => $_]; - } @lines ]; + return transform_exceptions { + my @lines = output_from_file $self->resources_file; + chomp @lines; + return { list => [ map { + m{^(\w+)\[(.*)\]$} + ? [$1, $2] + : [error => $_]; + } @lines ] }; + }; } sub _gather_classes { my ($self) = @_; - my $file = $self->classes_file; - open my $fh, '<', $file - or return { error => "Unable to read $file: $!" }; - my @lines = <$fh>; - chomp @lines; - return \@lines; + return transform_exceptions { + my @lines = output_from_file $self->classes_file; + chomp @lines; + return { list => \@lines }; + }; } 1; diff --git a/t/puppet.t b/t/puppet.t index 0d8d206..3a56c55 100644 --- a/t/puppet.t +++ b/t/puppet.t @@ -10,10 +10,10 @@ my $probe = System::Introspector::Puppet->new( ); my $data = $probe->gather; -is_deeply $data->{classes}, +is_deeply $data->{classes}{list}, [qw( user::foo settings user::foo user::bar )], 'classes parsing'; -is_deeply $data->{resources}, +is_deeply $data->{resources}{list}, [[user => 'foo'], [exec => 'ls -lha'], [file => '/home/foo/quux'],