From 8ac2eb6482aac96305d9f893f7cfd0ff175dd27a Mon Sep 17 00:00:00 2001 From: Maarten Vanraes Date: Sat, 6 Aug 2016 13:48:01 +0200 Subject: Plugin: add a tool_columns helper function --- lib/ManaTools/Shared/disk_backend/Plugin.pm | 81 +++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/lib/ManaTools/Shared/disk_backend/Plugin.pm b/lib/ManaTools/Shared/disk_backend/Plugin.pm index fc1dd7ee..e9a6cdbb 100644 --- a/lib/ManaTools/Shared/disk_backend/Plugin.pm +++ b/lib/ManaTools/Shared/disk_backend/Plugin.pm @@ -347,4 +347,85 @@ sub tool_fields { return %fields; } +#============================================================= + +=head2 tool_columns + +=head3 INPUT + + $toolname: Str + $headers: Bool + $ignores: Int + $identifier: Int + $separator: Str + @args: Array[Str] + +=head3 OUTPUT + + HashRef[HashRef]|undef + +=head3 DESCRIPTION + + this is a default method for executing a tool and getting all the STDOUT + in a HASH depending on the separator when it's a column-based output + +=cut + +#============================================================= +sub tool_columns { + my $self = shift; + my $toolname = shift; + my $headers = shift; + my $ignores = shift; + my $identifier = shift; + my $separator = shift; + my @args = @_; + my $fields = {}; + + # get lines from tool + my @lines = $self->tool_lines($toolname, @args); + return $fields if scalar(@lines) < ($ignores + !!$headers); + my @headers = (); + # get headers + my $line = shift(@lines); + @headers = split($separator, $line) if $headers; + # ignore lines if needed + for (my $i = 0; $i < $ignores; $i = $i + 1) { + shift(@lines); + } + # loop the data + for my $line (@lines) { + my $item = {}; + + # split into key & value + my @value = split($separator, $line); + + # if no 2 columns, skip this line + next if (scalar(@value) < 2); + + my $i = 0; + for my $value (@value) { + my $key = $i; + $key = $headers[$i] if (defined $headers[$i]); + + # trim key & value + $key =~ s/^\s+//; + $key =~ s/\s+$//; + $value =~ s/^\s+//; + $value =~ s/\s+$//; + + # need to be the index if key is empty after all + $key = $i if ($key eq ''); + + # set the field + $item->{$key} = $value; + + # next field + $i = $i + 1; + } + $fields->{$item->{$identifier}} = $item if defined($item->{$identifier}); + } + return $fields; +} + 1; -- cgit v1.2.1