#!/usr/bin/env perl
use 5.006;
use strict;
use warnings;

use Net::Plywood::Simple;
use Getopt::Long;
use Pod::Usage;
use Try::Tiny;

my ($file, $key, $path, $scheme, $host, $port, $stdout, $help, $man);
GetOptions(
	'file=s'   => \$file,
	'key=s'    => \$key,
	'path=s'   => \$path,
	'scheme=s' => \$scheme,
	'host=s'   => \$host,
	'port=s'   => \$port,
	'stdout|?' => \$stdout,
	'help|?'   => \$help,
	man        => \$man,
) or die("Error in command line arguments\n");
pod2usage(1) if $help;
pod2usage(-exitval => 0, -verbose => 2) if $man;
pod2usage(1) unless ($file && $key && $host && $port);

my $plywood = Net::Plywood::Simple->new(
	scheme => ($scheme || 'http'),
	host   => $host,
	port   => $port,
);

my @path     = $path ? split('/', $path) : '';
my $response = $plywood->get($key, @path);
my $content  = $response->{content};

if ($stdout) {
	print "$content\n";
} else {
	open(my $json_fh, '>', $file)
		|| die("Can't open $file: $!\n");
	print $json_fh $content;
	close($json_fh);
}

1;

__END__

=USAGE

USAGE:

	plywood-simple-get [options] [arguments]

Examples:

	plywood-simple-get --file test.json --key test123 --host localhost --port 8080 --path node/path/in/tree

