From 0f6e2fa51871b05b91dfc286a582ae025cc8c51f Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Tue, 19 Jan 2010 14:15:17 -0800 Subject: Restore priority comments Also sort the names when multiple values have same priority. --- scripts/priority.pl | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/scripts/priority.pl b/scripts/priority.pl index 3f5804f..4124987 100644 --- a/scripts/priority.pl +++ b/scripts/priority.pl @@ -8,43 +8,60 @@ use strict; use warnings; use File::Find; -my %pri; +my %priorities; +# Open node file and extract priority and comment if any sub get_priority { open( my $f, '<', $_ ) or return; my $priority; + my $comment; while (<$f>) { chomp; next unless m/^priority:\s(\d+)/; $priority = $1; + + $comment = $1 if (/#(.*)$/); + last; } close $f; - return $priority; + return ( $priority, $comment ); } +# Called by find and returns true iff +# file is named node.def +# file contains priority tag +# Side effect: tores resulting line in $priorities hash for display sub wanted { return unless ( $_ eq 'node.def' ); - my $p = get_priority($File::Find::name); - return unless $p; + my ( $priority, $comment ) = get_priority($File::Find::name); + return unless $priority; my $dir = $File::Find::dir; $dir =~ s/^.*\/templates\///; - push @{ $pri{$p} }, $dir; + $dir .= " #" . $comment + if $comment; + + # append line to list of entries with same priority + push @{ $priorities{$priority} }, $dir; return 1; } +# main program my $cfgdir = '/opt/vyatta/share/vyatta-cfg/templates'; die "$cfgdir does not exist!" unless -d $cfgdir; + +# walk config file tree find( \&wanted, $cfgdir ); -foreach my $key ( sort { $a <=> $b } keys %pri ) { - my @a = @{ $pri{$key} }; - foreach my $val (@a) { +# display resulting priorities +foreach my $key ( sort { $a <=> $b } keys %priorities ) { + my @a = @{ $priorities{$key} }; + foreach my $val ( sort @a ) { print $key, " ", $val, "\n"; } } -- cgit v1.2.3