Skip to content

Instantly share code, notes, and snippets.

@AndyA
Created November 17, 2014 16:06
Show Gist options
  • Select an option

  • Save AndyA/66ba718af408b05ebdb4 to your computer and use it in GitHub Desktop.

Select an option

Save AndyA/66ba718af408b05ebdb4 to your computer and use it in GitHub Desktop.
$ cat /etc/sphinxsearch/sphinx.conf
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename qw( dirname );
use File::Path;
use constant ROOT => 'sphinx.static.conf';
my @PATH = ( dirname($0) );
print include( need_file(ROOT) ), "\n";
sub include {
my $name = shift;
return '' unless defined $name;
open my $fh, '<', $name or die "Can't read $name: $!\n";
my @line = ();
while (<$fh>) {
chomp;
s/\{\{(\w+)\s+(.+)\}\}/expand($1, $2)/eg;
push @line, $_;
}
return join "\n", @line;
}
sub oneline {
my $name = shift;
my $conf = include($name);
$conf =~ s/\s+$//;
$conf =~ s/\n/ \\\n/mg;
return $conf;
}
sub expand {
my ( $cmd, $tail ) = @_;
my %cmd = (
include => sub { return include( need_file( $_[0] ) ) },
oneline => sub { return oneline( need_file( $_[0] ) ) },
optional => sub { return include( find_file( $_[0] ) ) },
path => sub { unshift @PATH, @_; return '' },
);
die "Unknown command: $cmd\n" unless exists $cmd{$cmd};
return $cmd{$cmd}->($tail);
}
sub need_file {
my $name = shift;
my $try = find_file($name);
die "Can't find $name\n" unless defined $try;
return $try;
}
sub find_file {
my $name = shift;
return $name if File::Spec->file_name_is_absolute($name);
for my $path (@PATH) {
my $try = File::Spec->catfile( $path, $name );
return $try if -f $try;
}
return;
}
# vim:ts=2:sw=2:sts=2:et:ft=perl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment