package System::Introspector::Probe::ResolvConf;
use Moo;

use System::Introspector::Util qw(
    handle_from_file
    transform_exceptions
);

has resolv_conf_file => (
    is      => 'ro',
    default => sub { '/etc/resolv.conf' },
);

sub gather {
    my ($self) = @_;
    return transform_exceptions {
        my $fh = $self->_open_resolv_conf_file;
        my @resolv;
        while (defined( my $line = <$fh> )) {
            chomp $line;
            next if $line =~ m{^\s*$}
                 or $line =~ m{^\s*#};
            push @resolv, [split m{\s+}, $line];
        }
        return { resolv_conf => \@resolv };
    };
}

sub _open_resolv_conf_file {
    my ($self) = @_;
    return handle_from_file $self->resolv_conf_file;
}

1;

__END__

=head1 NAME

System::Introspector::ResolvConf - Gather name resolution configuration

=head1 DESCRIPTION

Reads a C<resolv.conf> file to gather information about name resolution.

=head1 ATTRIBUTES

=head2 resolv_conf_file

The path to the C<resolv.conf> file that should be read. Defaults to
C</etc/resolv.conf>.

=head1 SEE ALSO

=over

=item L<System::Introspector>

=back

=cut

