Skip to content

Commit eda9f4d

Browse files
robinsmidsrodmcb30
authored andcommittedMar 21, 2018
[util] Support reversed sort ordering when generating NIC list
Signed-off-by: Michael Brown <mcb30@ipxe.org>
1 parent bc85368 commit eda9f4d

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed
 

‎src/util/niclist.pl

+29-21
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
GetOptions(
2020
'help' => \( my $help = 0 ),
2121
'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' ),
2323
'columns=s' => \( my $columns = 'bus,vendor_id,device_id,'
2424
. 'vendor_name,device_name,ipxe_driver,'
2525
. 'ipxe_name,ipxe_description,file,legacy_api'
@@ -47,26 +47,26 @@
4747
Column names (default order):
4848
bus, vendor_id, device_id, vendor_name, device_name,
4949
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
5053
EOM
5154

5255
# 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 $@;
7070
}
7171

7272
# Scan source dir and build NIC list
@@ -339,8 +339,16 @@ sub sort_ipxe_nic_list {
339339
my @sorted_list = @{ $ipxe_nic_list };
340340
while(@sort_column_names) {
341341
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+
}
344352
}
345353
return \@sorted_list;
346354
}
@@ -359,7 +367,7 @@ sub parse_columns_param {
359367
sub is_valid_column {
360368
my ($name) = @_;
361369
my $valid_column_map = {
362-
map { $_ => 1 }
370+
map { $_ => 1, $_ . "-" => 1 } # also supports keyword with a - suffix
363371
qw(
364372
bus file legacy_api
365373
ipxe_driver ipxe_name ipxe_description

0 commit comments

Comments
 (0)