Groups probe for /etc/group info
Robert 'phaylon' Sedlacek [Thu, 3 May 2012 16:13:09 +0000 (16:13 +0000)]
lib/System/Introspector/Groups.pm [new file with mode: 0644]

diff --git a/lib/System/Introspector/Groups.pm b/lib/System/Introspector/Groups.pm
new file mode 100644 (file)
index 0000000..b456a5f
--- /dev/null
@@ -0,0 +1,30 @@
+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;