bin/cloudflare-api

filter: Statementstmt 86.3% 233 / 270 Branchbran 70.4% 131 / 186 Conditioncond 61.5% 85 / 138 MC/DCmcdc 33.7% 25 / 74 Subroutinesub 79.4% 31 / 39 Podpod n/a totaltotal 71.4% 505 / 707 CC 141 SCAR 52.1
CC
141
Cov
76%
main
354.3
typed_value
13.1
next_query
11.5
CRAP 183.2
↑ Summary blib/lib/Cloudflare/API/Workers.pm »
1 #!/usr/bin/env perl
2 #
3 # This file is part of Cloudflare::API.
4 #
5 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 # Full license text is available at:
11 #
12 # <http://dev.perl.org/licenses/>
13 #
14  
15  
16 #
17 # Call Cloudflare management API methods from the command line
18 #
19 package main;
20  
21  
22 # Compiler pragmas
23 #
24 39 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 39 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28  
29 # Use the base module
30 #
31 39 ▶use Cloudflare::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 # External modules
35 #
36 39 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 39 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 39 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 39 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 39 ▶use FindBin qw($Script);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 39 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 39 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44  
45 # Local customisation
46 #
47 38 local $Data::Dumper::Indent=1;
48 38 local $Data::Dumper::Sortkeys=1;
49  
50  
51 # Version Info, must be all one line for MakeMaker, CPAN.
52 #
53 38 $VERSION='1.011';
54  
55  
56 # Only supported resource methods may be called by name. Each method records
57 # the minimum and maximum number of positional --arg values it accepts.
58 #
59 38 my %resource_method=(
60 accounts => {
61 list => [0, 0], get => [1, 1]
62 },
63 zones => {
64 list => [0, 0], get => [1, 1]
65 },
66 workers => {
67 list_scripts => [0, 0], search_scripts => [0, 0],
68 inspect_script => [0, 0], get_settings => [1, 1],
69 get_script_settings => [1, 1], upload_script => [1, 1],
70 upload_version => [1, 1], list_versions => [1, 1],
71 get_version => [2, 2], upload_assets => [2, 2],
72 delete_script => [1, 1], list_deployments => [1, 1],
73 create_deployment => [2, 2], get_deployment => [2, 2],
74 list_secrets => [1, 1], add_secret => [2, 2],
75 delete_secret => [2, 2], get_subdomain => [1, 1],
76 set_subdomain => [2, 2], list_routes => [1, 1],
77 create_route => [2, 2], update_route => [3, 3],
78 delete_route => [2, 2]
79 },
80 r2 => {
81 list_buckets => [0, 0], get_bucket => [1, 1],
82 create_bucket => [1, 1], update_bucket => [2, 2],
83 delete_bucket => [1, 1]
84 },
85 kv => {
86 list_namespaces => [0, 0], get_namespace => [1, 1],
87 create_namespace => [1, 1], rename_namespace => [2, 2],
88 delete_namespace => [1, 1], list_keys => [1, 1],
89 get_value => [2, 2], put_value => [3, 3],
90 delete_value => [2, 2]
91 },
92 d1 => {
93 list_databases => [0, 0], get_database => [1, 1],
94 create_database => [1, 1], delete_database => [1, 1],
95 update_database => [2, 2], query_database => [2, 2],
96 query_sql => [2, 3]
97 },
98 queues => {
99 list_queues => [0, 0], get_queue => [1, 1],
100 create_queue => [1, 1], delete_queue => [1, 1],
101 update_queue => [2, 2], list_consumers => [1, 1],
102 create_consumer => [2, 2], delete_consumer => [2, 2]
103 },
104 hyperdrive => {
105 list_configs => [0, 0], get_config => [1, 1],
106 create_config => [1, 1], replace_config => [2, 2],
107 update_config => [2, 2], delete_config => [1, 1]
108 },
109 secrets_store => {
110 list_stores => [0, 0], get_store => [1, 1],
111 create_store => [1, 1], delete_store => [1, 1],
112 list_secrets => [1, 1], get_secret => [2, 2],
113 create_secret => [2, 2], update_secret => [3, 3],
114 delete_secret => [2, 2], get_quota => [0, 0]
115 }
116 );
117  
118  
119 # Completion metadata is kept beside the parser it describes. The renderer is
120 # loaded only when completion output is requested.
121 #
122 38 my $completion_option_ar=[
123 ['--resource', 'resource', 'Cloudflare resource'],
124 ['--action', 'action', 'resource action'],
125 ['--method', 'method', 'raw HTTP method'],
126 ['--path', 'value', 'raw API path'],
127 ['--account-id', 'value', 'Cloudflare account ID'],
128 ['--auth', 'auth', 'authentication source'],
129 ['--output', 'output', 'output format'],
130 ['--full-response', 'flag', 'retain the Cloudflare response envelope'],
131 ['--no-full-response', 'flag', 'unwrap the Cloudflare result'],
132 ['--paginate', 'flag', 'follow result pages'],
133 ['--no-paginate', 'flag', 'do not follow result pages'],
134 ['--max-pages', 'value', 'maximum pages'],
135 ['--per-page', 'value', 'results per page'],
136 ['--help', 'flag', 'show usage'],
137 ['-h', 'flag', 'show usage'],
138 ['-?', 'flag', 'show usage'],
139 ['--man', 'flag', 'show the manual'],
140 ['--version', 'flag', 'show the version'],
141 ['--dump-opt', 'flag', 'show parsed arguments without a request'],
142 ['--asset', 'file', 'add a Worker asset file'],
143 ['--asset-list-json', 'file', 'read a JSON asset list'],
144 ['--asset-list-text', 'file', 'read a line-based asset list'],
145 ['--asset-list-stdin', 'flag', 'read an asset list from standard input'],
146 ['--arg', 'value', 'append a string argument'],
147 ['--arg-bool', 'bool', 'append a boolean argument'],
148 ['--arg-array', 'value', 'append a JSON array argument'],
149 ['--arg-hash', 'value', 'append a JSON object argument'],
150 ['--arg-json', 'value', 'append a JSON argument'],
151 ['--arg-json-file', 'file', 'read a JSON argument from a file'],
152 ['--arg-dumper-file', 'file', 'read a Data::Dumper argument from a file'],
153 ['--param', 'value', 'set a string named parameter'],
154 ['--param-bool', 'value', 'set a boolean named parameter'],
155 ['--param-json', 'value', 'set a JSON named parameter'],
156 ['--param-json-file', 'file', 'read a JSON named parameter from a file'],
157 ['--param-dumper-file', 'file', 'read a Data::Dumper parameter from a file'],
158 ['--generate-completion', 'shell', 'generate offline shell completion']
159 ];
160  
161  
162 # Run main. Keep the script loadable by the local test suite.
163 #
164 38 ▶exit main(\@ARGV) unless caller();
Branch: 1 branch
BranchTrueFalseTotal
unless caller() 38 0 38
165  
166  
167 #============================================================================
168  
169  
170 41 ▶sub main {
Subroutine main: called CC=91 SCAR=58.7
171  
172  
173 # Get arguments and command line options
174 #
175 41 my $argv_ar=shift();
176 41 my (%opt, @arg, %param, @asset_source, @argument_source);
177 41 my $getopt_or=Getopt::Long::Parser->new(config => [qw(permute)]);
178 $getopt_or->getoptionsfromarray(
179 $argv_ar,
180 \%opt,
181 my @opt=(
182 'resource=s',
183 'action=s',
184 'method=s',
185 'path=s',
186 'account-id=s',
187 'auth=s',
188 'output=s',
189 'full-response!',
190 'paginate!',
191 'max-pages=i',
192 'per-page=i',
193 'help|h|?',
194 'man',
195 'version',
196 'generate-completion=s',
197 'dump_opt|dump-opt|opt',
198 4 ▶ 'asset=s' => sub { push(@asset_source, ['file', $_[1]]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
199 1 ▶ 'asset-list-json=s' => sub { push(@asset_source, ['json', $_[1]]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
200 2 ▶ 'asset-list-text=s' => sub { push(@asset_source, ['text', $_[1]]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
201 3 ▶ 'asset-list-stdin' => sub { push(@asset_source, ['stdin']) },
Subroutine __ANON__: called CC=1 SCAR=0.0
202 11 ▶ 'arg=s' => sub { push(@argument_source, ['argument', typed_value('string', $_[1])]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
203 0 ▶ 'arg-bool=s' => sub { push(@argument_source, ['argument', typed_value('bool', $_[1])]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 1 ▶ 'arg-array=s' => sub { push(@argument_source, ['argument', typed_value('array', $_[1])]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
205 0 ▶ 'arg-hash=s' => sub { push(@argument_source, ['argument', typed_value('hash', $_[1])]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
206 1 ▶ 'arg-json=s' => sub { push(@argument_source, ['argument', typed_value('json', $_[1])]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
207 0 ▶ 'arg-json-file=s' => sub { push(@argument_source, ['argument', typed_value('json-file', $_[1])]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 ▶ 'arg-dumper-file=s' => sub { push(@argument_source, ['argument', typed_value('dumper-file', $_[1])]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
209 8 ▶ 'param=s' => sub { my ($key, $value)=parse_named('string', $_[1]); $param{$key}=$value },
Subroutine __ANON__: called CC=1 SCAR=0.0
210 0 ▶ 'param-bool=s' => sub { my ($key, $value)=parse_named('bool', $_[1]); $param{$key}=$value },
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 0 ▶ 'param-json=s' => sub { my ($key, $value)=parse_named('json', $_[1]); $param{$key}=$value },
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 0 ▶ 'param-json-file=s' => sub { my ($key, $value)=parse_named('json-file', $_[1]); $param{$key}=$value },
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 ▶ 'param-dumper-file=s' => sub { my ($key, $value)=parse_named('dumper-file', $_[1]); $param{$key}=$value },
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 37 ▶ '<>' => sub { push(@argument_source, ['bare', $_[0]]) }
Subroutine __ANON__: called CC=1 SCAR=0.0
215 )
216 41 ▶ ) || pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
$getopt_or->getoptionsfromarray($argv_ar, \%opt, my(@opt) = ("resource=s", "action=s", "method=s", "path=s", "account-id=s", "auth=s", "output=s", "full-response!", "paginate!", "max-pages=i", "per-page=i", "help|h|?", "man", "version", "generate-completion=s", "dump_opt|dump-opt|opt", "asset=s", sub { push @asset_source, ["file", $_[1]]; } , "asset-list-json=s", sub { push @asset_source, ["json", $_[1]]; } , "asset-list-text=s", sub { push @asset_source, ["text", $_[1]]; } , "asset-list-stdin", sub { push @asset_source, ["stdin"]; } , "arg=s", sub { push @argument_source, ["argument", typed_value("string", $_[1])]; } , "arg-bool=s", sub { push @argument_source, ["argument", typed_value("bool", $_[1])]; } , "arg-array=s", sub { push @argument_source, ["argument", typed_value("array", $_[1])]; } , "arg-hash=s", sub { push @argument_source, ["argument", typed_value("hash", $_[1])]; } , "arg-json=s", sub { push @argument_source, ["argument", typed_value("json", $_[1])]; } , "arg-json-file=s", sub { push @argument_source, ["argument", typed_value("json-file", $_[1])]; } , "arg-dumper-file=s", sub { push @argument_source, ["argument", typed_value("dumper-file", $_[1])]; } , "param=s", sub { my($key, $value) = parse_named("string", $_[1]); $param{$key} = $value; } , "param-bool=s", sub { my($key, $value) = parse_named("bool", $_[1]); $param{$key} = $value; } , "param-json=s", sub { my($key, $value) = parse_named("json", $_[1]); $param{$key} = $value; } , "param-json-file=s", sub { my($key, $value) = parse_named("json-file", $_[1]); $param{$key} = $value; } , "param-dumper-file=s", sub { my($key, $value) = parse_named("dumper-file", $_[1]); $param{$key} = $value; } , "<>", sub { push @argument_source, ["bare", $_[0]]; } )) or pod2usage(2) 41 0 41
217 41 ▶ pod2usage(1) if $opt{'help'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 41 41
218 41 ▶ pod2usage(-verbose => 2, -exitval => 0) if $opt{'man'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"man"} 0 41 41
219 41 ▶ $opt{'version'} && do {print "$Script $VERSION\n"; return 0};
Branch: 1 branch
BranchTrueFalseTotal
$opt{"version"} and do { print "$main::Script $main::VERSION\n"; return 0 } 1 40 41
220  
221  
222  
223 # Generate completion before validating a request or resolving credentials
224 #
225 40 ▶ if (defined($opt{'generate-completion'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"generate-completion"}) 4 36 40
226 4 require Cloudflare::API::CLI::Completion;
227 print Cloudflare::API::CLI::Completion::generate(
228 4 $opt{'generate-completion'}, {
229 resources => \%resource_method,
230 options => $completion_option_ar
231 }
232 );
233 3 return 0;
234 }
235  
236  
237  
238 # Resolve bare selection operands, then retain later operands as arguments
239 #
240 36 ▶ my $raw_fg=defined($opt{'method'})||defined($opt{'path'});
Condition66%2/3
defined $opt{'method'} || defined $opt{'path'}
l!l&&r!l&&!r
1035
MC/DC50%1/2
defined $opt{'method'} || defined $opt{'path'}
defined $opt{'method'} defined $opt{'path'}
Truth table66%2/3
A || B
A= defined $opt{'method'}B= defined $opt{'path'}
ABresult
1X1
011
000
241 36 push(@argument_source, map { ['bare', $_] } @$argv_ar);
242 36 @$argv_ar=();
243 36 foreach my $source_ar (@argument_source) {
244 51 my ($type, $value)=@$source_ar;
245 51 ▶ die "unexpected positional arguments\n" if $raw_fg&&$type eq 'bare';
Branch: 1 branch
BranchTrueFalseTotal
if $raw_fg and $type eq "bare" 1 50 51
Condition66%2/3
$raw_fg and $type eq "bare"
!ll&&!rl&&r
5001
MC/DC50%1/2
$raw_fg and $type eq "bare"
$raw_fg $type eq "bare"
Truth table66%2/3
A and B
A= $raw_fgB= $type eq "bare"
ABresult
0X0
100
111
246 50 ▶ if ($type eq 'bare'&&!defined($opt{'resource'})) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'bare' and not defined $opt{'resource'}) { } 14 36 50
elsif ($type eq 'bare' and not defined $opt{'action'}) { } 15 21 36
Condition100%3/3
$type eq "bare" and not defined $opt{"resource"}
!ll&&!rl&&r
132314
MC/DC100%2/2
$type eq "bare" and not defined $opt{"resource"}
$type eq "bare" not defined $opt{"resource"}
Truth table100%3/3
A and not(B)
A= $type eq "bare"B= not defined $opt{"resource"}
ABresult
0X0
100
111
247 14 $opt{'resource'}=$value;
248 }
249 ▶ elsif ($type eq 'bare'&&!defined($opt{'action'})) {
Condition100%3/3
$type eq "bare" and not defined $opt{"action"}
!ll&&!rl&&r
13815
MC/DC100%2/2
$type eq "bare" and not defined $opt{"action"}
$type eq "bare" not defined $opt{"action"}
Truth table100%3/3
A and not(B)
A= $type eq "bare"B= not defined $opt{"action"}
ABresult
0X0
100
111
250 15 $opt{'action'}=$value;
251 }
252 else {
253 21 push(@arg, $value);
254 }
255 }
256  
257  
258 # Check the output format and pagination limits before making a request
259 #
260 35 ▶ $opt{'output'}='json' unless defined($opt{'output'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"output"} 35 0 35
261 35 ▶ die "output must be json or dumper\n" unless $opt{'output'}=~/\A(?:json|dumper)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"output"} =~ /\A(?:json|dumper)\z/ 0 35 35
262 35 ▶ die "auth must be wrangler\n" if defined($opt{'auth'})&&$opt{'auth'} ne 'wrangler';
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"auth"} and $opt{"auth"} ne "wrangler" 1 34 35
Condition100%3/3
defined $opt{"auth"} and $opt{"auth"} ne "wrangler"
!ll&&!rl&&r
2951
MC/DC100%2/2
defined $opt{"auth"} and $opt{"auth"} ne "wrangler"
defined $opt{"auth"} $opt{"auth"} ne "wrangler"
Truth table100%3/3
A and B
A= defined $opt{"auth"}B= $opt{"auth"} ne "wrangler"
ABresult
0X0
100
111
263 34 ▶ die "max-pages must be positive\n" if defined($opt{'max-pages'})&&$opt{'max-pages'}<1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"max-pages"} and $opt{"max-pages"} < 1 0 34 34
Condition66%2/3
defined $opt{"max-pages"} and $opt{"max-pages"} < 1
!ll&&!rl&&r
3310
MC/DC0%0/2
defined $opt{"max-pages"} and $opt{"max-pages"} < 1
defined $opt{"max-pages"} $opt{"max-pages"} < 1
Truth table66%2/3
A and B
A= defined $opt{"max-pages"}B= $opt{"max-pages"} < 1
ABresult
0X0
100
111
264 34 ▶ die "per-page must be positive\n" if defined($opt{'per-page'})&&$opt{'per-page'}<1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"per-page"} and $opt{"per-page"} < 1 0 34 34
Condition66%2/3
defined $opt{"per-page"} and $opt{"per-page"} < 1
!ll&&!rl&&r
3310
MC/DC0%0/2
defined $opt{"per-page"} and $opt{"per-page"} < 1
defined $opt{"per-page"} $opt{"per-page"} < 1
Truth table66%2/3
A and B
A= defined $opt{"per-page"}B= $opt{"per-page"} < 1
ABresult
0X0
100
111
265 34 ▶ $param{'per_page'}=$opt{'per-page'} if defined($opt{'per-page'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"per-page"} 1 33 34
266  
267  
268 # Accept either a raw API request or a supported named resource method
269 #
270 34 ▶ if ($raw_fg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_fg) { } 0 34 34
271 die "--method and --path are both required\n"
272 0 ▶ unless defined($opt{'method'})&&defined($opt{'path'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"method"} and defined $opt{"path"} 0 0 0
Condition0%0/3
defined $opt{"method"} and defined $opt{"path"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt{"method"} and defined $opt{"path"}
defined $opt{"method"} defined $opt{"path"}
Truth table0%0/3
A and B
A= defined $opt{"method"}B= defined $opt{"path"}
ABresult
0X0
100
111
273 die "--resource and --action cannot accompany --method\n"
274 0 ▶ if defined($opt{'resource'})||defined($opt{'action'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"resource"} or defined $opt{"action"} 0 0 0
Condition0%0/3
defined $opt{"resource"} or defined $opt{"action"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $opt{"resource"} or defined $opt{"action"}
defined $opt{"resource"} defined $opt{"action"}
Truth table0%0/3
A or B
A= defined $opt{"resource"}B= defined $opt{"action"}
ABresult
1X1
011
000
275 0 ▶ die "raw request accepts at most one body argument\n" if @arg>1;
Branch: 1 branch
BranchTrueFalseTotal
if @arg > 1 0 0 0
276 }
277 else {
278 34 my ($resource, $action)=@opt{qw(resource action)};
279 34 my $resource_list=join(', ', sort(keys(%resource_method)));
280 34 ▶ die "resource is required; valid resources: $resource_list\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resource 1 33 34
281 unless defined($resource);
282 die "unknown resource '$resource'; valid resources: $resource_list\n"
283 33 ▶ unless exists($resource_method{$resource});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $resource_method{$resource} 1 32 33
284 32 my $action_list=join(', ', sort(keys(%{$resource_method{$resource}})));
285 32 ▶ die "action is required for resource '$resource'; valid actions: $action_list\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 1 31 32
286 unless defined($action);
287 die "unknown action '$action' for resource '$resource'; valid actions: $action_list\n"
288 31 ▶ unless exists($resource_method{$resource}{$action});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $resource_method{$resource}{$action} 1 30 31
289 }
290 die "--paginate requires a list or search action\n"
291 if $opt{'paginate'}&&($raw_fg||
292 30 ▶ $opt{'action'}!~/\A(?:list(?:_|\z)|search_scripts\z)/);
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"paginate"} and $raw_fg || !($opt{"action"} =~ /\A(?:list(?:_|\z)|search_scripts\z)/) 1 29 30
Condition83%5/6
$opt{"paginate"} and $raw_fg || !($opt{"action"} =~ /\A(?:list(?:_|\z)|search_scripts\z)/)
!ll&&!rl&&r
2721
$raw_fg or not $opt{"action"} =~ /\A(?:list(?:_|\z)|search_scripts\z)/
l!l&&r!l&&!r
012
MC/DC66%2/3
$opt{"paginate"} and $raw_fg || !($opt{"action"} =~ /\A(?:list(?:_|\z)|search_scripts\z)/)
$opt{"paginate"} $raw_fg not $opt{"action"} =~ /\A(?:list(?:_|\z)|search_scripts\z)/
Truth table75%3/4
A and B or not(C)
A= $opt{"paginate"}B= $raw_fgC= not $opt{"action"} =~ /\A(?:list(?:_|\z)|search_scripts\z)/
ABCresult
0XX0
1000
11X1
1011
293 29 ▶ die "--max-pages requires --paginate\n" if defined($opt{'max-pages'})&&!$opt{'paginate'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"max-pages"} and not $opt{"paginate"} 0 29 29
Condition66%2/3
defined $opt{"max-pages"} and not $opt{"paginate"}
!ll&&!rl&&r
2810
MC/DC0%0/2
defined $opt{"max-pages"} and not $opt{"paginate"}
defined $opt{"max-pages"} not $opt{"paginate"}
Truth table66%2/3
A and not(B)
A= defined $opt{"max-pages"}B= not $opt{"paginate"}
ABresult
0X0
100
111
294  
295  
296 # Combine asset sources only for the named Worker asset upload action
297 #
298 29 ▶ if (@asset_source) {
Branch: 1 branch
BranchTrueFalseTotal
if (@asset_source) 5 24 29
299 die "asset list options require --resource workers --action upload_assets\n"
300 5 ▶ if $raw_fg||$opt{'resource'} ne 'workers'||$opt{'action'} ne 'upload_assets';
Branch: 1 branch
BranchTrueFalseTotal
if $raw_fg or $opt{"resource"} ne "workers" or $opt{"action"} ne "upload_assets" 1 4 5
Condition66%4/6
$raw_fg or $opt{"resource"} ne "workers" or $opt{"action"} ne "upload_assets"
l!l&&r!l&&!r
104
$raw_fg or $opt{"resource"} ne "workers"
l!l&&r!l&&!r
014
MC/DC33%1/3
$raw_fg or $opt{"resource"} ne "workers" or $opt{"action"} ne "upload_assets"
$raw_fg $opt{"resource"} ne "workers" $opt{"action"} ne "upload_assets"
Truth table50%2/4
A or B or C
A= $raw_fgB= $opt{"resource"} ne "workers"C= $opt{"action"} ne "upload_assets"
ABCresult
1XX1
01X1
0011
0000
301 4 ▶ die "asset list options require one Worker name and no other source argument\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @arg == 1 and defined $arg[0] and not ref $arg[0] 1 3 4
Condition66%4/6
@arg == 1 and defined $arg[0] and not ref $arg[0]
!ll&&!rl&&r
103
@arg == 1 and defined $arg[0]
!ll&&!rl&&r
103
MC/DC33%1/3
@arg == 1 and defined $arg[0] and not ref $arg[0]
@arg == 1 defined $arg[0] not ref $arg[0]
Truth table50%2/4
A and B and not(C)
A= @arg == 1B= defined $arg[0]C= not ref $arg[0]
ABCresult
0XX0
10X0
1100
1111
302 unless @arg==1&&defined($arg[0])&&!ref($arg[0]);
303 die "--asset-list-stdin may be used only once\n"
304 3 ▶ if 1<grep { $_->[0] eq 'stdin' } @asset_source;
Branch: 1 branch
BranchTrueFalseTotal
if 1 < grep({$_->[0] eq "stdin";} @asset_source) 1 2 3
305 2 my @asset;
306 2 foreach my $source_ar (@asset_source) {
307 6 my ($type, $value)=@$source_ar;
308 6 ▶ if ($type eq 'file') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'file') { } 2 4 6
elsif ($type eq 'json') { } 1 3 4
elsif ($type eq 'text') { } 2 1 3
309 2 push(@asset, $value);
310 }
311 elsif ($type eq 'json') {
312 1 push(@asset, @{typed_value('array', read_file($value))});
313 }
314 elsif ($type eq 'text') {
315 2 ▶ open(my $asset_fh, '<', $value) || die "unable to open $value: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $asset_fh, "<", $value or die "unable to open ${value}: $!\n" 2 0 2
316 2 push(@asset, @{read_asset_lines($asset_fh)});
317 2 ▶ close($asset_fh) || die "unable to close $value: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $asset_fh or die "unable to close ${value}: $!\n" 2 0 2
318 }
319 else {
320 1 push(@asset, @{read_asset_lines(\*STDIN)});
321 }
322 }
323 2 ▶ die "asset list is empty\n" unless @asset;
Branch: 1 branch
BranchTrueFalseTotal
unless @asset 0 2 2
324 2 push(@arg, \@asset);
325 }
326  
327  
328  
329 # Reject missing or excess method arguments before authentication or I/O
330 #
331 26 ▶ unless ($raw_fg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw_fg) 26 0 26
332 26 my $method_arg_ar=$resource_method{$opt{'resource'}}{$opt{'action'}};
333 26 my ($minimum, $maximum)=@$method_arg_ar;
334 26 ▶ die "action '$opt{'action'}' for resource '$opt{'resource'}' requires at least " .
Branch: 2 branches
BranchTrueFalseTotal
if @arg < $minimum 1 25 26
$minimum == 1 ? : 1 0 1
335 "$minimum --arg value".($minimum==1 ? '' : 's')."; ".scalar(@arg)." supplied\n"
336 if @arg<$minimum;
337 25 ▶ die "action '$opt{'action'}' for resource '$opt{'resource'}' accepts at most " .
Branch: 2 branches
BranchTrueFalseTotal
if @arg > $maximum 1 24 25
$maximum == 1 ? : 1 0 1
338 "$maximum --arg value".($maximum==1 ? '' : 's')."; ".scalar(@arg)." supplied\n"
339 if @arg>$maximum;
340 }
341  
342  
343  
344 # Validate the composite Worker inspection before resolving credentials
345 #
346 24 ▶ if (!$raw_fg&&$opt{'resource'} eq 'workers'&&$opt{'action'} eq 'inspect_script') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $raw_fg and $opt{"resource"} eq "workers" and $opt{"action"} eq "inspect_script") 5 19 24
Condition83%5/6
not $raw_fg and $opt{"resource"} eq "workers" and $opt{"action"} eq "inspect_script"
!ll&&!rl&&r
1185
not $raw_fg and $opt{"resource"} eq "workers"
!ll&&!rl&&r
01113
MC/DC66%2/3
not $raw_fg and $opt{"resource"} eq "workers" and $opt{"action"} eq "inspect_script"
not $raw_fg $opt{"resource"} eq "workers" $opt{"action"} eq "inspect_script"
Truth table75%3/4
not(A) and B and C
A= not $raw_fgB= $opt{"resource"} eq "workers"C= $opt{"action"} eq "inspect_script"
ABCresult
0XX0
10X0
1100
1111
347 5 ▶ die "full_response is unavailable for inspect_script\n" if $opt{'full-response'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"full-response"} 1 4 5
348 4 my @unknown=sort(grep { $_!~/\A(?:name|tag|etag)\z/ } keys(%param));
349 4 ▶ die "unknown inspect selector: $unknown[0]\n" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 3 4
350 3 my @selector=grep { exists($param{$_}) } qw(name tag etag);
351 3 ▶ die "inspect_script requires exactly one of name, tag, or etag\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @selector == 1 2 1 3
352 unless @selector==1;
353 1 my $selector=$selector[0];
354 1 my $value=$param{$selector};
355 1 ▶ die "$selector must be a non-empty scalar\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and length $value 0 1 1
Condition33%2/6
defined $value and not ref $value and length $value
!ll&&!rl&&r
001
defined $value and not ref $value
!ll&&!rl&&r
001
MC/DC0%0/3
defined $value and not ref $value and length $value
defined $value not ref $value length $value
Truth table25%1/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value
ABCresult
0XX0
10X0
1100
1111
356 unless defined($value)&&!ref($value)&&length($value);
357 }
358  
359  
360 # Dump parsed options without needing a Cloudflare token
361 #
362 20 ▶ if ($opt{'dump_opt'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"dump_opt"}) 17 3 20
363 die "dump_opt is unsafe for secret-bearing actions\n"
364 if !$raw_fg&&(($opt{'resource'} eq 'secrets_store'&&
365 $opt{'action'}=~/\A(?:create_secret|update_secret)\z/)||
366 ($opt{'resource'} eq 'workers'&&$opt{'action'} eq 'add_secret')||
367 ($opt{'resource'} eq 'hyperdrive'&&
368 17 ▶ $opt{'action'}=~/\A(?:create_config|replace_config|update_config)\z/));
Branch: 1 branch
BranchTrueFalseTotal
if not $raw_fg and $opt{"resource"} eq "secrets_store" && $opt{"action"} =~ /\A(?:create_secret|update_secret)\z/ || $opt{"resource"} eq "workers" && $opt{"action"} eq "add_secret" || $opt{"resource"} eq "hyperdrive" && $opt{"action"} =~ /\A(?:create_config|replace_config|update_config)\z/ 1 16 17
Condition61%11/18
not $raw_fg and $opt{"resource"} eq "secrets_store" && $opt{"action"} =~ /\A(?:create_secret|update_secret)\z/ || $opt{"resource"} eq "workers" && $opt{"action"} eq "add_secret" || $opt{"resource"} eq "hyperdrive" && $opt{"action"} =~ /\A(?:create_config|replace_config|update_config)\z/
!ll&&!rl&&r
0161
$opt{"resource"} eq "secrets_store" and $opt{"action"} =~ /\A(?:create_secret|update_secret)\z/ or $opt{"resource"} eq "workers" and $opt{"action"} eq "add_secret" or $opt{"resource"} eq "hyperdrive" and $opt{"action"} =~ /\A(?:create_config|replace_config|update_config)\z/
l!l&&r!l&&!r
1016
$opt{"resource"} eq "secrets_store" and $opt{"action"} =~ /\A(?:create_secret|update_secret)\z/ or $opt{"resource"} eq "workers" and $opt{"action"} eq "add_secret"
l!l&&r!l&&!r
1016
$opt{"resource"} eq "secrets_store" and $opt{"action"} =~ /\A(?:create_secret|update_secret)\z/
!ll&&!rl&&r
1601
$opt{"resource"} eq "workers" and $opt{"action"} eq "add_secret"
!ll&&!rl&&r
880
$opt{"resource"} eq "hyperdrive" and $opt{"action"} =~ /\A(?:create_config|replace_config|update_config)\z/
!ll&&!rl&&r
1600
MC/DC14%1/7
not $raw_fg and $opt{"resource"} eq "secrets_store" && $opt{"action"} =~ /\A(?:create_secret|update_secret)\z/ || $opt{"resource"} eq "workers" && $opt{"action"} eq "add_secret" || $opt{"resource"} eq "hyperdrive" && $opt{"action"} =~ /\A(?:create_config|replace_config|update_config)\z/
not $raw_fg $opt{"resource"} eq "secrets_store" $opt{"action"} =~ /\A(?:create_secret|update_secret)\z/ $opt{"resource"} eq "workers" $opt{"action"} eq "add_secret" $opt{"resource"} eq "hyperdrive" $opt{"action"} =~ /\A(?:create_config|replace_config|update_config)\z/
Truth table18%3/16
not(A) and B and C or D and E or F and G
A= not $raw_fgB= $opt{"resource"} eq "secrets_store"C= $opt{"action"} =~ /\A(?:create_secret|update_secret)\z/D= $opt{"resource"} eq "workers"E= $opt{"action"} eq "add_secret"F= $opt{"resource"} eq "hyperdrive"G= $opt{"action"} =~ /\A(?:create_config|replace_config|update_config)\z/
ABCDEFGresult
0XXXXXX0
10X0X0X0
10X0X100
10X100X0
10X10100
1100X0X0
1100X100
110100X0
11010100
111XXXX1
10X11XX1
11011XX1
10X0X111
10X10111
1100X111
11010111
369 16 my $out_hr={ options => \%opt, arguments => \@arg, parameters => \%param };
370 16 print Data::Dumper::Dumper($out_hr);
371 16 return 0;
372 }
373  
374  
375 # Resolve optional Wrangler authentication before creating the client
376 #
377 3 my %client_opt;
378 3 ▶ $client_opt{'account_id'}=$opt{'account-id'} if defined($opt{'account-id'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"account-id"} 1 2 3
379 3 ▶ if (defined($opt{'auth'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"auth"}) 3 0 3
380 3 $client_opt{'token'}=wrangler_token();
381 $client_opt{'account_id'}=wrangler_account_id()
382 if !$raw_fg&&$opt{'resource'}!~/\A(?:accounts|zones)\z/&&
383 !($opt{'resource'} eq 'workers'&&
384 $opt{'action'}=~/\A(?:list|create|update|delete)_route(?:s)?\z/)&&
385 3 ▶ !defined($opt{'account-id'})&&!defined($ENV{'CLOUDFLARE_ACCOUNT_ID'});
Branch: 1 branch
BranchTrueFalseTotal
unless $raw_fg or $opt{"resource"} =~ /\A(?:accounts|zones)\z/ or $opt{"resource"} eq "workers" and $opt{"action"} =~ /\A(?:list|create|update|delete)_route(?:s)?\z/ or defined $opt{"account-id"} or defined $ENV{"CLOUDFLARE_ACCOUNT_ID"} 1 2 3
Condition66%10/15
$raw_fg or $opt{"resource"} =~ /\A(?:accounts|zones)\z/ or $opt{"resource"} eq "workers" and $opt{"action"} =~ /\A(?:list|create|update|delete)_route(?:s)?\z/ or defined $opt{"account-id"} or defined $ENV{"CLOUDFLARE_ACCOUNT_ID"}
l!l&&r!l&&!r
201
$raw_fg or $opt{"resource"} =~ /\A(?:accounts|zones)\z/ or $opt{"resource"} eq "workers" and $opt{"action"} =~ /\A(?:list|create|update|delete)_route(?:s)?\z/ or defined $opt{"account-id"}
l!l&&r!l&&!r
111
$raw_fg or $opt{"resource"} =~ /\A(?:accounts|zones)\z/ or $opt{"resource"} eq "workers" and $opt{"action"} =~ /\A(?:list|create|update|delete)_route(?:s)?\z/
l!l&&r!l&&!r
012
$raw_fg or $opt{"resource"} =~ /\A(?:accounts|zones)\z/
l!l&&r!l&&!r
003
$opt{"resource"} eq "workers" and $opt{"action"} =~ /\A(?:list|create|update|delete)_route(?:s)?\z/
!ll&&!rl&&r
201
MC/DC33%2/6
$raw_fg or $opt{"resource"} =~ /\A(?:accounts|zones)\z/ or $opt{"resource"} eq "workers" and $opt{"action"} =~ /\A(?:list|create|update|delete)_route(?:s)?\z/ or defined $opt{"account-id"} or defined $ENV{"CLOUDFLARE_ACCOUNT_ID"}
$raw_fg $opt{"resource"} =~ /\A(?:accounts|zones)\z/ $opt{"resource"} eq "workers" $opt{"action"} =~ /\A(?:list|create|update|delete)_route(?:s)?\z/ defined $opt{"account-id"} defined $ENV{"CLOUDFLARE_ACCOUNT_ID"}
Truth table33%3/9
A or B or C and D or E or F
A= $raw_fgB= $opt{"resource"} =~ /\A(?:accounts|zones)\z/C= $opt{"resource"} eq "workers"D= $opt{"action"} =~ /\A(?:list|create|update|delete)_route(?:s)?\z/E= defined $opt{"account-id"}F= defined $ENV{"CLOUDFLARE_ACCOUNT_ID"}
ABCDEFresult
1XXXXX1
01XXXX1
0011XX1
000X1X1
00101X1
000X011
0010011
000X000
0010000
386 }
387 3 my $api_or=Cloudflare::API->new(%client_opt);
388 3 ▶ my $invoke_cr=sub {
Subroutine __ANON__: called CC=5 SCAR=21.5
389 3 my ($query_hr, $full)=@_;
390 3 ▶ if ($raw_fg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_fg) 0 3 3
391 0 my %request=(query => $query_hr, full_response => $full);
392 0 ▶ $request{'json'}=$arg[0] if @arg;
Branch: 1 branch
BranchTrueFalseTotal
if @arg 0 0 0
393 0 return $api_or->request(uc($opt{'method'}), $opt{'path'}, %request);
394 }
395 3 my $resource=$opt{'resource'};
396 3 my $object_or=$api_or->$resource();
397 3 my $action=$opt{'action'};
398 3 ▶ if ($action=~/\A(?:list(?:_|\z)|search_scripts\z)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action =~ /\A(?:list(?:_|\z)|search_scripts\z)/) 3 0 3
399 3 my $page_action=$action.'_page_response';
400 3 my $response_hr=$object_or->$page_action(@arg, %$query_hr);
401 0 ▶ return $full ? $response_hr : $response_hr->{'result'};
Branch: 1 branch
BranchTrueFalseTotal
$full ? : 0 0 0
402 }
403 0 return $object_or->$action(@arg, %$query_hr, full_response => $full);
404 3 };
405  
406  
407 # Follow list pages when requested, retaining each page boundary
408 #
409 3 my $result;
410 3 ▶ if ($opt{'paginate'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'paginate'}) { } 0 3 3
411 0 my @pages;
412 0 my $query_hr={ %param };
413 0 my %seen;
414 0 ▶ my $page=defined($query_hr->{'page'}) ? $query_hr->{'page'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $query_hr->{'page'} ? : 0 0 0
415 0 while (1) {
416 0 my $envelope_hr=$invoke_cr->($query_hr, 1);
417 0 ▶ push(@pages, $opt{'full-response'} ? $envelope_hr : $envelope_hr->{'result'});
Branch: 1 branch
BranchTrueFalseTotal
$opt{'full-response'} ? : 0 0 0
418 0 ▶ last if defined($opt{'max-pages'})&&@pages>=$opt{'max-pages'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"max-pages"} and @pages >= $opt{"max-pages"} 0 0 0
Condition0%0/3
defined $opt{"max-pages"} and @pages >= $opt{"max-pages"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt{"max-pages"} and @pages >= $opt{"max-pages"}
defined $opt{"max-pages"} @pages >= $opt{"max-pages"}
Truth table0%0/3
A and B
A= defined $opt{"max-pages"}B= @pages >= $opt{"max-pages"}
ABresult
0X0
100
111
419 0 my $next_hr=next_query($query_hr, $envelope_hr, $page, \%seen);
420 0 ▶ last unless $next_hr;
Branch: 1 branch
BranchTrueFalseTotal
unless $next_hr 0 0 0
421 0 $query_hr=$next_hr;
422 0 ▶ $page=defined($query_hr->{'page'}) ? $query_hr->{'page'} : $page+1;
Branch: 1 branch
BranchTrueFalseTotal
defined $query_hr->{'page'} ? : 0 0 0
423 }
424 0 $result=\@pages;
425 }
426 else {
427 3 $result=$invoke_cr->(\%param, $opt{'full-response'});
428 }
429  
430  
431 # Print the result in the requested format
432 #
433 0 ▶ if ($opt{'output'} eq 'dumper') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'output'} eq 'dumper') { } 0 0 0
434 0 print Data::Dumper::Dumper($result);
435 }
436 else {
437 0 print JSON::PP->new()->canonical()->pretty()->encode($result);
438 }
439  
440  
441 # Done
442 #
443 0 return 0;
444  
445 }
446  
447  
448 9 ▶sub wrangler_token {
Subroutine wrangler_token: called CC=6 SCAR=18.6
449  
450  
451 # Ask Wrangler to refresh its login and return the selected bearer token
452 #
453 9 my $credential_hr=wrangler_json('auth token', qw(auth token --json));
454 die "wrangler auth token returned unsupported credential type\n"
455 unless defined($credential_hr->{'type'})&&
456 7 ▶ $credential_hr->{'type'}=~/\A(?:api_token|oauth)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $credential_hr->{"type"} and $credential_hr->{"type"} =~ /\A(?:api_token|oauth)\z/ 1 6 7
Condition66%2/3
defined $credential_hr->{"type"} and $credential_hr->{"type"} =~ /\A(?:api_token|oauth)\z/
!ll&&!rl&&r
016
MC/DC50%1/2
defined $credential_hr->{"type"} and $credential_hr->{"type"} =~ /\A(?:api_token|oauth)\z/
defined $credential_hr->{"type"} $credential_hr->{"type"} =~ /\A(?:api_token|oauth)\z/
Truth table66%2/3
A and B
A= defined $credential_hr->{"type"}B= $credential_hr->{"type"} =~ /\A(?:api_token|oauth)\z/
ABresult
0X0
100
111
457 6 my $token=$credential_hr->{'token'};
458 6 ▶ die "wrangler auth token returned no usable token\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and not ref $token and $token =~ /\A[^\x00-\x20\x7f]+\z/ 1 5 6
Condition50%3/6
defined $token and not ref $token and $token =~ /\A[^\x00-\x20\x7f]+\z/
!ll&&!rl&&r
015
defined $token and not ref $token
!ll&&!rl&&r
006
MC/DC33%1/3
defined $token and not ref $token and $token =~ /\A[^\x00-\x20\x7f]+\z/
defined $token not ref $token $token =~ /\A[^\x00-\x20\x7f]+\z/
Truth table50%2/4
A and not(B) and C
A= defined $tokenB= not ref $tokenC= $token =~ /\A[^\x00-\x20\x7f]+\z/
ABCresult
0XX0
10X0
1100
1111
459 unless defined($token)&&!ref($token)&&$token=~/\A[^\x00-\x20\x7f]+\z/;
460 5 return $token;
461  
462 }
463  
464  
465 5 ▶sub wrangler_account_id {
Subroutine wrangler_account_id: called CC=8 SCAR=21.7
466  
467  
468 # Select the only account available through the active Wrangler login
469 #
470 5 my $user_hr=wrangler_json('whoami', qw(whoami --json));
471 5 my $account_ar=$user_hr->{'accounts'};
472 5 ▶ die "wrangler whoami returned no account list\n" unless ref($account_ar) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $account_ar eq "ARRAY" 1 4 5
473 4 ▶ die "wrangler authentication has no available accounts\n" unless @$account_ar;
Branch: 1 branch
BranchTrueFalseTotal
unless @$account_ar 1 3 4
474 3 ▶ die "wrangler authentication has multiple accounts; use --account-id or CLOUDFLARE_ACCOUNT_ID\n"
Branch: 1 branch
BranchTrueFalseTotal
if @$account_ar > 1 1 2 3
475 if @$account_ar>1;
476 2 my $account_hr=$account_ar->[0];
477 2 ▶ die "wrangler whoami returned an invalid account\n" unless ref($account_hr) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $account_hr eq "HASH" 0 2 2
478 2 my $account_id=$account_hr->{'id'};
479 2 ▶ die "wrangler whoami returned no usable account ID\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $account_id and not ref $account_id and $account_id =~ /\A[^\x00-\x20\x7f]+\z/ 0 2 2
Condition33%2/6
defined $account_id and not ref $account_id and $account_id =~ /\A[^\x00-\x20\x7f]+\z/
!ll&&!rl&&r
002
defined $account_id and not ref $account_id
!ll&&!rl&&r
002
MC/DC0%0/3
defined $account_id and not ref $account_id and $account_id =~ /\A[^\x00-\x20\x7f]+\z/
defined $account_id not ref $account_id $account_id =~ /\A[^\x00-\x20\x7f]+\z/
Truth table25%1/4
A and not(B) and C
A= defined $account_idB= not ref $account_idC= $account_id =~ /\A[^\x00-\x20\x7f]+\z/
ABCresult
0XX0
10X0
1100
1111
480 unless defined($account_id)&&!ref($account_id)&&
481 $account_id=~/\A[^\x00-\x20\x7f]+\z/;
482 2 return $account_id;
483  
484 }
485  
486  
487 14 ▶sub wrangler_json {
Subroutine wrangler_json: called CC=6 SCAR=18.0
488  
489  
490 # Run a Wrangler JSON command without a shell or exposing diagnostic output
491 #
492 14 my ($description, @arg)=@_;
493 14 my $null_fn=File::Spec->devnull();
494 14 ▶ open(my $error_fh, '>', $null_fn) || die "unable to open null device: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $error_fh, ">", $null_fn or die "unable to open null device: $!\n" 14 0 14
495 14 my $output_fh;
496 14 my $pid=eval { open3(undef, $output_fh, $error_fh, 'wrangler', @arg) };
497 14 ▶ die "unable to start wrangler $description\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 14 14
498 14 local $/;
499 14 my $output=<$output_fh>;
500 14 close($output_fh);
501 14 waitpid($pid, 0);
502 14 ▶ die "wrangler $description failed; check Wrangler login and configuration\n" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 1 13 14
503  
504 13 my $result_hr=eval { JSON::PP->new()->decode($output) };
505 13 ▶ die "wrangler $description returned invalid JSON\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ or ref $result_hr ne "HASH" 1 12 13
Condition66%2/3
$@ or ref $result_hr ne "HASH"
l!l&&r!l&&!r
1012
MC/DC50%1/2
$@ or ref $result_hr ne "HASH"
$@ ref $result_hr ne "HASH"
Truth table66%2/3
A or B
A= $@B= ref $result_hr ne "HASH"
ABresult
1X1
011
000
506 if $@||ref($result_hr) ne 'HASH';
507 12 return $result_hr;
508  
509 }
510  
511  
512 3 ▶sub read_file {
Subroutine read_file: called CC=3 SCAR=11.2
513  
514 # Read JSON or a trusted Data::Dumper file as a single value
515 #
516 3 my $path_fn=shift();
517 3 ▶ open(my $file_fh, '<', $path_fn) || die "unable to open $path_fn: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $file_fh, "<", $path_fn or die "unable to open ${path_fn}: $!\n" 3 0 3
518 3 local $/;
519 3 my $content=<$file_fh>;
520 3 ▶ close($file_fh) || die "unable to close $path_fn: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $file_fh or die "unable to close ${path_fn}: $!\n" 3 0 3
521 3 return $content;
522  
523 }
524  
525  
526 3 ▶sub read_asset_lines {
Subroutine read_asset_lines: called CC=4 SCAR=13.9
527  
528  
529 # Keep filename whitespace intact; only empty lines are separators
530 #
531 3 my $file_fh=shift();
532 3 my @file;
533 3 ▶ while (my $path_fn=<$file_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $path_fn = readline $file_fh)) 9 3 12
534 9 chomp($path_fn);
535 9 $path_fn=~s/\r\z//;
536 9 ▶ next unless length($path_fn);
Branch: 1 branch
BranchTrueFalseTotal
unless length $path_fn 3 6 9
537 6 push(@file, $path_fn);
538 }
539 3 ▶ die "unable to read asset list: $!\n" if !eof($file_fh);
Branch: 1 branch
BranchTrueFalseTotal
unless eof $file_fh 0 3 3
540 3 return \@file;
541  
542 }
543  
544  
545 33 ▶sub typed_value {
Subroutine typed_value: called CC=13 SCAR=25.7
546  
547 # Keep strings literal; decode structured and boolean arguments
548 #
549 33 my ($type, $value)=@_;
550 33 ▶ return $value if $type eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "string" 20 13 33
551 13 ▶ if ($type eq 'bool') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "bool") 3 10 13
552 3 ▶ die "boolean must be true or false\n" unless $value=~/\A(?:true|false)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A(?:true|false)\z/i 1 2 3
553 2 ▶ return $value=~/\Atrue\z/i ? JSON::PP::true : JSON::PP::false;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\Atrue\z/i ? : 1 1 2
554 }
555 10 ▶ if ($type eq 'json-file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "json-file") 1 9 10
556 1 $value=read_file($value);
557 1 $type='json';
558 }
559 10 ▶ if ($type eq 'dumper-file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "dumper-file") 1 9 10
560 # This executes Perl, so the caller must supply a trusted file
561 #
562 1 my $source=read_file($value);
563 1 ▶ my $result=eval 'no strict; my $VAR1; '.$source;
Subroutine BEGIN: called CC=1 SCAR=0.0
564 1 ▶ die "unable to evaluate trusted Data::Dumper file $value: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
565 1 return $result;
566 }
567 9 my $decoded=eval { JSON::PP->new()->decode($value) };
568 9 ▶ die "invalid JSON: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
569 9 ▶ die "$type requires a JSON array\n" if $type eq 'array'&&ref($decoded) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" and ref $decoded ne "ARRAY" 1 8 9
Condition100%3/3
$type eq "array" and ref $decoded ne "ARRAY"
!ll&&!rl&&r
531
MC/DC100%2/2
$type eq "array" and ref $decoded ne "ARRAY"
$type eq "array" ref $decoded ne "ARRAY"
Truth table100%3/3
A and B
A= $type eq "array"B= ref $decoded ne "ARRAY"
ABresult
0X0
100
111
570 8 ▶ die "$type requires a JSON object\n" if $type eq 'hash'&&ref($decoded) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" and ref $decoded ne "HASH" 0 8 8
Condition66%2/3
$type eq "hash" and ref $decoded ne "HASH"
!ll&&!rl&&r
710
MC/DC0%0/2
$type eq "hash" and ref $decoded ne "HASH"
$type eq "hash" ref $decoded ne "HASH"
Truth table66%2/3
A and B
A= $type eq "hash"B= ref $decoded ne "HASH"
ABresult
0X0
100
111
571 8 return $decoded;
572  
573 }
574  
575  
576 9 ▶sub parse_named {
Subroutine parse_named: called CC=3 SCAR=12.0
577  
578 # Split only at the first equals sign so values can contain equals signs
579 #
580 9 my ($type, $input)=@_;
581 9 my ($name, $value)=split(/=/, $input, 2);
582 9 ▶ die "named parameter must be NAME=VALUE\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ 0 9 9
Condition33%1/3
defined $value and $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
!ll&&!rl&&r
009
MC/DC0%0/2
defined $value and $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
defined $value $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Truth table33%1/3
A and B
A= defined $valueB= $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
ABresult
0X0
100
111
583 unless defined($value)&&$name=~/\A[A-Za-z_][A-Za-z0-9_]*\z/;
584 9 return ($name, typed_value($type, $value));
585  
586 }
587  
588  
589 5 ▶sub next_query {
Subroutine next_query: called CC=11 SCAR=24.4
590  
591 # Prefer a cursor when Cloudflare supplies one
592 #
593 5 my ($query_hr, $envelope_hr, $page, $seen_hr)=@_;
594 5 my $info_hr=$envelope_hr->{'result_info'};
595 5 ▶ return unless ref($info_hr) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $info_hr eq "HASH" 0 5 5
596 5 ▶ if (defined($info_hr->{'cursor'})&&length($info_hr->{'cursor'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info_hr->{"cursor"} and length $info_hr->{"cursor"}) 2 3 5
Condition66%2/3
defined $info_hr->{"cursor"} and length $info_hr->{"cursor"}
!ll&&!rl&&r
302
MC/DC50%1/2
defined $info_hr->{"cursor"} and length $info_hr->{"cursor"}
defined $info_hr->{"cursor"} length $info_hr->{"cursor"}
Truth table66%2/3
A and B
A= defined $info_hr->{"cursor"}B= length $info_hr->{"cursor"}
ABresult
0X0
100
111
597 2 my $cursor=$info_hr->{'cursor'};
598 2 ▶ die "pagination cursor repeated\n" if $seen_hr->{$cursor}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_hr->{$cursor}++ 1 1 2
599 1 return { %$query_hr, cursor => $cursor };
600 }
601  
602  
603 # Otherwise calculate the next numbered page, if any
604 #
605 3 my $total=$info_hr->{'total_pages'};
606 3 ▶ if (!defined($total)&&defined($info_hr->{'total_count'})&&defined($info_hr->{'per_page'})&&
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $total and defined $info_hr->{"total_count"} and defined $info_hr->{"per_page"} and $info_hr->{"per_page"} > 0) 1 2 3
Condition66%6/9
not defined $total and defined $info_hr->{"total_count"} and defined $info_hr->{"per_page"} and $info_hr->{"per_page"} > 0
!ll&&!rl&&r
201
not defined $total and defined $info_hr->{"total_count"} and defined $info_hr->{"per_page"}
!ll&&!rl&&r
201
not defined $total and defined $info_hr->{"total_count"}
!ll&&!rl&&r
201
MC/DC25%1/4
not defined $total and defined $info_hr->{"total_count"} and defined $info_hr->{"per_page"} and $info_hr->{"per_page"} > 0
not defined $total defined $info_hr->{"total_count"} defined $info_hr->{"per_page"} $info_hr->{"per_page"} > 0
Truth table40%2/5
not(A) and B and C and D
A= not defined $totalB= defined $info_hr->{"total_count"}C= defined $info_hr->{"per_page"}D= $info_hr->{"per_page"} > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
607 $info_hr->{'per_page'}>0) {
608 1 $total=int(($info_hr->{'total_count'}+$info_hr->{'per_page'}-1)/$info_hr->{'per_page'});
609 }
610 3 ▶ return unless defined($total)&&$page<$total;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $total and $page < $total 1 2 3
Condition66%2/3
defined $total and $page < $total
!ll&&!rl&&r
012
MC/DC50%1/2
defined $total and $page < $total
defined $total $page < $total
Truth table66%2/3
A and B
A= defined $totalB= $page < $total
ABresult
0X0
100
111
611 2 return { %$query_hr, page => $page+1 };
612  
613 }
614  
615  
616 1;
617  
618 __END__
↑ Summary blib/lib/Cloudflare/API/Workers.pm »