6 # PODNAME: wtscript2json
7 # ABSTRACT: convert read configuration into JSON
11 use File
::Slurp
qw(write_file);
14 use List
::MoreUtils
qw(zip);
15 use Params
::Util
qw(_ARRAY);
18 use WWW
::Mechanize
::Script
::Util
qw(:ALL);
19 use WWW
::Mechanize
::Script
;
21 our $VERSION = '0.100';
25 "output-pattern" => []
28 "input-files=s@" => $opts{"input-files"},
29 "output-files=s@" => $opts{"output-files"},
30 "output-pattern=s{2}" => $opts{"output-pattern"},
34 GetOptions
( \
%opts, @options ) or pod2usage
(2);
37 @
{ $opts{"input-files"} } or delete $opts{"input-files"};
38 @
{ $opts{"output-files"} } or delete $opts{"output-files"};
39 @
{ $opts{"output-pattern"} } or delete $opts{"output-pattern"};
42 defined( $opts{help
} )
50 defined( $opts{usage
} ) and $opts{usage
} and pod2usage
(1);
51 opt_required_all
( \
%opts, qw(input-files) );
52 opt_exclusive
( \
%opts, qw(output-files output-pattern) );
53 opt_required_one
( \
%opts, qw(output-files output-pattern) );
55 _ARRAY
( $opts{"input-files"} )
56 and $opts{"input-files"} = [ split( ",", join( ",", @
{ $opts{"input-files"} } ) ) ];
57 _ARRAY
( $opts{"output-files"} )
58 and $opts{"output-files"} = [ split( ",", join( ",", @
{ $opts{"output-files"} } ) ) ];
60 _ARRAY
( $opts{"input-files"} )
61 and _ARRAY
( $opts{"output-files"} )
62 and scalar( @
{ $opts{"input-files"} } ) != scalar( @
{ $opts{"output-files"} } )
65 -message
=> "Count of --input-files and --output-files doesn't match",
71 _ARRAY
( $opts{"output-files"} )
72 ? zip
( @
{ $opts{"input-files"} }, @
{ $opts{"output-files"} } )
74 my %cfg = load_config
();
76 my $coder = JSON
->new();
77 _ARRAY
( $cfg{wtscript_extensions
} )
78 and Config
::Any
::WTScript
->extensions( @
{ $cfg{wtscript_extensions
} } );
79 foreach my $filename ( @
{ $opts{"input-files"} } )
81 my @script_files = find_scripts
( \
%cfg, $filename );
82 my $scripts = Config
::Any
->load_files(
84 files
=> [@script_files],
89 if ( $opts{"output-files"} )
91 @script_files = keys %{$scripts};
92 scalar(@script_files) > 1
95 -message
=> "filename $filename is ambigious: " . join( ", ", @script_files ),
99 scalar(@script_files) < 1
100 and next; # file not found or not parsable ...
101 # merge into default and previous loaded config ...
102 my $json = $coder->pretty->encode( $scripts->{ $script_files[0] } );
103 write_file
( $in2out{$filename}, $json );
107 while ( my ( $script_file, $script ) = each(%$scripts) )
109 my $json = $coder->pretty->encode($script);
110 ( my $target = $script_file ) =~
111 s/$opts{"output-pattern"}->[0]/$opts{"output-pattern"}->[1]/;
112 write_file
( $target, $json );
121 wtscript2json is a helper to convert WebTest Script files into a modern
122 format. Currently the format is hard-coded to JSON.
124 To avoid conflicts or duplicates running check_web(2), it's recommended to
125 use different C<script_dirs> in special configuration files for check_web(2)
126 and wtscript2json (check-web2.json vs. wtscript2json.json vs. check_web.json
127 as common configuration file for both). See
128 L<WWW::Mechanize::Script::Util/load_config> for further information.
132 # search in $config->{script_dirs} for any loadable script config
133 $ wtscript2json --input-files service1/script1,service1/script2,service2/cool_script \
134 --output-files /opt/new_checks/service1/script1.json \
135 --output-files /opt/new_checks/service1/script2.json \
136 --output-files /opt/new_checks/service2/cool_script.json
138 # in-location conversion ...
139 $ find . -name "*splunk*"
140 ./check_web_config/log/test_splunk_1.txt
141 ./check_web_config/log/test_splunk_2.txt
142 ./check_web_config/log/test_splunk_3.txt
143 ./check_web_config/log/test_splunk_4.txt
144 $ wtscript2json --input-files log/test_splunk[1-4] --output-pattern .txt .json
145 $ find . -name "*splunk*"
146 $ find . -name "*splunk*"
147 ./check_web_config/log/test_splunk_1.txt
148 ./check_web_config/log/test_splunk_2.txt
149 ./check_web_config/log/test_splunk_3.txt
150 ./check_web_config/log/test_splunk_4.txt
151 ./check_web_config/log/test_splunk_4.json
152 ./check_web_config/log/test_splunk_3.json
153 ./check_web_config/log/test_splunk_2.json
154 ./check_web_config/log/test_splunk_1.json