top level resolv_conf/error value
[scpubgit/System-Introspector.git] / lib / System / Introspector / Hosts.pm
index d5e880c..5f01f37 100644 (file)
@@ -1,24 +1,61 @@
 package System::Introspector::Hosts;
 use Moo;
 
+use System::Introspector::Util qw(
+    handle_from_file
+    transform_exceptions
+);
+
+has hosts_file => (
+    is      => 'ro',
+    default => sub { '/etc/hosts' },
+);
+
 sub gather {
     my ($self) = @_;
-    my $fh = $self->_open_hosts_file;
-    my @hosts;
-    while (defined( my $line = <$fh> )) {
-        chomp $line;
-        next if $line =~ m{^\s*$}
-             or $line =~ m{^\s*#};
-        push @hosts, [split m{\s+}, $line];
-    }
-    return \@hosts;
+    return transform_exceptions {
+        my $fh = $self->_open_hosts_file;
+        my @hosts;
+        while (defined( my $line = <$fh> )) {
+            chomp $line;
+            next if $line =~ m{^\s*$}
+                 or $line =~ m{^\s*#};
+            push @hosts, [split m{\s+}, $line];
+        }
+        return { hosts => \@hosts };
+    };
 }
 
 sub _open_hosts_file {
     my ($self) = @_;
-    open my $fh, '<', '/etc/hosts'
-        or die "Unable to read /etc/hosts: $!\n";
-    return $fh;
+    return handle_from_file $self->hosts_file;
 }
 
 1;
+
+__END__
+
+=head1 NAME
+
+System::Introspector::Hosts - Gather known hosts
+
+=head1 DESCRIPTION
+
+Reads a C<hosts> file to produce a list of known hosts
+
+=head1 ATTRIBUTES
+
+=head2 hosts_file
+
+The path to the C<hosts> file that should be read. Defaults to C</etc/hosts>.
+
+=head1 SEE ALSO
+
+=over
+
+=item L<System::Introspector>
+
+=back
+
+=cut
+