tabularize auto-pager
[hband-tools.git] / admin-tools / nginx_conftree.pl
blobab00b56fb6adc552639498174bd637828df7419b
1 #!/usr/bin/env perl
3 use Data::Dumper;
4 use Getopt::Long;
6 $output = "plain";
9 if($ENV{'FCGI_ROLE'}) {
10 print "Content-Type: text/plain\r\n\r\n";
14 GetOptions(
15 'o|output=s' => \$output,
16 'f|file=s' => \$nginx_conf,
18 or die "Usage: $0 [-f config_file] [-o <plain|dot|merge>]\n";
20 undef $/;
23 if(not $nginx_conf) {
24 open $fh, '-|', "nginx -V 2>&1";
25 $_ = <$fh>;
26 close $fh;
28 /--conf-path=(\S+)/ and $nginx_conf = $1 or die "No '--conf-path' compile option found for nginx.\n";
31 ($nginx_conf_root) = ($nginx_conf =~ /^(.*)\/+[^\/]+$/);
34 sub readconffile {
35 my $filespec = shift;
36 my $indent = shift;
37 my $caller = shift;
39 $filespec = "$nginx_conf_root/$filespec" if $filespec !~ /^\//;
41 for my $file (glob $filespec) {
42 my ($rel_file) = $file;
43 $rel_file =~ s/^\Q$nginx_conf_root\E\/+//;
45 if($output eq "plain") {
46 printf "%s%s\n", " "x$indent, $rel_file;
48 if($output eq "dot" and $caller) {
49 printf "\"%s\" -> \"%s\";\n", $caller, $rel_file;
52 open my $fh, '<', $file or warn "$file: $!\n";
53 my $conf = <$fh>;
54 close $fh;
56 if($output eq "merge") {
57 print "\n#!", "#" x $indent, " file: $file\n";
60 while(1) {
61 if($conf =~ /(^|;)\s*include\s+(\S+?)\s*;/sm) {
62 if($output eq "merge") {
63 print $`.$1;
66 my $child = $2;
67 $conf = $';
68 readconffile($child, $indent+1, $rel_file);
70 else {
71 if($output eq "merge") {
72 print $conf;
74 last;
78 if($output eq "merge") {
79 print "\n#!", "#" x $indent, " end of file: $file\n";
84 if($output eq "dot") {
85 print "
86 digraph \"nginx \" {
87 rankdir=LR;
88 concentrate=true;
92 readconffile($nginx_conf, 0);
94 if($output eq "dot") {
95 print "