derobert icon

Config manager: tools/combine-config

derobert | PRO | 03/13/15 11:57:12 PM UTC | 0 ⭐ | 445 👁️ | Never ⏰ | []
Perl |

740 B

|

None

|

0 👍

/

0 👎

#!/usr/bin/perl
use warnings qw(all);
use strict;
 
use File::Copy qw(copy);
use File::Spec;
 
my $host = shift;
my $host_re = qr/$host/i;
my %files;
 
foreach my $path (@ARGV) {
    my (undef, undef, $name) = File::Spec->splitpath($path);
    $name =~ /^(.+)\.([^.]+)$/ or die "Bad file name: $name";
    my ($group, $host) = ($1, $2);
 
    if ($host =~ /^default$/i) {
        $files{$group}{default} = $path;
    } elsif ($host =~ $host_re) {
        $files{$group}{host} = $path;
    }
}
 
foreach my $group (sort keys %files) {
    # must run with 5.8, no // operator...
    my $path = defined $files{$group}{host}
        ? $files{$group}{host}
        : $files{$group}{default};
    defined $path or die "BUG: $group applies to us, but doesn't";
 
    copy($path, *STDOUT);
}

Comments