Browse Source

[util] Support reversed sort ordering when generating NIC list

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Robin Smidsrød 6 years ago
parent
commit
eda9f4db61
1 changed files with 29 additions and 21 deletions
  1. 29
    21
      src/util/niclist.pl

+ 29
- 21
src/util/niclist.pl View File

@@ -19,7 +19,7 @@ use Getopt::Long qw(GetOptions);
19 19
 GetOptions(
20 20
     'help'       => \( my $help     = 0      ),
21 21
     'format=s'   => \( my $format   = 'text' ),
22
-    'sort=s'     => \( my $sort     = 'bus,ipxe_driver,ipxe_name' ),
22
+    'sort=s'     => \( my $sort     = 'bus-,ipxe_driver,ipxe_name' ),
23 23
     'columns=s'  => \( my $columns  = 'bus,vendor_id,device_id,'
24 24
                                     . 'vendor_name,device_name,ipxe_driver,'
25 25
                                     . 'ipxe_name,ipxe_description,file,legacy_api'
@@ -47,26 +47,26 @@ Output formats:
47 47
 Column names (default order):
48 48
     bus, vendor_id, device_id, vendor_name, device_name,
49 49
     ipxe_driver, ipxe_name, ipxe_description, file, legacy_api
50
+
51
+Default sort order (minus at the end means reverse sort):
52
+    bus-, ipxe_driver, ipxe_name
50 53
 EOM
51 54
 
52 55
 # Only load runtime requirements if actually in use
53
-given($format) {
54
-    when( /csv/  ) {
55
-                       eval { require Text::CSV; };
56
-                       die("Please install Text::CSV CPAN module to use this feature.\n")
57
-                           if $@;
58
-                   }
59
-    when( /json/ ) {
60
-                       eval { require JSON; };
61
-                       die("Please install JSON CPAN module to use this feature.\n")
62
-                           if $@;
63
-                   }
64
-    when( /html/ ) {
65
-                       eval { require HTML::Entities; };
66
-                       die("Please install HTML::Entities CPAN module to use this feature.\n")
67
-                           if $@;
68
-                   }
69
-    default        { }
56
+if ( $format =~ /csv/ ) {
57
+    eval { require Text::CSV; };
58
+    die("Please install Text::CSV CPAN module to use this feature.\n")
59
+        if $@;
60
+}
61
+if ( $format =~ /json/ ) {
62
+    eval { require JSON; };
63
+    die("Please install JSON CPAN module to use this feature.\n")
64
+        if $@;
65
+}
66
+if ( $format =~ /html/ ) {
67
+    eval { require HTML::Entities; };
68
+    die("Please install HTML::Entities CPAN module to use this feature.\n")
69
+        if $@;
70 70
 }
71 71
 
72 72
 # Scan source dir and build NIC list
@@ -339,8 +339,16 @@ sub sort_ipxe_nic_list {
339 339
     my @sorted_list = @{ $ipxe_nic_list };
340 340
     while(@sort_column_names) {
341 341
         my $column_name = pop @sort_column_names;
342
-        @sorted_list = sort { ( $a->{$column_name} || "" ) cmp ( $b->{$column_name} || "" ) }
343
-                       @sorted_list;
342
+        my $reverse = substr($column_name, -1) eq '-' ? 1 : 0;  # use reverse order if last character is minus
343
+        $column_name = substr($column_name, 0, -1) if $reverse; # chop of the minus
344
+        if ( $reverse ) {
345
+            @sorted_list = sort { ( $b->{$column_name} || "" ) cmp ( $a->{$column_name} || "" ) }
346
+                           @sorted_list;
347
+        }
348
+        else {
349
+            @sorted_list = sort { ( $a->{$column_name} || "" ) cmp ( $b->{$column_name} || "" ) }
350
+                           @sorted_list;
351
+        }
344 352
     }
345 353
     return \@sorted_list;
346 354
 }
@@ -359,7 +367,7 @@ sub parse_columns_param {
359 367
 sub is_valid_column {
360 368
     my ($name) = @_;
361 369
     my $valid_column_map = {
362
-        map { $_ => 1 }
370
+        map { $_ => 1, $_ . "-" => 1 } # also supports keyword with a - suffix
363 371
         qw(
364 372
            bus file legacy_api
365 373
            ipxe_driver ipxe_name ipxe_description

Loading…
Cancel
Save