Better organize execution flow

This commit is contained in:
Brian Buchalter 2013-01-18 11:19:56 -04:00
parent 273d2a93bc
commit c8382eabb6

View File

@ -52,51 +52,51 @@ my $json_response;
eval { eval {
$json_response = decode_json($response->content); $json_response = decode_json($response->content);
print "JSON repsonse decoded successfully." print "JSON repsonse decoded successfully.";
$status = EXIT_OK;
if ($opts{d}) {
if ( -e $opts{d}) {
my $hash_import = do $opts{d};
my %attr_check = %{$hash_import};
my @errors;
for my $key (sort keys %attr_check) {
for my $attr (sort keys %{$attr_check{$key}}) {
my $have = $json_response->{products}{$key}{now}{$attr};
my $expect = $attr_check{$key}{$attr};
push @errors, "For key $key, attribute $attr, expected '$expect', but got '$have'"
unless $have eq $expect;
}
}
if (@errors) {
print "Errors:\n", map { "$_\n" } @errors;
$status = EXIT_CRITICAL;
}
else {
print "Found expected content.";
$status = EXIT_OK;
}
}
else {
print "Unable to find data file $opts{d}";
$status = EXIT_UNKNOWN;
}
}
exit $status;
} or do { } or do {
print "Unable to decode JSON, invalid response?"; print "Unable to decode JSON, invalid response?";
exit EXIT_CRITICAL; exit EXIT_CRITICAL;
}; };
$status = EXIT_OK;
if ($opts{d}) {
if ( -e $opts{d}) {
my $hash_import = do $opts{d};
my %attr_check = %{$hash_import};
my @errors;
for my $key (sort keys %attr_check) {
for my $attr (sort keys %{$attr_check{$key}}) {
my $have = $json_response->{products}{$key}{now}{$attr};
my $expect = $attr_check{$key}{$attr};
push @errors, "For key $key, attribute $attr, expected '$expect', but got '$have'"
unless $have eq $expect;
}
}
if (@errors) {
print "Errors:\n", map { "$_\n" } @errors;
$status = EXIT_CRITICAL;
}
else {
print "Found expected content.";
$status = EXIT_OK;
}
}
else {
print "Unable to find data file $opts{d}";
$status = EXIT_UNKNOWN;
}
}
exit $status;
sub HELP_MESSAGE sub HELP_MESSAGE
{ {
print <<EOHELP print <<EOHELP