| 1 | #!/usr/bin/env perl | ||||||||||
| 2 | |||||||||||
| 3 | # PODNAME: pplquery | ||||||||||
| 4 | # ABSTRACT: Run OpenSearch Piped Processing Language queries from the command line | ||||||||||
| 5 | # | ||||||||||
| 6 | # User documentation lives in PPLQuery.pod at the distribution root and is | ||||||||||
| 7 | # injected at the '## POD ##' placeholder below when the distribution is built. | ||||||||||
| 8 | |||||||||||
| 9 | 24 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 24 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 24 | ▶ | use Getopt::Long::Descriptive qw(describe_options); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 24 | ▶ | use OpenSearch::PPLQuery (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 24 | ▶ | use OpenSearch::PPLQuery::Connection (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 24 | ▶ | binmode STDOUT, ':encoding(UTF-8)' or die "Cannot configure stdout as UTF-8: $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 24 | ▶ | binmode STDERR, ':encoding(UTF-8)' or die "Cannot configure stderr as UTF-8: $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 24 | ▶ | binmode STDIN, ':raw' or die "Cannot configure stdin as raw input: $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | |||||||||||
| 20 | 24 | my $exit_status = eval { main(\@ARGV) }; | |||||||||
| 21 | 24 | ▶ | if ($@ ne '') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 10 | my $message = OpenSearch::PPLQuery::exception_text($@); | |||||||||
| 23 | 10 | $message =~ s/\s+\z//; | |||||||||
| 24 | 10 | print STDERR "pplquery: $message\n"; | |||||||||
| 25 | 10 | exit 1; | |||||||||
| 26 | } | ||||||||||
| 27 | 14 | exit $exit_status; | |||||||||
| 28 | |||||||||||
| 29 | 24 | ▶ | sub main { | ||||||||
|
Subroutine
main: called CC=15 SCAR=27.9
| |||||||||||
| 30 | 24 | my ($argv) = @_; | |||||||||
| 31 | 24 | @$argv = map { OpenSearch::PPLQuery::decode_utf8($_, 'command-line argument') } @$argv; | |||||||||
| 32 | 24 | my %options = (format => 'table', max_width => 0); | |||||||||
| 33 | 24 | my %cli; | |||||||||
| 34 | 24 | local @ARGV = @$argv; | |||||||||
| 35 | 24 | my ($parsed, $usage) = describe_options( | |||||||||
| 36 | 'Usage: %c [options] FILE|-', | ||||||||||
| 37 | ['url|u=s' => 'OpenSearch base URL'], | ||||||||||
| 38 | ['user|U=s' => 'Basic-auth username'], | ||||||||||
| 39 | ['password-file|p=s' => 'Read the Basic-auth password from a file'], | ||||||||||
| 40 | ['ca-file=s' => 'CA certificate for HTTPS verification'], | ||||||||||
| 41 | ['insecure' => 'Disable HTTPS certificate and hostname verification'], | ||||||||||
| 42 | ['connection-file|c=s' => 'Read one connection header from a file'], | ||||||||||
| 43 | ['format|f=s' => 'Output format: table, json, or csv'], | ||||||||||
| 44 | ['max-width|w=i' => 'Truncate table cells to N terminal columns'], | ||||||||||
| 45 | ['timeout|t=s' => 'HTTP timeout in seconds'], | ||||||||||
| 46 | ['help' => 'Show this help'], | ||||||||||
| 47 | {getopt_conf => [qw(no_auto_abbrev no_getopt_compat permute)]}, | ||||||||||
| 48 | ); | ||||||||||
| 49 | 21 | $options{help} = $parsed->help; | |||||||||
| 50 | 21 | for my $name (qw(url user password_file ca_file insecure timeout)) { | |||||||||
| 51 | 126 | ▶ | $cli{$name} = $parsed->$name if defined $parsed->$name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | } | ||||||||||
| 53 | 21 | for my $name (qw(connection_file format max_width)) { | |||||||||
| 54 | 63 | ▶ | $options{$name} = $parsed->$name if defined $parsed->$name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | } | ||||||||||
| 56 | 21 | @$argv = @ARGV; | |||||||||
| 57 | 0 | ▶ | if ($options{help}) { print $usage->text; return 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | die "--format must be 'table', 'json', or 'csv', not '" . OpenSearch::PPLQuery::terminal_text($options{format}) . "'\n" | ||||||||||
| 59 | 21 | ▶ | if $options{format} !~ /\A(?:table|json|csv)\z/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 21 | ▶ | die "--max-width must not be negative, but $options{max_width} was given. Use 0 for no limit.\n" if $options{max_width} < 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 21 | ▶ | OpenSearch::PPLQuery::validate_timeout($cli{timeout}, '--timeout') if defined $cli{timeout}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 21 | ▶ | if (@$argv != 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | die(($argv->@* == 0 | ||||||||||
| 65 | ? "No query file was given. Supply the path to a .ppl file, or '-' to read the query from standard input.\n" | ||||||||||
| 66 | : 'Only one query file can be given, but ' . scalar($argv->@*) . ' were: ' | ||||||||||
| 67 | 0 | ▶ | . join(', ', map { OpenSearch::PPLQuery::terminal_text($_) } $argv->@*) . "\n") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | . "\n" . $usage->text); | ||||||||||
| 69 | } | ||||||||||
| 70 | my ($query, $connection) = OpenSearch::PPLQuery::Connection::resolve_connection( | ||||||||||
| 71 | 21 | \%cli, $options{connection_file}, $argv->[0], \*STDIN, | |||||||||
| 72 | ); | ||||||||||
| 73 | |||||||||||
| 74 | 16 | my $client = OpenSearch::PPLQuery->new(%$connection); | |||||||||
| 75 | 16 | my ($result, $error_status) = $client->execute($query); | |||||||||
| 76 | 15 | ▶ | if ($options{format} eq 'json') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 5 | print OpenSearch::PPLQuery::render_json($result); | |||||||||
| 78 | 5 | ▶ | return defined($error_status) ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | 10 | ▶ | die OpenSearch::PPLQuery::format_error($error_status, $result) if defined $error_status; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | print $options{format} eq 'csv' | ||||||||||
| 82 | ? OpenSearch::PPLQuery::render_csv($result) | ||||||||||
| 83 | 9 | ▶ | : OpenSearch::PPLQuery::render_table($result, max_width => $options{max_width}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 9 | return 0; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | __END__ | ||||||||||