package System::Introspector::Groups;
use Moo;

sub gather {
    my ($self) = @_;
    my %group;
    my $fh = $self->_open_group_file;
    while (defined( my $line = <$fh> )) {
        chomp $line;
        my ($name, undef, $gid, $users) = split m{:}, $line;
        $users = length($users)
            ? [split m{,}, $users]
            : [];
        $group{ $gid } = {
            name    => $name,
            gid     => $gid,
            users   => $users,
        };
    }
    return \%group;
}

sub _open_group_file {
    my ($self) = @_;
    open my $fh, '<', '/etc/group'
        or die "Unable to read group file /etc/group: $!\n";
    return $fh;
}

1;

__END__

=head1 NAME

System::Introspector::Groups - Gather group information

=head1 DESCRIPTION

Uses C</etc/group> to gather information about groups.

=head1 SEE ALSO

=over

=item L<System::Introspector>

=back

=cut

