b456a5fcf0a62c9a17f6cc4441c34bfe138ae26b
[scpubgit/System-Introspector.git] / lib / System / Introspector / Groups.pm
1 package System::Introspector::Groups;
2 use Moo;
3
4 sub gather {
5     my ($self) = @_;
6     my %group;
7     my $fh = $self->_open_group_file;
8     while (defined( my $line = <$fh> )) {
9         chomp $line;
10         my ($name, undef, $gid, $users) = split m{:}, $line;
11         $users = length($users)
12             ? [split m{,}, $users]
13             : [];
14         $group{ $gid } = {
15             name    => $name,
16             gid     => $gid,
17             users   => $users,
18         };
19     }
20     return \%group;
21 }
22
23 sub _open_group_file {
24     my ($self) = @_;
25     open my $fh, '<', '/etc/group'
26         or die "Unable to read group file /etc/group: $!\n";
27     return $fh;
28 }
29
30 1;