derobert

Config manager: tools/combine-config

Mar 13th, 2015
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.74 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use warnings qw(all);
  3. use strict;
  4.  
  5. use File::Copy qw(copy);
  6. use File::Spec;
  7.  
  8. my $host = shift;
  9. my $host_re = qr/$host/i;
  10. my %files;
  11.  
  12. foreach my $path (@ARGV) {
  13.     my (undef, undef, $name) = File::Spec->splitpath($path);
  14.     $name =~ /^(.+)\.([^.]+)$/ or die "Bad file name: $name";
  15.     my ($group, $host) = ($1, $2);
  16.  
  17.     if ($host =~ /^default$/i) {
  18.         $files{$group}{default} = $path;
  19.     } elsif ($host =~ $host_re) {
  20.         $files{$group}{host} = $path;
  21.     }
  22. }
  23.  
  24. foreach my $group (sort keys %files) {
  25.     # must run with 5.8, no // operator...
  26.     my $path = defined $files{$group}{host}
  27.         ? $files{$group}{host}
  28.         : $files{$group}{default};
  29.     defined $path or die "BUG: $group applies to us, but doesn't";
  30.  
  31.     copy($path, *STDOUT);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment