Skip to content

Commit be4c965

Browse files
committed
adding better json outputfile option
1 parent db326d9 commit be4c965

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

mysqltuner.pl

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6142,7 +6142,6 @@ sub file2string {
61426142
$templateModel = file2string( $opt{'template'} );
61436143
}
61446144
else {
6145-
61466145
# DEFAULT REPORT TEMPLATE
61476146
$templateModel = <<'END_TEMPLATE';
61486147
<!DOCTYPE html>
@@ -6164,21 +6163,17 @@ sub file2string {
61646163
}
61656164

61666165
sub dump_result {
6167-
if ( $opt{'debug'} ) {
6168-
debugprint Dumper( \%result );
6169-
}
6170-
6166+
debugprint Dumper( \%result ) if ( $opt{'debug'} );
61716167
debugprint "HTML REPORT: $opt{'reportfile'}";
61726168

61736169
if ( $opt{'reportfile'} ne 0 ) {
61746170
eval { require Text::Template };
61756171
if ($@) {
61766172
badprint "Text::Template Module is needed.";
6177-
exit 1;
6173+
die "Text::Template Module is needed.";
61786174
}
61796175

61806176
my $vars = { 'data' => Dumper( \%result ) };
6181-
61826177
my $template;
61836178
{
61846179
no warnings 'once';
@@ -6188,21 +6183,32 @@ sub dump_result {
61886183
SOURCE => $templateModel
61896184
) or die "Couldn't construct template: $Text::Template::ERROR";
61906185
}
6186+
61916187
open my $fh, q(>), $opt{'reportfile'}
61926188
or die
6193-
"Unable to open $opt{'reportfile'} in write mode. please check permissions for this file or directory";
6189+
"Unable to open $opt{'reportfile'} in write mode. please check permissions for this file or directory";
61946190
$template->fill_in( HASH => $vars, OUTPUT => $fh );
61956191
close $fh;
61966192
}
6193+
61976194
if ( $opt{'json'} ne 0 ) {
61986195
eval { require JSON };
6199-
if ($@) {
6196+
if ($@) {
62006197
print "$bad JSON Module is needed.\n";
6201-
exit 1;
6198+
return 1;
62026199
}
6200+
62036201
my $json = JSON->new->allow_nonref;
62046202
print $json->utf8(1)->pretty( ( $opt{'prettyjson'} ? 1 : 0 ) )
6205-
->encode( \%result );
6203+
->encode( \%result ) unless ( $opt{'silent'} );
6204+
6205+
if ( $opt{'outputfile'} ne 0 ) {
6206+
open my $fh, q(>), $opt{'outputfile'}
6207+
or die
6208+
"Unable to open $opt{'outputfile'} in write mode. please check permissions for this file or directory";
6209+
print $fh $json->utf8(1)->pretty( ( $opt{'prettyjson'} ? 1 : 0 ) );
6210+
close $fh;
6211+
}
62066212
}
62076213
}
62086214

@@ -6212,9 +6218,7 @@ sub which {
62126218
my @path_array = split /:/, $ENV{'PATH'};
62136219

62146220
for my $path (@path_array) {
6215-
if ( -x "$path/$prog_name" ) {
6216-
return "$path/$prog_name";
6217-
}
6221+
return "$path/$prog_name" if ( -x "$path/$prog_name" );
62186222
}
62196223

62206224
return 0;

0 commit comments

Comments
 (0)