OpenSearch-PPLQuery

 view release on metacpan or  search on metacpan

bin/pplquery  view on Meta::CPAN

#!/usr/bin/env perl

# PODNAME: pplquery
# ABSTRACT: Run OpenSearch Piped Processing Language queries from the command line
#
# User documentation lives in PPLQuery.pod at the distribution root and is
# injected at the '## POD ##' placeholder below when the distribution is built.

use v5.36;
use utf8;

use Getopt::Long::Descriptive qw(describe_options);
use OpenSearch::PPLQuery ();
use OpenSearch::PPLQuery::Connection ();

binmode STDOUT, ':encoding(UTF-8)' or die "Cannot configure stdout as UTF-8: $!\n";
binmode STDERR, ':encoding(UTF-8)' or die "Cannot configure stderr as UTF-8: $!\n";
binmode STDIN, ':raw' or die "Cannot configure stdin as raw input: $!\n";

my $exit_status = eval { main(\@ARGV) };
if ($@ ne '') {
    my $message = OpenSearch::PPLQuery::exception_text($@);
    $message =~ s/\s+\z//;
    print STDERR "pplquery: $message\n";
    exit 1;
}
exit $exit_status;

sub main {
    my ($argv) = @_;
    @$argv = map { OpenSearch::PPLQuery::decode_utf8($_, 'command-line argument') } @$argv;
    my %options = (format => 'table', max_width => 0);
    my %cli;
    local @ARGV = @$argv;
    my ($parsed, $usage) = describe_options(
        'Usage: %c [options] FILE|-',
        ['url|u=s' => 'OpenSearch base URL'],
        ['user|U=s' => 'Basic-auth username'],
        ['password-file|p=s' => 'Read the Basic-auth password from a file'],
        ['ca-file=s' => 'CA certificate for HTTPS verification'],
        ['insecure' => 'Disable HTTPS certificate and hostname verification'],
        ['connection-file|c=s' => 'Read one connection header from a file'],
        ['format|f=s' => 'Output format: table, json, or csv'],
        ['max-width|w=i' => 'Truncate table cells to N terminal columns'],
        ['timeout|t=s' => 'HTTP timeout in seconds'],
        ['help' => 'Show this help'],
        {getopt_conf => [qw(no_auto_abbrev no_getopt_compat permute)]},
    );
    $options{help} = $parsed->help;
    for my $name (qw(url user password_file ca_file insecure timeout)) {
        $cli{$name} = $parsed->$name if defined $parsed->$name;
    }
    for my $name (qw(connection_file format max_width)) {
        $options{$name} = $parsed->$name if defined $parsed->$name;
    }
    @$argv = @ARGV;
    if ($options{help}) { print $usage->text; return 0; }
    die "--format must be 'table', 'json', or 'csv', not '" . OpenSearch::PPLQuery::terminal_text($options{format}) . "'\n"
        if $options{format} !~ /\A(?:table|json|csv)\z/;
    die "--max-width must not be negative, but $options{max_width} was given. Use 0 for no limit.\n" if $options{max_width} < 0;
    OpenSearch::PPLQuery::validate_timeout($cli{timeout}, '--timeout') if defined $cli{timeout};

    if (@$argv != 1) {
        die(($argv->@* == 0
            ? "No query file was given. Supply the path to a .ppl file, or '-' to read the query from standard input.\n"
            : 'Only one query file can be given, but ' . scalar($argv->@*) . ' were: '
                . join(', ', map { OpenSearch::PPLQuery::terminal_text($_) } $argv->@*) . "\n")
            . "\n" . $usage->text);
    }
    my ($query, $connection) = OpenSearch::PPLQuery::Connection::resolve_connection(
        \%cli, $options{connection_file}, $argv->[0], \*STDIN,
    );

    my $client = OpenSearch::PPLQuery->new(%$connection);
    my ($result, $error_status) = $client->execute($query);
    if ($options{format} eq 'json') {
        print OpenSearch::PPLQuery::render_json($result);
        return defined($error_status) ? 1 : 0;
    }
    die OpenSearch::PPLQuery::format_error($error_status, $result) if defined $error_status;
    print $options{format} eq 'csv'
        ? OpenSearch::PPLQuery::render_csv($result)
        : OpenSearch::PPLQuery::render_table($result, max_width => $options{max_width});
    return 0;
}

__END__


=pod

=encoding utf8

=head1 NAME



( run in 0.956 second using v1.01-cache-2.11-cpan-d80b1682f3f )