script/pplquery

filter: Statementstmt 93.2% 55 / 59 Branchbran 71.8% 23 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 86.7% 85 / 98 CC 15 SCAR 24.8
CC
15
Cov
86%
main
16.4
BEGIN
1.0
BEGIN
1.0
CRAP 12.0
« lib/OpenSearch/PPLQuery.pm ↑ Summary
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
BranchTrueFalseTotal
binmode STDOUT, ":encoding(UTF-8)" or die "Cannot configure stdout as UTF-8: $!\n" 24 0 24
17 24 ▶binmode STDERR, ':encoding(UTF-8)' or die "Cannot configure stderr as UTF-8: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
binmode STDERR, ":encoding(UTF-8)" or die "Cannot configure stderr as UTF-8: $!\n" 24 0 24
18 24 ▶binmode STDIN, ':raw' or die "Cannot configure stdin as raw input: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
binmode STDIN, ":raw" or die "Cannot configure stdin as raw input: $!\n" 24 0 24
19  
20 24 my $exit_status = eval { main(\@ARGV) };
21 24 ▶if ($@ ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ ne "") 10 14 24
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
BranchTrueFalseTotal
if defined $parsed->$name 22 104 126
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
BranchTrueFalseTotal
if defined $parsed->$name 11 52 63
55 }
56 21 @$argv = @ARGV;
57 0 ▶ if ($options{help}) { print $usage->text; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"help"}) 0 21 21
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
BranchTrueFalseTotal
unless $options{"format"} =~ /\A(?:table|json|csv)\z/u 0 21 21
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
BranchTrueFalseTotal
if $options{"max_width"} < 0 0 21 21
61 21 ▶ OpenSearch::PPLQuery::validate_timeout($cli{timeout}, '--timeout') if defined $cli{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cli{"timeout"} 1 20 21
62  
63 21 ▶ if (@$argv != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$argv != 1) 0 21 21
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
BranchTrueFalseTotal
@$argv == 0 ? : 0 0 0
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
BranchTrueFalseTotal
if ($options{"format"} eq "json") 5 10 15
77 5 print OpenSearch::PPLQuery::render_json($result);
78 5 ▶ return defined($error_status) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $error_status ? : 1 4 5
79 }
80 10 ▶ die OpenSearch::PPLQuery::format_error($error_status, $result) if defined $error_status;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error_status 1 9 10
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
BranchTrueFalseTotal
$options{'format'} eq 'csv' ? : 2 7 9
84 9 return 0;
85 }
86  
87 __END__
« lib/OpenSearch/PPLQuery.pm ↑ Summary