modify default extensions for web-test scripts, allow override and take from config
[WWW-Mechanize-Script.git] / bin / wtscript2json.pl
blobcde7e2978ad7ee3e3f9887adcd3531848f2ec981
1 #! perl
3 use strict;
4 use warnings;
6 use v5.10.1;
8 use File::Slurp qw(write_file);
9 use Getopt::Long;
10 use JSON ();
11 use List::MoreUtils qw(zip);
12 use Params::Util qw(_ARRAY);
13 use Pod::Usage;
15 use WWW::Mechanize::Script::Util qw(:ALL);
16 use WWW::Mechanize::Script;
18 my $VERSION = 0.001;
19 my %opts;
20 my @options = ( "input-files=s@", "output-files=s@", "output-pattern=s{2}", "help|h", "usage|?" );
22 GetOptions( \%opts, @options ) or pod2usage(2);
24 defined( $opts{help} )
25 and $opts{help}
26 and pod2usage(
28 -verbose => 2,
29 -exitval => 0
32 defined( $opts{usage} ) and $opts{usage} and pod2usage(1);
33 opt_required_all( \%opts, qw(input-files) );
34 opt_exclusive( \%opts, qw(output-files output-pattern) );
35 opt_required_one( \%opts, qw(output-files output-pattern) );
36 _ARRAY( $opts{"input-files"} )
37 and _ARRAY( $opts{"output-files"} )
38 and scalar( @{ $opts{"input-files"} } ) != scalar( @{ $opts{"output-files"} } )
39 and pod2usage(
41 -message => "Count of --input-files and --output-files doesn't match",
42 -exitval => 1
46 my %in2out =
47 _ARRAY( $opts{"input-files"} )
48 ? zip( @{ $opts{"input-files"} }, @{ $opts{"output-files"} } )
49 : map {
50 my $f = $_;
51 $f =~ s/$opts{"output-pattern"}->[0]/$opts{"output-pattern"}->[1]/;
52 ( $_, $f );
53 } @{ $opts{"input-files"} };
54 my %cfg = load_config();
56 my $coder = JSON->new();
57 _ARRAY( $cfg{wtscript_extensions} )
58 and Config::Any::WTScript->extensions( @{ $cfg{wtscript_extensions} } );
59 foreach my $filename ( @{ $opts{"input-files"} } )
61 my @script_files = find_scripts( \%cfg, $filename );
62 my $scripts = Config::Any->load_files(
64 files => [@script_files],
65 use_ext => 1,
66 flatten_to_hash => 1,
69 @script_files = keys %{$scripts};
70 scalar(@script_files) > 1
71 and pod2usage(
73 -message => "filename $filename is ambigious: " . join( ", ", @script_files ),
74 -exitval => 1
77 scalar(@script_files) < 1
78 and next; # file not found or not parsable ...
79 # merge into default and previous loaded config ...
80 my $json = $coder->pretty->encode( $scripts->{ $script_files[0] } );
81 write_file( $in2out{$filename}, $json );
84 __END__
86 =head1 NAME
88 check_web2 - allows checking of website according to configured specifications
90 =head1 DESCRIPTION
92 check_web2 is intended to be used to check web-sites according a configuration.
93 The configuration covers the request configuration (including agent part) and
94 check configuration to specify check parameters.
96 See C<WWW::Mechanize::Script> for details about the configuration options.
98 =head2 HISTORY
100 This script is created as successor of an check_web script of a nagios setup
101 based on HTTP::WebCheck. This module isn't longer maintained, so decision
102 was made to create a new environment simulating the old one basing on
103 WWW::Mechanize.
105 =head1 SYNOPSIS
107 $ check_web2 --file domain1/site1.json
108 $ check_web2 --file domain2/site1.yml
109 # for compatibility
110 $ check_web2 --file domain1/site2.wts
112 =head1 AUTHOR
114 Jens Rehsack, C<< <rehsack at cpan.org> >>
116 =head1 BUGS
118 Please report any bugs or feature requests to C<bug-www-mechanize-script at rt.cpan.org>, or through
119 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Mechanize-Script>. I will be notified, and then you'll
120 automatically be notified of progress on your bug as I make changes.
122 =head1 SUPPORT
124 You can find documentation for this module with the perldoc command.
126 perldoc WWW:Mechanize::Script
128 You can also look for information at:
130 =over 4
132 =item * RT: CPAN's request tracker (report bugs here)
134 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Mechanize-Script>
136 =item * AnnoCPAN: Annotated CPAN documentation
138 L<http://annocpan.org/dist/WWW-Mechanize-Script>
140 =item * CPAN Ratings
142 L<http://cpanratings.perl.org/d/WWW-Mechanize-Script>
144 =item * Search CPAN
146 L<http://search.cpan.org/dist/WWW-Mechanize-Script/>
148 =back
150 =head1 ACKNOWLEDGEMENTS
152 =head1 LICENSE AND COPYRIGHT
154 Copyright 2012 Jens Rehsack.
156 This program is free software; you can redistribute it and/or modify it
157 under the terms of either: the GNU General Public License as published
158 by the Free Software Foundation; or the Artistic License.
160 See http://dev.perl.org/licenses/ for more information.
162 =cut