script/pplquery

filter: Statementstmt 89.8% 124 / 138 Branchbran 69.3% 61 / 88 Conditioncond 52.9% 27 / 51 MC/DCmcdc 25.8% 8 / 31 Subroutinesub 100% 18 / 18 Podpod n/a totaltotal 73.0% 238 / 326 CC 60 SCAR 31.4
CC
60
Cov
77%
main
42.2
read_password_file
11.3
print_connection_list
11.1
CRAP 23.1
« lib/OpenSearch/PPLQuery.pm ↑ Summary lib/OpenSearch/PPLQuery/Config.pm »
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 # Keep usage() and that file in step: usage() is the terse reminder, the POD is
9 # the reference, and both must describe the same options.
10  
11 20 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 20 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use OpenSearch::PPLQuery ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use OpenSearch::PPLQuery::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 20 ▶binmode STDOUT, ':encoding(UTF-8)' or die "Cannot configure stdout as UTF-8: $!\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
binmode STDOUT, ":encoding(UTF-8)" or die "Cannot configure stdout as UTF-8: $!\n" 0 20 20
20 20 ▶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" 0 20 20
21 20 ▶binmode STDIN, ':encoding(UTF-8)' or die "Cannot configure stdin as UTF-8: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
binmode STDIN, ":encoding(UTF-8)" or die "Cannot configure stdin as UTF-8: $!\n" 0 20 20
22  
23 20 my $exit_status = eval { main(\@ARGV) };
24 20 ▶if ($@ ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ ne "") 6 14 20
25 6 my $message = OpenSearch::PPLQuery::exception_text($@);
26 6 $message =~ s/\s+\z//;
27 6 print STDERR "pplquery: $message\n";
28 6 exit 1;
29 }
30 14 exit $exit_status;
31  
32 sub main {
33 20 ▶ my ($argv) = @_;
Subroutine main: called CC=37 SCAR=37.4
34 20 @$argv = map { OpenSearch::PPLQuery::decode_utf8($_, 'command-line argument') } @$argv;
35 20 my %options = (format => 'table', max_width => 0);
36 20 my %cli;
37 my $parsed = GetOptionsFromArray(
38 $argv, 'config=s' => \$cli{config}, 'connection=s' => \$cli{connection}, 'list-connections' => \$cli{list_connections},
39 'url=s' => \$cli{url}, 'user=s' => \$cli{user}, 'password-file=s' => \$cli{password_file},
40 'ca-file=s' => \$cli{ca_file}, 'insecure' => \$cli{insecure},
41 'format=s' => \$options{format}, 'max-width=i' => \$options{max_width},
42 'timeout=i' => \$cli{timeout}, 'help' => \$options{help},
43 20 );
44 20 ▶ die usage() if !$parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 1 19 20
45 19 ▶ if ($options{help}) { print usage(); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"help"}) 0 19 19
46 19 ▶ die "--format must be 'table', 'json', or 'csv'\n" if $options{format} !~ /\A(?:table|json|csv)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"format"} =~ /\A(?:table|json|csv)\z/u 0 19 19
47 19 ▶ die "--max-width must not be negative\n" if $options{max_width} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options{"max_width"} < 0 0 19 19
48 19 ▶ die "--timeout must be greater than zero\n" if defined($cli{timeout}) && $cli{timeout} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cli{"timeout"} and $cli{"timeout"} <= 0 0 19 19
Condition33%1/3
defined $cli{"timeout"} and $cli{"timeout"} <= 0
!ll&&!rl&&r
1900
MC/DC0%0/2
defined $cli{"timeout"} and $cli{"timeout"} <= 0
defined $cli{"timeout"} $cli{"timeout"} <= 0
Truth table33%1/3
A and B
A= defined $cli{"timeout"}B= $cli{"timeout"} <= 0
ABresult
0X0
100
111
49  
50 19 ▶ if ($cli{list_connections}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cli{"list_connections"}) 1 18 19
51 1 ▶ die "--list-connections does not accept a query file\n" if @$argv;
Branch: 1 branch
BranchTrueFalseTotal
if @$argv 0 1 1
52 1 ▶ die "--list-connections supports only table or json format\n" if $options{format} eq 'csv';
Branch: 1 branch
BranchTrueFalseTotal
if $options{"format"} eq "csv" 0 1 1
53 die "--list-connections cannot be combined with connection or password options\n"
54 1 ▶ if defined($cli{connection}) || grep { defined $cli{$_} } qw(url user password_file ca_file insecure);
Branch: 1 branch
BranchTrueFalseTotal
if defined $cli{"connection"} or grep {defined $cli{$_};} "url", "user", "password_file", "ca_file", "insecure" 0 1 1
Condition33%1/3
defined $cli{"connection"} or grep {defined $cli{$_};} "url", "user", "password_file", "ca_file", "insecure"
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $cli{"connection"} or grep {defined $cli{$_};} "url", "user", "password_file", "ca_file", "insecure"
defined $cli{"connection"} grep {defined $cli{$_};} "url", "user", "password_file", "ca_file", "insecure"
Truth table33%1/3
A or B
A= defined $cli{"connection"}B= grep {defined $cli{$_};} "url", "user", "password_file", "ca_file", "insecure"
ABresult
1X1
011
000
55 1 my $config = load_config(\%cli, 1);
56 1 print_connection_list($config, $options{format});
57 1 return 0;
58 }
59  
60 18 ▶ die usage() if @$argv != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$argv != 1 0 18 18
61 18 my @direct_options = grep { defined $cli{$_} } qw(url user ca_file insecure);
62 18 ▶ die "--connection cannot be combined with direct connection options\n" if defined($cli{connection}) && @direct_options;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cli{"connection"} and @direct_options 1 17 18
Condition100%3/3
defined $cli{"connection"} and @direct_options
!ll&&!rl&&r
1431
MC/DC100%2/2
defined $cli{"connection"} and @direct_options
defined $cli{"connection"} @direct_options
Truth table100%3/3
A and B
A= defined $cli{"connection"}B= @direct_options
ABresult
0X0
100
111
63 17 my $environment_connection = environment_text('PPLQUERY_CONNECTION');
64 17 my ($connection_name, $config);
65 17 ▶ if (defined $cli{connection}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $cli{'connection'}) { } 3 14 17
elsif (not @direct_options and defined $environment_connection) { } 1 13 14
elsif (not @direct_options) { } 3 10 13
66 3 $connection_name = $cli{connection};
67 ▶ } elsif (!@direct_options && defined $environment_connection) {
Condition100%3/3
not @direct_options and defined $environment_connection
!ll&&!rl&&r
1031
MC/DC100%2/2
not @direct_options and defined $environment_connection
not @direct_options defined $environment_connection
Truth table100%3/3
not(A) and B
A= not @direct_optionsB= defined $environment_connection
ABresult
0X0
100
111
68 1 $connection_name = $environment_connection;
69 } elsif (!@direct_options) {
70 3 ▶ my $configured_path = $cli{config} // environment_text('PPLQUERY_CONFIG');
Condition100%3/3
$cli{"config"} // environment_text("PPLQUERY_CONFIG")
l!l&&r!l&&!r
111
MC/DC100%2/2
$cli{"config"} // environment_text("PPLQUERY_CONFIG")
$cli{"config"} environment_text("PPLQUERY_CONFIG")
Truth table100%3/3
A // B
A= $cli{"config"}B= environment_text("PPLQUERY_CONFIG")
ABresult
1X1
011
000
71 3 my $path = $configured_path;
72 3 ▶ $path //= OpenSearch::PPLQuery::Config->default_path if !defined(environment_text('OPENSEARCH_URL'));
Branch: 1 branch
BranchTrueFalseTotal
unless defined environment_text("OPENSEARCH_URL") 2 1 3
Condition33%1/3
$path //= "OpenSearch::PPLQuery::Config"->default_path
l!l&&r!l&&!r
200
MC/DC0%0/2
$path //= "OpenSearch::PPLQuery::Config"->default_path
$path "OpenSearch::PPLQuery::Config"->default_path
Truth table33%1/3
A //= B
A= $pathB= "OpenSearch::PPLQuery::Config"->default_path
ABresult
1X1
011
000
73 3 ▶ if (defined($path) && (defined($configured_path) || -f $path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path and defined $configured_path || -f $path) 2 1 3
Condition50%3/6
defined $path and defined $configured_path || -f $path
!ll&&!rl&&r
120
defined $configured_path or -f $path
l!l&&r!l&&!r
200
MC/DC0%0/3
defined $path and defined $configured_path || -f $path
defined $path defined $configured_path -f $path
Truth table25%1/4
A and B or C
A= defined $pathB= defined $configured_pathC= -f $path
ABCresult
0XX0
1000
11X1
1011
74 2 $config = OpenSearch::PPLQuery::Config->load($path);
75 2 $connection_name = $config->default_name;
76 2 ▶ die "Connection configuration $path has no default connection; use --connection\n" if !defined $connection_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $connection_name 0 2 2
77 }
78 }
79  
80 17 ▶ if (defined $connection_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connection_name) { } 6 11 17
81 6 ▶ $config //= load_config(\%cli, 1);
Condition66%2/3
$config //= load_config(\%cli, 1)
l!l&&r!l&&!r
240
MC/DC0%0/2
$config //= load_config(\%cli, 1)
$config load_config(\%cli, 1)
Truth table66%2/3
A //= B
A= $configB= load_config(\%cli, 1)
ABresult
1X1
011
000
82 6 my $connection = $config->connection($connection_name);
83 6 my $auth_type = delete $connection->{auth_type};
84 6 ▶ if ($auth_type eq 'basic') {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_type eq "basic") 2 4 6
85 2 $connection->{password} = connection_password($connection, \%cli, $connection_name);
86 }
87 4 delete $connection->{password_environment};
88 4 delete $connection->{password_file};
89 4 %options = (%options, %$connection);
90 } else {
91 %options = (
92 %options,
93 url => environment_text('OPENSEARCH_URL') // 'http://127.0.0.1:9200',
94 user => environment_text('OPENSEARCH_USERNAME'), password => direct_password(\%cli),
95 timeout => 60,
96 11 ▶ map { defined($cli{$_}) ? ($_ => $cli{$_}) : () } qw(url user ca_file insecure),
Branch: 1 branch
BranchTrueFalseTotal
defined $cli{$_} ? : 10 34 44
Condition100%2/2
environment_text("OPENSEARCH_URL") // "http://127.0.0.1:9200"
l!l
110
MC/DC100%1/1
environment_text("OPENSEARCH_URL") // "http://127.0.0.1:9200"
environment_text("OPENSEARCH_URL")
Truth table100%2/2
A
A= environment_text("OPENSEARCH_URL")
Aresult
11
00
97 );
98 }
99 15 ▶ $options{timeout} = $cli{timeout} if defined $cli{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cli{"timeout"} 0 15 15
100  
101 15 my $query = read_query($argv->[0]);
102 15 $query =~ s/^#[^\r\n]*(?:\r\n|\r|\n|\z)//mg;
103 15 ▶ die "Query is empty\n" if $query !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $query =~ /\S/u 1 14 15
104 14 my $client = OpenSearch::PPLQuery->new(%options);
105 14 my ($result, $error_status) = $client->execute($query);
106 14 ▶ if ($options{format} eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"format"} eq "json") 8 6 14
107 8 print OpenSearch::PPLQuery::render_json($result);
108 8 ▶ return defined($error_status) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $error_status ? : 1 7 8
109 }
110 6 ▶ die OpenSearch::PPLQuery::format_error($error_status, $result) if defined $error_status;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error_status 1 5 6
111 5 ▶ print $options{format} eq 'csv' ? OpenSearch::PPLQuery::render_csv($result) : OpenSearch::PPLQuery::render_table($result, %options);
Branch: 1 branch
BranchTrueFalseTotal
$options{'format'} eq 'csv' ? : 2 3 5
112 5 return 0;
113 }
114  
115 sub usage {
116 1 ▶ return <<'USAGE';
Subroutine usage: called CC=1 SCAR=0.0
117 Usage: pplquery [options] FILE|-
118  
119 Execute one PPL query read from a UTF-8 file or stdin.
120  
121 Options:
122 --config FILE Connection configuration (default: PPLQUERY_CONFIG or platform config directory)
123 --connection NAME Named connection (default: PPLQUERY_CONNECTION or configured default)
124 --list-connections List configured connections; use --format json for machine-readable output
125 --url URL OpenSearch base URL (default: OPENSEARCH_URL or http://127.0.0.1:9200)
126 --user USER Basic-auth username (default: OPENSEARCH_USERNAME)
127 --password-file FILE
128 Read the Basic-auth password from a UTF-8 file
129 --ca-file FILE CA certificate for HTTPS verification
130 --insecure Disable HTTPS certificate and hostname verification
131 --format FORMAT Output format: table, json, or csv (default: table)
132 --max-width N Truncate table cells to N characters (default: 0, no limit)
133 --timeout SECONDS HTTP timeout (default: 60)
134 --help Show this help
135  
136 Set OPENSEARCH_PASSWORD when Basic authentication has no configured password source. Passwords are not accepted on the command line.
137  
138 Run 'perldoc pplquery' for the full manual, covering named connections, password sources, output formats, and troubleshooting.
139 USAGE
140 }
141  
142 sub read_query {
143 15 ▶ my ($path) = @_;
Subroutine read_query: called CC=4 SCAR=14.0
144 15 ▶ return read_text_handle(\*STDIN) if $path eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "-" 14 1 15
145 1 ▶ open my $handle, '<:encoding(UTF-8)', $path or die "Cannot open $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<:encoding(UTF-8)", $path or die "Cannot open ${path}: $!\n" 0 1 1
146 1 my $query = read_text_handle($handle);
147 1 ▶ close $handle or die "Cannot close $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $handle or die "Cannot close ${path}: $!\n" 0 1 1
148 1 return $query;
149 }
150  
151 sub read_text_handle {
152 15 ▶ my ($handle) = @_;
Subroutine read_text_handle: called CC=2 SCAR=7.1
153 15 local $/;
154 15 ▶ return <$handle> // '';
Condition50%1/2
readline $handle // ""
l!l
150
MC/DC0%0/1
readline $handle // ""
readline $handle
Truth table50%1/2
A
A= readline $handle
Aresult
11
00
155 }
156  
157 sub config_path {
158 5 ▶ my ($cli) = @_;
Subroutine config_path: called CC=3 SCAR=12.5
159 5 ▶ return $cli->{config} // environment_text('PPLQUERY_CONFIG') // OpenSearch::PPLQuery::Config->default_path;
Condition50%3/6
($cli->{"config"} // environment_text("PPLQUERY_CONFIG")) // "OpenSearch::PPLQuery::Config"->default_path
l!l&&r!l&&!r
500
$cli->{"config"} // environment_text("PPLQUERY_CONFIG")
l!l&&r!l&&!r
410
MC/DC0%0/3
($cli->{"config"} // environment_text("PPLQUERY_CONFIG")) // "OpenSearch::PPLQuery::Config"->default_path
$cli->{"config"} environment_text("PPLQUERY_CONFIG") "OpenSearch::PPLQuery::Config"->default_path
Truth table50%2/4
A // B // C
A= $cli->{"config"}B= environment_text("PPLQUERY_CONFIG")C= "OpenSearch::PPLQuery::Config"->default_path
ABCresult
1XX1
01X1
0011
0000
160 }
161  
162 sub load_config {
163 5 ▶ my ($cli, $required) = @_;
Subroutine load_config: called CC=3 SCAR=12.0
164 5 my $path = config_path($cli);
165 5 ▶ die "Connection configuration $path does not exist\n" if $required && !-f $path;
Branch: 1 branch
BranchTrueFalseTotal
if $required and not -f $path 0 5 5
Condition33%1/3
$required and not -f $path
!ll&&!rl&&r
050
MC/DC0%0/2
$required and not -f $path
$required not -f $path
Truth table33%1/3
A and not(B)
A= $requiredB= not -f $path
ABresult
0X0
100
111
166 5 return OpenSearch::PPLQuery::Config->load($path);
167 }
168  
169 sub print_connection_list {
170 1 ▶ my ($config, $format) = @_;
Subroutine print_connection_list: called CC=5 SCAR=24.1
171 1 my $descriptions = $config->descriptions;
172 1 ▶ if ($format eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "json") 1 0 1
173 1 print Cpanel::JSON::XS->new->utf8(0)->canonical(1)->pretty(1)->encode({
174 config => $config->path, default => $config->default_name, connections => $descriptions,
175 });
176 1 return;
177 }
178 0 my $default = $config->default_name;
179 0 for my $connection (@$descriptions) {
180 0 ▶ my $marker = defined($default) && $connection->{name} eq $default ? '*' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $default && $connection->{'name'} eq $default ? : 0 0 0
Condition0%0/3
defined $default and $connection->{"name"} eq $default
!ll&&!rl&&r
000
MC/DC0%0/2
defined $default and $connection->{"name"} eq $default
defined $default $connection->{"name"} eq $default
Truth table0%0/3
A and B
A= defined $defaultB= $connection->{"name"} eq $default
ABresult
0X0
100
111
181 0 print "$marker $connection->{name}\t$connection->{url}\t$connection->{authentication}{type}\n";
182 }
183 }
184  
185 sub connection_password {
186 2 ▶ my ($connection, $cli, $name) = @_;
Subroutine connection_password: called CC=6 SCAR=21.2
187 2 ▶ my $override_file = $cli->{password_file} // environment_text('PPLQUERY_PASSWORD_FILE');
Condition66%2/3
$cli->{"password_file"} // environment_text("PPLQUERY_PASSWORD_FILE")
l!l&&r!l&&!r
101
MC/DC50%1/2
$cli->{"password_file"} // environment_text("PPLQUERY_PASSWORD_FILE")
$cli->{"password_file"} environment_text("PPLQUERY_PASSWORD_FILE")
Truth table66%2/3
A // B
A= $cli->{"password_file"}B= environment_text("PPLQUERY_PASSWORD_FILE")
ABresult
1X1
011
000
188 2 ▶ return read_password_file($override_file) if defined $override_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $override_file 1 1 2
189 1 ▶ if (defined $connection->{password_environment}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connection->{"password_environment"}) 1 0 1
190 1 my $password = environment_text($connection->{password_environment});
191 1 ▶ die "Connection '$name' requires environment variable $connection->{password_environment}\n" if !defined $password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password 1 0 1
192 0 return $password;
193 }
194 0 ▶ return read_password_file($connection->{password_file}) if defined $connection->{password_file};
Branch: 1 branch
BranchTrueFalseTotal
if defined $connection->{"password_file"} 0 0 0
195 0 return environment_text('OPENSEARCH_PASSWORD');
196 }
197  
198 sub direct_password {
199 11 ▶ my ($cli) = @_;
Subroutine direct_password: called CC=3 SCAR=12.5
200 11 ▶ my $file = $cli->{password_file} // environment_text('PPLQUERY_PASSWORD_FILE');
Condition33%1/3
$cli->{"password_file"} // environment_text("PPLQUERY_PASSWORD_FILE")
l!l&&r!l&&!r
0011
MC/DC0%0/2
$cli->{"password_file"} // environment_text("PPLQUERY_PASSWORD_FILE")
$cli->{"password_file"} environment_text("PPLQUERY_PASSWORD_FILE")
Truth table33%1/3
A // B
A= $cli->{"password_file"}B= environment_text("PPLQUERY_PASSWORD_FILE")
ABresult
1X1
011
000
201 11 ▶ return defined($file) ? read_password_file($file) : environment_text('OPENSEARCH_PASSWORD');
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 0 11 11
202 }
203  
204 sub read_password_file {
205 1 ▶ my ($path) = @_;
Subroutine read_password_file: called CC=4 SCAR=24.2
206 1 ▶ open my $handle, '<:encoding(UTF-8)', $path or die "Cannot open password file $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<:encoding(UTF-8)", $path or die "Cannot open password file ${path}: $!\n" 1 0 1
207 0 local $/;
208 0 ▶ my $password = <$handle> // '';
Condition0%0/2
readline $handle // ""
l!l
00
MC/DC0%0/1
readline $handle // ""
readline $handle
Truth table0%0/2
A
A= readline $handle
Aresult
11
00
209 0 ▶ close $handle or die "Cannot close password file $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $handle or die "Cannot close password file ${path}: $!\n" 0 0 0
210 0 $password =~ s/\r?\n\z//;
211 0 return $password;
212 }
213  
214 sub environment_text {
215 69 ▶ my ($name) = @_;
Subroutine environment_text: called CC=2 SCAR=6.9
216 69 ▶ return undef if !exists $ENV{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{$name} 64 5 69
217 5 return OpenSearch::PPLQuery::decode_utf8($ENV{$name}, "environment variable $name");
218 }
219  
220 __END__
« lib/OpenSearch/PPLQuery.pm ↑ Summary lib/OpenSearch/PPLQuery/Config.pm »