-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdump-nconf
executable file
·86 lines (55 loc) · 1.8 KB
/
dump-nconf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl
=head1 NAME
dump-nconf - Dump resulting configuration file data structure
=head1 SYNOPSIS
dump-nconf /path/to/file.nconf [--mode=<mode>]
Supported modes:
=over 10
=item neat
Use Config::Neat::Render to print out the data structure (default mode)
=item dumper
Print out the data structure using Data::Dumper
=item json
Print out the data structure as JSON
=back
=head1 DESCRIPTION
This script will read and parse the provided configuration file,
apply its inheritance rules, and dump the resulting data structure
using Config::Neat::Render, Data::Dumper or JSON methods.
This is useful to understand the data structure behind you configuration
files in general and debug inheritance rules in particular.
=head1 AUTHOR
Igor Afanasyev <[email protected]>
=cut
use strict;
use FindBin;
use lib "$FindBin::Bin/../lib";
use Config::Neat::Inheritable;
use Config::Neat::Render;
use Data::Dumper;
use Getopt::Long;
use JSON -convert_blessed_universally;
use Pod::Usage;
pod2usage(-verbose => 0) unless $ARGV[0];
my $mode;
GetOptions(
'mode=s' => \$mode,
) or error("Failed to parse some command-line parameters.");
$mode = 'neat' unless $mode;
error("Unsupported mode '$mode'") unless $mode =~ m/^(neat|dumper|json)$/;
error("Multiple configuration files not allowed") unless $#ARGV == 0;
my $cfg = Config::Neat::Inheritable->new();
my $data = $cfg->parse_file($ARGV[0]);
my $r = Config::Neat::Render->new();
if ($mode eq 'neat') {
print $r->render($data);
} elsif ($mode eq 'dumper') {
print Dumper($data);
} elsif ($mode eq 'json') {
print JSON->new->convert_blessed->indent->encode($data);
}
sub error {
my ($message, $exitstatus) = @_;
$exitstatus = 1 unless defined $exitstatus;
pod2usage(-message => $message."\n", -verbose => 0, -exitstatus => $exitstatus);
}