turned into a dist
[scpubgit/System-Introspector.git] / lib / System / Introspector / Puppet.pm
index 9e65114..a764b30 100644 (file)
@@ -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;