sort by filesystem/mount_point
[scpubgit/System-Introspector.git] / lib / System / Introspector / DiskUsage.pm
index ef7e6d9..b2d1d9e 100644 (file)
@@ -1,25 +1,36 @@
 package System::Introspector::DiskUsage;
 use Moo;
 
+use System::Introspector::Util qw(
+    lines_from_command
+    transform_exceptions
+);
+
 sub gather {
     my ($self) = @_;
-    my @lines = `df -aP`;
-    shift @lines; # header
-    chomp @lines;
-    my @rows;
-    for my $line (@lines) {
-        my %row;
-        @row{qw(
-            filesystem
-            blocks_1024
-            used
-            available
-            capacity
-            mount_point
-        )} = split m{\s+}, $line;
-        push @rows, \%row;
-    }
-    return \@rows;
+    return transform_exceptions {
+        my @lines = lines_from_command ['df', '-aP'];
+        shift @lines; # header
+        my @rows;
+        for my $line (@lines) {
+            my %row;
+            @row{qw(
+                filesystem
+                blocks_1024
+                used
+                available
+                capacity
+                mount_point
+            )} = split m{\s+}, $line;
+            push @rows, \%row;
+        }
+        no warnings 'uninitialized';
+        return { disk_usage => [ sort {
+            ($a->{filesystem} cmp $b->{filesystem})
+            ||
+            ($a->{mount_point} cmp $b->{mount_point})
+        } @rows ] };
+    };
 }
 
 1;