#!/usr/bin/env perl
use strict;
use warnings;
use JSON::Path 0.420;
use Data::Printer 0.40 { output => 'stdout' };
use Getopt::Long;
use Pod::Usage;

my $man  = 0;
my $help = 0;
my $json_path_exp;

GetOptions( 'help|?' => \$help, man => \$man, 'exp=s' => \$json_path_exp )
  or pod2usage(2);
pod2usage(1) if $help;
pod2usage( -exitval => 0, -verbose => 2 ) if $man;
pod2usage(1) unless ( defined($json_path_exp) );

die "Must pass a non empty JSONPath expression to -exp argument.\n"
  if ( $json_path_exp eq '' );

my $json_data;

{
    local $/;
    $json_data = <>;
}

my $p      = JSON::Path->new($json_path_exp);
my $action = ref( $p->value($json_data) );

my %actions = (
    'ARRAY'  => sub { p @{ $p->value(shift) } },
    'HASH'   => sub { p %{ $p->value(shift) } },
    'SCALAR' => sub { p ${ $p->value(shift) } },
);

die "Expression '$json_path_exp' did not bring any result!\n"
  if ( $action eq '' );

die "Don't know how to use reference '$action'\n!"
  unless ( exists( $actions{$action} ) );

$actions{$action}->($json_data);

__END__

=pod

=head1 NAME

psonpath: a CLI that parses JSON data with JSONPath

=head1 SYNOPSIS

  <data> | psonpath -exp <JSONPath expression>

  Options:
    -exp             a JSONPath expression (required)
    -help            brief help message
    -man             full documentation

=head1 DESCRIPTION

Reads JSON data from STDIN and prints to STDOUT the result of the JSONPath
filter.

If the expression works, data will be printed out in a formated and
colourfull output to STDOUT.

Here is an example:

  $ cat ./aws/venv/lib/python3.7/site-packages/awscli-1.16.201.dist-info/metadata.json
  {"license": "Apache License 2.0", "name": "awscli", "metadata_version": "2.0", "generator": "bdist_wheel (0.24.0)", "summary": "Universal Command Line Environment for AWS.", "run_requires": [{"environment": "python_version!=\"2.6\"", "requires": ["PyYAML>=3.10,<=5.1"]}, {"requires": ["botocore==1.12.191", "colorama>=0.2.5,<=0.3.9", "docutils>=0.10", "rsa>=3.1.2,<=3.5.0", "s3transfer>=0.2.0,<0.3.0"]}, {"environment": "python_version==\"2.6\"", "requires": ["PyYAML>=3.10,<=3.13", "argparse>=1.1"]}], "version": "1.16.201", "extensions": {"python.details": {"project_urls": {"Home": "http://aws.amazon.com/cli/"}, "document_names": {"description": "DESCRIPTION.rst"}, "contacts": [{"role": "author", "name": "Amazon Web Services"}]}}, "classifiers": ["Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "Intended Audience :: System Administrators", "Natural Language :: English", "License :: OSI Approved :: Apache Software License", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7"], "extras": []}
  $ cat somefile.json | psonpath -exp '$..run_requires.[1]'
  {
      requires   [
          [0] "botocore==1.12.191",
          [1] "colorama>=0.2.5,<=0.3.9",
          [2] "docutils>=0.10",
          [3] "rsa>=3.1.2,<=3.5.0",
          [4] "s3transfer>=0.2.0,<0.3.0"
      ]
  }

If the result of the applied JSONPath expression is not valid, it finished with
and error message and exit code different from zero.

=head1 SEE ALSO

=over

=item *

L<Psonpath>: additional documentation

=back

=head1 AUTHOR

Alceu Rodrigues de Freitas Junior, E<lt>glasswalk3r@yahoo.com.brE<gt>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2019 of Alceu Rodrigues de Freitas Junior,
E<lt>glasswalk3r@yahoo.com.brE<gt>

This file is part of psonpath project.

psonpath is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

psonpath is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with psonpath.  If not, see L<http://www.gnu.org/licenses/>.

=cut
