package System::Introspector::Probe::Packages::Apt;
use Moo;

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

sub gather {
    my ($self) = @_;
    return {
        installed => transform_exceptions {
            return { packages => $self->_gather_installed };
        },
    };
}

sub _open_dpkg_query_pipe {
    my ($self) = @_;
    return handle_from_command 'dpkg-query --show';
}

sub _gather_installed {
    my ($self) = @_;
    my $pipe = $self->_open_dpkg_query_pipe;
    my %package;
    while (defined( my $line = <$pipe> )) {
        chomp $line;
        my ($package, $version) = split m{\s+}, $line;
        $package{ $package } = {
            version => $version,
        };
    }
    return \%package;
}

1;

__END__

=head1 NAME

System::Introspector::Packages::Apt - Gather APT package status

=head1 DESCRIPTION

Uses C<dpkg-query> to list all installed packages.

=head1 SEE ALSO

=over

=item L<System::Introspector>

=back

=cut
