blib/lib/Cloudflare/API/CLI/Completion.pm

filter: Statementstmt 100% 123 / 123 Branchbran 84.2% 32 / 38 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 16.6% 1 / 6 totaltotal 91.7% 166 / 181 CC 29 SCAR 23.1
CC
29
Cov
95%
fish
16.0
generate
10.0
bash
3.0
CRAP 10.1
« blib/lib/Cloudflare/API.pm ↑ Summary blib/lib/Cloudflare/API/Error.pm »
1 #
2 # This file is part of Cloudflare::API.
3 #
4 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 # Full license text is available at:
10 #
11 # <http://dev.perl.org/licenses/>
12 #
13  
14  
15 #
16 # Render self-contained shell completion for cloudflare-api
17 #
18 package Cloudflare::API::CLI::Completion;
19  
20  
21 # Compiler pragmas
22 #
23 4 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 # Version information
29 #
30 4 $VERSION='1.011';
31  
32  
33 #============================================================================
34  
35  
36 4 ▶sub generate {
Subroutine generate: called CC=9 SCAR=23.0
Pod documented
37  
38  
39 # Check the requested shell and command specification
40 #
41 4 my ($shell, $spec_hr)=@_;
42 4 ▶ die "completion shell is required; valid shells: bash, fish, zsh\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shell and length $shell 0 4 4
Condition33%1/3
defined $shell and length $shell
!ll&&!rl&&r
004
MC/DC0%0/2
defined $shell and length $shell
defined $shell length $shell
Truth table33%1/3
A and B
A= defined $shellB= length $shell
ABresult
0X0
100
111
43 unless defined($shell)&&length($shell);
44 4 ▶ die "unknown completion shell '$shell'; valid shells: bash, fish, zsh\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $shell =~ /\A(?:bash|fish|zsh)\z/ 1 3 4
45 unless $shell=~/\A(?:bash|fish|zsh)\z/;
46 3 ▶ die "completion specification must be a hash reference\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec_hr eq "HASH" 0 3 3
47 unless ref($spec_hr) eq 'HASH';
48 die "completion resources must be a hash reference\n"
49 3 ▶ unless ref($spec_hr->{'resources'}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec_hr->{"resources"} eq "HASH" 0 3 3
50 die "completion options must be an array reference\n"
51 3 ▶ unless ref($spec_hr->{'options'}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec_hr->{"options"} eq "ARRAY" 0 3 3
52  
53  
54 # Render the selected shell without consulting external state
55 #
56 3 ▶ return bash($spec_hr) if $shell eq 'bash';
Branch: 1 branch
BranchTrueFalseTotal
if $shell eq "bash" 1 2 3
57 2 ▶ return fish($spec_hr) if $shell eq 'fish';
Branch: 1 branch
BranchTrueFalseTotal
if $shell eq "fish" 1 1 2
58 1 return zsh($spec_hr);
59  
60 }
61  
62  
63 1 ▶sub bash {
Subroutine bash: called CC=3 SCAR=11.0
Pod undocumented
64  
65 1 my $spec_hr=shift();
66 1 my @resource=sort(keys(%{$spec_hr->{'resources'}}));
67 1 my @action=all_actions($spec_hr);
68 1 my @option=map { $_->[0] } @{$spec_hr->{'options'}};
69 1 my $resource=join(' ', @resource);
70 1 my $action=join(' ', @action);
71 1 my $option=join(' ', @option);
72 1 my $output=<<'BASH';
73 # bash completion for cloudflare-api
74 _cloudflare_api_actions()
75 {
76 case "$1" in
77 BASH
78 1 foreach my $name (@resource) {
79 9 my @name_action=sort(keys(%{$spec_hr->{'resources'}{$name}}));
80 9 $output.=' '.shell_quote($name).') printf \'%s\\n\'';
81 9 $output.=' '.join(' ', map { shell_quote($_) } @name_action)." ;;\n";
82 }
83 1 $output.=<<'BASH';
84 esac
85 }
86  
87 _cloudflare_api_option_kind()
88 {
89 case "$1" in
90 BASH
91 1 foreach my $option_ar (@{$spec_hr->{'options'}}) {
92 36 $output.=' '.shell_quote($option_ar->[0]).') printf \'%s\' '.
93 shell_quote($option_ar->[1])." ;;\n";
94 }
95 1 $output.=<<'BASH';
96 *) printf '%s' flag ;;
97 esac
98 }
99  
100 _cloudflare_api_complete()
101 {
102 local cur resource action expect word kind candidate option_value value i
103 local resources actions options
104 COMPREPLY=()
105 BASH
106 1 $output.=" resources=".shell_quote($resource)."\n";
107 1 $output.=" actions=".shell_quote($action)."\n";
108 1 $output.=" options=".shell_quote($option)."\n";
109 1 $output.=<<'BASH';
110 cur=${COMP_WORDS[COMP_CWORD]}
111  
112 for ((i=1; i<COMP_CWORD; i++)); do
113 word=${COMP_WORDS[i]}
114 if [[ -n $expect ]]; then
115 [[ $expect == resource ]] && resource=$word
116 [[ $expect == action ]] && action=$word
117 expect=
118 continue
119 fi
120 case "$word" in
121 --resource=*) resource=${word#*=} ;;
122 --action=*) action=${word#*=} ;;
123 --*=*) ;;
124 -*)
125 kind=$(_cloudflare_api_option_kind "$word")
126 [[ $kind != flag ]] && expect=$kind
127 ;;
128 *)
129 if [[ -z $resource ]]; then
130 resource=$word
131 elif [[ -z $action ]]; then
132 action=$word
133 fi
134 ;;
135 esac
136 done
137  
138 if [[ -n $expect ]]; then
139 case "$expect" in
140 resource) COMPREPLY=( $(compgen -W "$resources" -- "$cur") ) ;;
141 action)
142 candidate=$(_cloudflare_api_actions "$resource")
143 [[ -z $candidate ]] && candidate=$actions
144 COMPREPLY=( $(compgen -W "$candidate" -- "$cur") )
145 ;;
146 auth) COMPREPLY=( $(compgen -W 'wrangler' -- "$cur") ) ;;
147 output) COMPREPLY=( $(compgen -W 'json dumper' -- "$cur") ) ;;
148 shell) COMPREPLY=( $(compgen -W 'bash fish zsh' -- "$cur") ) ;;
149 method) COMPREPLY=( $(compgen -W 'DELETE GET PATCH POST PUT' -- "$cur") ) ;;
150 bool) COMPREPLY=( $(compgen -W 'false true' -- "$cur") ) ;;
151 file)
152 while IFS= read -r candidate; do
153 COMPREPLY[${#COMPREPLY[@]}]=$candidate
154 done < <(compgen -f -- "$cur")
155 ;;
156 esac
157 return
158 fi
159  
160 if [[ $cur == --*=* ]]; then
161 option_value=${cur%%=*}
162 value=${cur#*=}
163 kind=$(_cloudflare_api_option_kind "$option_value")
164 case "$kind" in
165 resource) candidate=$resources ;;
166 action)
167 candidate=$(_cloudflare_api_actions "$resource")
168 [[ -z $candidate ]] && candidate=$actions
169 ;;
170 auth) candidate=wrangler ;;
171 output) candidate='json dumper' ;;
172 shell) candidate='bash fish zsh' ;;
173 method) candidate='DELETE GET PATCH POST PUT' ;;
174 bool) candidate='false true' ;;
175 file)
176 while IFS= read -r candidate; do
177 COMPREPLY[${#COMPREPLY[@]}]="${option_value}=${candidate}"
178 done < <(compgen -f -- "$value")
179 return
180 ;;
181 *) return ;;
182 esac
183 for candidate in $(compgen -W "$candidate" -- "$value"); do
184 COMPREPLY[${#COMPREPLY[@]}]="${option_value}=${candidate}"
185 done
186 return
187 fi
188  
189 if [[ $cur == -* ]]; then
190 COMPREPLY=( $(compgen -W "$options" -- "$cur") )
191 elif [[ -z $resource ]]; then
192 COMPREPLY=( $(compgen -W "$resources" -- "$cur") )
193 elif [[ -z $action ]]; then
194 candidate=$(_cloudflare_api_actions "$resource")
195 COMPREPLY=( $(compgen -W "$candidate" -- "$cur") )
196 fi
197 }
198  
199 complete -F _cloudflare_api_complete cloudflare-api
200 BASH
201 1 return $output;
202  
203 }
204  
205  
206 1 ▶sub zsh {
Subroutine zsh: called CC=3 SCAR=11.0
Pod undocumented
207  
208 1 my $spec_hr=shift();
209 1 my @resource=sort(keys(%{$spec_hr->{'resources'}}));
210 1 my @action=all_actions($spec_hr);
211 1 my @option=map { $_->[0] } @{$spec_hr->{'options'}};
212 1 my $resource=join(' ', @resource);
213 1 my $action=join(' ', @action);
214 1 my $option=join(' ', @option);
215 1 my $output=<<'ZSH';
216 #compdef cloudflare-api
217  
218 _cloudflare_api_actions()
219 {
220 case "$1" in
221 ZSH
222 1 foreach my $name (@resource) {
223 9 my @name_action=sort(keys(%{$spec_hr->{'resources'}{$name}}));
224 9 $output.=' '.shell_quote($name).') print -l -- ';
225 9 $output.=join(' ', map { shell_quote($_) } @name_action)." ;;\n";
226 }
227 1 $output.=<<'ZSH';
228 esac
229 }
230  
231 _cloudflare_api_option_kind()
232 {
233 case "$1" in
234 ZSH
235 1 foreach my $option_ar (@{$spec_hr->{'options'}}) {
236 36 $output.=' '.shell_quote($option_ar->[0]).') print -rn -- '.
237 shell_quote($option_ar->[1])." ;;\n";
238 }
239 1 $output.=<<'ZSH';
240 *) print -rn -- flag ;;
241 esac
242 }
243  
244 _cloudflare_api()
245 {
246 local cur resource action expect word kind option_value value candidate
247 local -a resources actions options candidates
248 integer i
249 ZSH
250 1 $output.=" resources=(".join(' ', map { shell_quote($_) } @resource).")\n";
251 1 $output.=" actions=(".join(' ', map { shell_quote($_) } @action).")\n";
252 1 $output.=" options=(".join(' ', map { shell_quote($_) } @option).")\n";
253 1 $output.=<<'ZSH';
254 cur=$words[$CURRENT]
255  
256 for ((i=2; i<CURRENT; i++)); do
257 word=$words[$i]
258 if [[ -n $expect ]]; then
259 [[ $expect == resource ]] && resource=$word
260 [[ $expect == action ]] && action=$word
261 expect=
262 continue
263 fi
264 case "$word" in
265 --resource=*) resource=${word#*=} ;;
266 --action=*) action=${word#*=} ;;
267 --*=*) ;;
268 -*)
269 kind=$(_cloudflare_api_option_kind "$word")
270 [[ $kind != flag ]] && expect=$kind
271 ;;
272 *)
273 if [[ -z $resource ]]; then
274 resource=$word
275 elif [[ -z $action ]]; then
276 action=$word
277 fi
278 ;;
279 esac
280 done
281  
282 if [[ -n $expect ]]; then
283 case "$expect" in
284 resource) compadd -- $resources ;;
285 action)
286 candidates=( ${(f)"$(_cloudflare_api_actions "$resource")"} )
287 (( ${#candidates} )) || candidates=( $actions )
288 compadd -- $candidates
289 ;;
290 auth) compadd -- wrangler ;;
291 output) compadd -- json dumper ;;
292 shell) compadd -- bash fish zsh ;;
293 method) compadd -- DELETE GET PATCH POST PUT ;;
294 bool) compadd -- false true ;;
295 file) _files ;;
296 esac
297 return
298 fi
299  
300 if [[ $cur == --*=* ]]; then
301 option_value=${cur%%=*}
302 value=${cur#*=}
303 kind=$(_cloudflare_api_option_kind "$option_value")
304 case "$kind" in
305 resource) candidates=( $resources ) ;;
306 action)
307 candidates=( ${(f)"$(_cloudflare_api_actions "$resource")"} )
308 (( ${#candidates} )) || candidates=( $actions )
309 ;;
310 auth) candidates=( wrangler ) ;;
311 output) candidates=( json dumper ) ;;
312 shell) candidates=( bash fish zsh ) ;;
313 method) candidates=( DELETE GET PATCH POST PUT ) ;;
314 bool) candidates=( false true ) ;;
315 *) return ;;
316 esac
317 candidates=( ${^candidates/#/${option_value}=} )
318 compadd -- $candidates
319 return
320 fi
321  
322 if [[ $cur == -* ]]; then
323 compadd -- $options
324 elif [[ -z $resource ]]; then
325 compadd -- $resources
326 elif [[ -z $action ]]; then
327 candidates=( ${(f)"$(_cloudflare_api_actions "$resource")"} )
328 compadd -- $candidates
329 fi
330 }
331  
332 compdef _cloudflare_api cloudflare-api
333 ZSH
334 1 return $output;
335  
336 }
337  
338  
339 1 ▶sub fish {
Subroutine fish: called CC=16 SCAR=27.7
Pod undocumented
340  
341 1 my $spec_hr=shift();
342 1 my @resource=sort(keys(%{$spec_hr->{'resources'}}));
343 1 my @action=all_actions($spec_hr);
344 1 my $resource=join(' ', @resource);
345 1 my $action=join(' ', @action);
346 1 my $output=<<'FISH';
347 # fish completion for cloudflare-api
348 function __cloudflare_api_seen
349 set -l words (commandline -opc)
350 for word in $words
351 set word (string replace -r '^--resource=' '' -- $word)
352 set word (string replace -r '^--action=' '' -- $word)
353 contains -- $word $argv; and return 0
354 end
355 return 1
356 end
357  
358 complete -c cloudflare-api -f
359 FISH
360 1 $output.='complete -c cloudflare-api -n '.
361 shell_quote("not __cloudflare_api_seen $resource").' -a '.
362 shell_quote($resource).' -d '.shell_quote('resource')."\n";
363 1 foreach my $name (@resource) {
364 9 my @name_action=sort(keys(%{$spec_hr->{'resources'}{$name}}));
365 9 my $name_action=join(' ', @name_action);
366 9 my $condition="__cloudflare_api_seen $name; and not __cloudflare_api_seen $action";
367 9 $output.='complete -c cloudflare-api -n '.shell_quote($condition).' -a '.
368 shell_quote($name_action).' -d '.shell_quote("$name action")."\n";
369 }
370 1 foreach my $option_ar (@{$spec_hr->{'options'}}) {
371 36 my ($name, $kind, $description)=@$option_ar;
372 36 ▶ my $switch=$name=~/\A--(.+)\z/ ? '-l '.shell_quote($1) :
Branch: 2 branches
BranchTrueFalseTotal
$name =~ /\A--(.+)\z/ ? : 34 2 36
$name =~ /\A-(.)\z/ ? : 2 0 2
373 $name=~/\A-(.)\z/ ? '-s '.shell_quote($1) : next;
374 36 my $line='complete -c cloudflare-api '.$switch;
375 36 ▶ $line.=' -d '.shell_quote($description) if defined($description);
Branch: 1 branch
BranchTrueFalseTotal
if defined $description 36 0 36
376 36 ▶ if ($kind eq 'resource') {
Branch: 9 branches
BranchTrueFalseTotal
if ($kind eq 'resource') { } 1 35 36
elsif ($kind eq 'action') { } 1 34 35
elsif ($kind eq 'auth') { } 1 33 34
elsif ($kind eq 'output') { } 1 32 33
elsif ($kind eq 'shell') { } 1 31 32
elsif ($kind eq 'method') { } 1 30 31
elsif ($kind eq 'bool') { } 1 29 30
elsif ($kind eq 'file') { } 7 22 29
elsif ($kind ne 'flag') { } 11 11 22
377 1 $line.=' -r -f -a '.shell_quote($resource);
378 }
379 elsif ($kind eq 'action') {
380 1 foreach my $resource_name (@resource) {
381 9 my $actions=join(' ', sort(keys(%{$spec_hr->{'resources'}{$resource_name}})));
382 9 $output.=$line.' -r -f -n '.shell_quote("__cloudflare_api_seen $resource_name").
383 ' -a '.shell_quote($actions)."\n";
384 }
385 1 next;
386 }
387 elsif ($kind eq 'auth') {
388 1 $line.=' -r -f -a '.shell_quote('wrangler');
389 }
390 elsif ($kind eq 'output') {
391 1 $line.=' -r -f -a '.shell_quote('json dumper');
392 }
393 elsif ($kind eq 'shell') {
394 1 $line.=' -r -f -a '.shell_quote('bash fish zsh');
395 }
396 elsif ($kind eq 'method') {
397 1 $line.=' -r -f -a '.shell_quote('DELETE GET PATCH POST PUT');
398 }
399 elsif ($kind eq 'bool') {
400 1 $line.=' -r -f -a '.shell_quote('false true');
401 }
402 elsif ($kind eq 'file') {
403 7 $line.=' -r -F';
404 }
405 elsif ($kind ne 'flag') {
406 11 $line.=' -r -f';
407 }
408 35 $output.=$line."\n";
409 }
410 1 return $output;
411  
412 }
413  
414  
415 3 ▶sub all_actions {
Subroutine all_actions: called CC=2 SCAR=6.9
Pod undocumented
416  
417 3 my $spec_hr=shift();
418 3 my %action;
419 3 foreach my $resource (keys(%{$spec_hr->{'resources'}})) {
420 27 @action{keys(%{$spec_hr->{'resources'}{$resource}})}=();
421 }
422 3 return sort(keys(%action));
423  
424 }
425  
426  
427 548 ▶sub shell_quote {
Subroutine shell_quote: called CC=1 SCAR=0.0
Pod undocumented
428  
429 548 my $value=shift();
430 548 $value=~s/'/'"'"'/g;
431 548 return "'$value'";
432  
433 }
434  
435  
436 4 1;
437 __END__
« blib/lib/Cloudflare/API.pm ↑ Summary blib/lib/Cloudflare/API/Error.pm »