blib/lib/Langertha/Raider/CLI/Provider.pm

filter: Statementstmt 98.6% 144 / 146 Branchbran 92.5% 50 / 54 Conditioncond 56.2% 9 / 16 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 90.9% 20 / 22 Podpod 70.0% 7 / 10 totaltotal 90.2% 232 / 257 CC 45 SCAR 21.8
CC
45
Cov
94%
show
16.3
warnings
13.1
inspect
8.0
CRAP 8.8
« blib/lib/Langertha/Raider/Hall.pm ↑ Summary blib/lib/Langertha/Raider/CLI/REPL.pm »
1 package Langertha::Raider::CLI::Provider;
2 # ABSTRACT: Internal provider commands of the raider CLI: inspect a manifest, activate it for --provider
3 55 our $VERSION = '0.503';
4 55 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 55 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use Langertha::Manifest;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 55 ▶use Langertha::Manifest::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 55 ▶use Langertha::Raider::Provider::Activation;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 55 ▶use Langertha::Raider::Provider::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 55 has output => (
14 is => 'ro',
15 isa => 'Langertha::Raider::CLI::Output',
16 required => 1,
17 );
18  
19 has err => (
20 is => 'ro',
21 0 ▶ default => sub { \*STDERR },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 55 );
23  
24 has fetch_args => (
25 is => 'ro',
26 isa => 'HashRef',
27 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 55 );
29  
30 31 ▶sub fetch_class { 'Langertha::Raider::Provider::Fetch' }
Subroutine fetch_class: called CC=1 SCAR=0.0
Pod undocumented
31 11 ▶sub activation_class { 'Langertha::Raider::Provider::Activation' }
Subroutine activation_class: called CC=1 SCAR=0.0
Pod undocumented
32 13 ▶sub manifest_class { 'Langertha::Manifest' }
Subroutine manifest_class: called CC=1 SCAR=0.0
Pod undocumented
33  
34 55 has _known_capability => (
35 is => 'ro',
36 lazy => 1,
37 builder => '_build__known_capability',
38 );
39  
40 7 ▶sub _build__known_capability {
Subroutine _build__known_capability: called CC=1 SCAR=0.0
41 7 return { map { $_ => 1 } Langertha::Manifest::Builder->model_capabilities };
42 }
43  
44  
45 31 ▶sub fetcher {
Subroutine fetcher: called CC=1 SCAR=0.0
Pod documented
46 31 my ( $self, %args ) = @_;
47 31 return $self->fetch_class->new(%{ $self->fetch_args }, %args);
48 }
49  
50  
51 20 ▶sub inspect {
Subroutine inspect: called CC=8 SCAR=20.8
Pod documented
52 20 my ( $self, $target, %opt ) = @_;
53 20 ▶ my $fetch = $self->fetcher(allow_internal => $opt{allow_internal} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$opt{'allow_internal'} ? : 16 4 20
54 20 my $url = eval { $fetch->target_url($target) };
55 20 ▶ unless (defined $url) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url) 2 18 20
56 2 $self->_err('raider provider inspect: '.$self->output->error_text($@)."\n");
57 2 return 2;
58 }
59 18 my $report = $fetch->fetch_f($url)->get;
60 18 my $manifest;
61 18 ▶ if ($report->{status} eq 'completed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$report{"status"} eq "completed") 13 5 18
62 13 $manifest = eval { $self->manifest_class->from_json($report->{body}) };
63 13 ▶ unless ($manifest) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($manifest) 6 7 13
64 6 $report->{status} = 'failed';
65 6 $report->{error} = 'not a valid provider manifest: '.$self->output->error_text($@);
66 }
67 }
68 18 my $doc = $self->document($report, $manifest, $fetch);
69 18 ▶ if (my $machine = $opt{machine}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $machine = $opt{'machine'}) { } 10 8 18
elsif ($manifest) { } 3 5 8
70 10 $machine->write({ version => $machine->version, %$doc });
71 }
72 elsif ($manifest) {
73 3 $self->show($doc, $manifest);
74 }
75 else {
76 5 $self->_err('raider provider inspect: '.$doc->{status}.': '.$doc->{error}."\n");
77 }
78 18 ▶ return $manifest ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$manifest ? : 7 11 18
79 }
80  
81  
82 11 ▶sub activate {
Subroutine activate: called CC=7 SCAR=19.5
Pod documented
83 11 my ( $self, $target, %opt ) = @_;
84 my $activation = $self->activation_class->new(
85 11 ▶ fetch => $self->fetcher(allow_internal => $opt{allow_internal} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$opt{'allow_internal'} ? : 9 2 11
86 );
87 my $got = $activation->activate_f($target,
88 model => $opt{model},
89 11 ▶ has_api_key => $opt{has_api_key} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$opt{'has_api_key'} ? : 7 4 11
90 )->get;
91 11 ▶ unless ($got->{status} eq 'completed') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$got{"status"} eq "completed") 6 5 11
92 6 ▶ my $status = $got->{status} eq 'usage' ? '' : $got->{status}.': ';
Branch: 1 branch
BranchTrueFalseTotal
$$got{'status'} eq 'usage' ? : 4 2 6
93 6 $self->_err('raider --provider: '.$status.$self->output->error_text($got->{error})."\n");
94 6 ▶ return $got->{status} eq 'usage' ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$$got{'status'} eq 'usage' ? : 4 2 6
95 }
96 5 $self->_err('raider --provider: warning: '.$_."\n") for @{ $got->{warnings} };
97 5 return ( undef, $got );
98 }
99  
100  
101 18 ▶sub document {
Subroutine document: called CC=4 SCAR=14.0
Pod documented
102 18 my ( $self, $report, $manifest, $fetch ) = @_;
103 18 ▶ my %doc = map { exists $report->{$_} ? ( $_ => $report->{$_} ) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $$report{$_} ? : 112 32 144
104 qw( status url final_url redirects address error location elapsed );
105 18 ▶ if ($manifest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($manifest) 7 11 18
106 7 $doc{manifest} = $manifest->to_hash;
107 7 ▶ $doc{warnings} = [ $self->warnings($manifest, $fetch->origin($report->{final_url} // $report->{url}), $fetch) ];
Condition33%1/3
$$report{"final_url"} // $$report{"url"}
l!l&&r!l&&!r
700
MC/DC0%0/2
$$report{"final_url"} // $$report{"url"}
$$report{"final_url"} $$report{"url"}
Truth table33%1/3
A // B
A= $$report{"final_url"}B= $$report{"url"}
ABresult
1X1
011
000
108 7 $doc{notes} = [ $self->notes($manifest) ];
109 }
110 18 return \%doc;
111 }
112  
113  
114 7 ▶sub warnings {
Subroutine warnings: called CC=13 SCAR=25.7
Pod documented
115 7 my ( $self, $manifest, $origin, $fetch ) = @_;
116 7 my @warnings;
117 7 my $issuer = $fetch->origin($manifest->issuer);
118 7 ▶ push @warnings, 'issuer '.$manifest->issuer.' is not the origin the manifest came from ('.$origin.')'
Branch: 1 branch
BranchTrueFalseTotal
if defined $origin and ($issuer // "") ne $origin 2 5 7
Condition60%3/5
defined $origin and ($issuer // "") ne $origin
!ll&&!rl&&r
052
$issuer // ""
l!l
70
MC/DC0%0/2
defined $origin and ($issuer // "") ne $origin
defined $origin $issuer
Truth table33%1/3
A and B
A= defined $originB= $issuer
ABresult
0X0
100
111
119 if defined $origin && ( $issuer // '' ) ne $origin;
120 7 for my $endpoint (@{ $manifest->endpoints }) {
121 11 my $what = "endpoint '".$endpoint->id."'";
122 11 ▶ push @warnings, $what.": dialect '".$endpoint->dialect."' is unknown to this raider (no adapter for it)"
Branch: 1 branch
BranchTrueFalseTotal
unless $endpoint->is_known_dialect 2 9 11
123 unless $endpoint->is_known_dialect;
124 11 my $uri = URI->new($endpoint->base_url);
125 11 ▶ push @warnings, $what.': base_url is plain http, not https' if $uri->scheme eq 'http';
Branch: 1 branch
BranchTrueFalseTotal
if $uri->scheme eq "http" 1 10 11
126 11 my ( $kind ) = $fetch->address_kind($uri->host);
127 11 ▶ push @warnings, $what.': base_url points at a '.$kind.' address ('.$uri->host.')'
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "public" or $kind eq "invalid" 2 9 11
Condition66%2/3
$kind eq "public" or $kind eq "invalid"
l!l&&r!l&&!r
092
MC/DC50%1/2
$kind eq "public" or $kind eq "invalid"
$kind eq "public" $kind eq "invalid"
Truth table66%2/3
A or B
A= $kind eq "public"B= $kind eq "invalid"
ABresult
1X1
011
000
128 unless $kind eq 'public' || $kind eq 'invalid';
129 }
130 7 for my $auth (@{ $manifest->auth }) {
131 9 ▶ push @warnings, "auth '".$auth->id."': type '".$auth->type."' is unknown to this raider (it cannot supply it)"
Branch: 1 branch
BranchTrueFalseTotal
unless $auth->is_known_type 2 7 9
132 unless $auth->is_known_type;
133 }
134 7 my $known = $self->_known_capability;
135 7 for my $model (@{ $manifest->models }) {
136 7 for my $cap (grep { !$known->{$_} } sort keys %{ $model->capabilities }) {
137 2 push @warnings, "model '".$model->id."' on '".$model->endpoint_ref."': capability '".$cap
138 ."' is not one Langertha knows; treated as absent";
139 }
140 }
141 7 return @warnings;
142 }
143  
144  
145 7 ▶sub notes {
Subroutine notes: called CC=2 SCAR=6.9
Pod documented
146 7 my ( $self, $manifest ) = @_;
147 7 my @keys = sort keys %{ $manifest->extensions };
148 7 ▶ return unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 5 2 7
149 2 return 'extensions ('.join(', ', @keys).') are inert: kept as published, never interpreted, loaded or run';
150 }
151  
152  
153 3 ▶sub show {
Subroutine show: called CC=16 SCAR=27.9
Pod documented
154 3 my ( $self, $doc, $manifest ) = @_;
155 3 my $out = $self->output;
156 3 ▶ my $line = sub { $out->emit($out->c(meta => sprintf('%-11s ', $_[0])), @_[ 1 .. $#_ ], "\n") };
Subroutine __ANON__: called CC=1 SCAR=0.0
157 3 $line->('provider', $out->c(title => $manifest->provider_id));
158 3 $line->('issuer', $manifest->issuer);
159 3 ▶ $line->('fetched', $doc->{final_url}.( defined $doc->{address} ? ' ('.$doc->{address}.')' : '' ));
Branch: 1 branch
BranchTrueFalseTotal
defined $$doc{'address'} ? : 3 0 3
160 3 $line->('redirected', $_) for @{ $doc->{redirects} };
161  
162 3 $out->emit("\n", $out->c(title => 'endpoints'), "\n");
163 3 for my $endpoint (@{ $manifest->endpoints }) {
164 4 ▶ $out->emit(sprintf(" %-14s %-18s %s ", $endpoint->id, $endpoint->dialect, $endpoint->base_url),
Branch: 1 branch
BranchTrueFalseTotal
$endpoint->is_known_dialect ? : 3 1 4
Condition50%1/2
$endpoint->auth_ref // "none"
l!l
40
MC/DC0%0/1
$endpoint->auth_ref // "none"
$endpoint->auth_ref
Truth table50%1/2
A
A= $endpoint->auth_ref
Aresult
11
00
165 $out->c(meta => 'auth: '.( $endpoint->auth_ref // 'none' )),
166 $endpoint->is_known_dialect ? '' : $out->c(warn => ' (unknown dialect)'), "\n");
167 }
168 3 $out->emit($out->c(title => 'auth'), "\n");
169 3 ▶ $out->emit(' ', $out->c(meta => 'none declared'), "\n") unless @{ $manifest->auth };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$manifest->auth;} 0 3 3
170 3 for my $auth (@{ $manifest->auth }) {
171 4 ▶ $out->emit(sprintf(' %-14s %s', $auth->id, $auth->type),
Branch: 1 branch
BranchTrueFalseTotal
$auth->is_known_type ? : 3 1 4
172 $auth->is_known_type ? '' : $out->c(warn => ' (unknown type)'), "\n");
173 }
174 3 $out->emit($out->c(title => 'models'), "\n");
175 3 ▶ $out->emit(' ', $out->c(meta => 'none listed'), "\n") unless @{ $manifest->models };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$manifest->models;} 0 3 3
176 3 for my $model (@{ $manifest->models }) {
177 3 my $caps = $model->capabilities;
178 3 my @claimed = grep { $caps->{$_} } sort keys %$caps;
179 3 ▶ $out->emit(sprintf(' %-30s ', $model->id), $out->c(meta => 'on '.$model->endpoint_ref), ' ',
Branch: 1 branch
BranchTrueFalseTotal
@claimed ? : 3 0 3
180 @claimed ? join(', ', @claimed) : $out->c(meta => 'no capabilities claimed'), "\n");
181 }
182 3 ▶ $out->emit("\n") if @{ $doc->{warnings} } || @{ $doc->{notes} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$$doc{"warnings"};} or @{$$doc{"notes"};} 1 2 3
Condition66%2/3
@{$$doc{"warnings"};} or @{$$doc{"notes"};}
l!l&&r!l&&!r
102
MC/DC50%1/2
@{$$doc{"warnings"};} or @{$$doc{"notes"};}
@{$$doc{"warnings"};} @{$$doc{"notes"};}
Truth table66%2/3
A or B
A= @{$$doc{"warnings"};}B= @{$$doc{"notes"};}
ABresult
1X1
011
000
183 3 $out->emit($out->c(warn => 'warning: '.$_), "\n") for @{ $doc->{warnings} };
184 3 $out->emit($out->c(meta => 'note: '.$_), "\n") for @{ $doc->{notes} };
185 3 $out->emit("\n", $out->c(meta => 'What the provider claims; nothing was stored or bound.'), "\n");
186 3 return;
187 }
188  
189 14 ▶sub _err {
Subroutine _err: called CC=1 SCAR=0.0
190 14 my ( $self, @text ) = @_;
191 14 print { $self->err } @text;
192 14 return;
193 }
194  
195 55 __PACKAGE__->meta->make_immutable;
196  
197 55 1;
198  
199 __END__
200  
201 =pod
202  
203 =encoding UTF-8
204  
205 =head1 NAME
206  
207 Langertha::Raider::CLI::Provider - Internal provider commands of the raider CLI: inspect a manifest, activate it for --provider
208  
209 =head1 VERSION
210  
211 version 0.503
212  
213 =head1 SYNOPSIS
214  
215 # Internal to Langertha-Raider -- no API promise.
216 my $provider = Langertha::Raider::CLI::Provider->new(output => $out, err => \*STDERR);
217 my $exit = $provider->inspect('provider.example'); # human
218 my $exit = $provider->inspect('knarr.lan:8443', allow_internal => 1,
219 machine => $machine); # --json
220  
221 =head1 DESCRIPTION
222  
223 B<Internal module.> Its interface may change without notice.
224  
225 C<raider provider inspect>: fetches a provider manifest
226 (L<Langertha::Raider::Provider::Fetch>), validates it with
227 L<Langertha::Manifest> and shows what it declares -- provider id, issuer,
228 endpoints, auth mechanisms and models -- together with what this raider
229 cannot use (an unknown dialect, auth type or capability) and the inert
230 C<extensions>. Nothing is stored, no credential is bound and nothing is
231 trusted: a manifest states what a provider claims (ADR 0007).
232  
233 C<raider --provider>: L</activate> picks the endpoint of the manifest a
234 run uses (L<Langertha::Raider::Provider::Activation>) and reports why it
235 cannot, for L<Langertha::Raider::CLI::Main>.
236  
237 =head2 output
238  
239 The L<Langertha::Raider::CLI::Output> the result goes to. Required.
240  
241 =head2 err
242  
243 Filehandle for failures in human output. Defaults to C<STDERR>.
244  
245 =head2 fetch_args
246  
247 Extra constructor arguments for the fetcher (L</fetch_class>), for tests:
248 C<ssl_options>, C<resolver>, limits. Default none.
249  
250 =head2 fetcher
251  
252 my $fetch = $provider->fetcher(allow_internal => 1);
253  
254 A new fetcher with L</fetch_args> and the given arguments.
255  
256 =head2 inspect
257  
258 my $exit = $provider->inspect($target, allow_internal => $bool, machine => $machine);
259  
260 Inspects the provider C<$target> names (C<HOST>, C<HOST:PORT> or an
261 C<https> URL; L<Langertha::Raider::Provider::Fetch/target_url>). Returns
262 the exit status: C<0> for a valid manifest, C<1> when it was refused
263 (an address or redirect), could not be fetched or is not a valid manifest,
264 C<2> for a target that is none. With a machine, one document (see
265 L</document>); without, the report on L</output> and a failure on L</err>.
266  
267 =head2 activate
268  
269 my ( $exit, $activation ) = $provider->activate($target,
270 allow_internal => $bool, model => $model, has_api_key => $bool);
271  
272 C<raider --provider TARGET>: the endpoint of the provider's manifest the
273 run uses, through L<Langertha::Raider::Provider::Activation>. Returns
274 C<undef> and the completed activation, with its warnings written to
275 L</err>; or, after writing why to L</err>, just the exit status: C<2> when
276 the command line has to change (a target that is none, a model the
277 manifest does not list, no model where it lists several, no key where
278 the endpoint needs one), C<1> when the provider cannot be used as it is
279 (refused, not fetched, not valid, not usable by this raider).
280  
281 =head2 document
282  
283 my $doc = $provider->document($report, $manifest, $fetch);
284  
285 The result as data, the machine document without C<version>:
286  
287 =over
288  
289 =item C<status> -- C<completed> (a valid manifest), C<refused> (an
290 address or a redirect the fetch policy refuses) or C<failed> (network,
291 TLS, HTTP status, a limit, invalid JSON or schema).
292  
293 =item C<url> -- the manifest URL; C<final_url> -- where the body came
294 from after same-origin redirects (C<completed> only); C<redirects> -- those
295 redirects; C<address> -- the address connected to, once there was one.
296  
297 =item C<manifest> -- the validated manifest as core serializes it
298 (L<Langertha::Manifest/to_hash>), C<completed> only.
299  
300 =item C<warnings> -- what this raider could not use or found odd, one
301 sentence each (L</warnings>); C<notes> -- that C<extensions> are inert.
302 Both C<completed> only.
303  
304 =item C<error> -- why (C<refused>, C<failed>); C<location> -- the target
305 of a redirect that was not followed.
306  
307 =item C<elapsed> -- seconds the fetch took.
308  
309 =back
310  
311 =head2 warnings
312  
313 my @warnings = $provider->warnings($manifest, $origin, $fetch);
314  
315 What a client should know before using the manifest: endpoints whose
316 dialect this raider has no adapter for, auth types it cannot supply,
317 capability names Langertha does not know (treated as absent), an issuer
318 that is not the origin the manifest came from, and endpoints that are
319 plain C<http> or point at an internal address literal.
320  
321 =head2 notes
322  
323 The C<extensions> of the manifest, when it has any: inert, kept as
324 published, never interpreted, loaded or run.
325  
326 =head2 show
327  
328 $provider->show($doc, $manifest);
329  
330 The human report of a valid manifest.
331  
332 =head1 SEE ALSO
333  
334 =over
335  
336 =item * L<Langertha::Raider::Provider::Fetch>
337  
338 =item * L<Langertha::Raider::CLI::Main>
339  
340 =item * L<raider> -- C<PROVIDERS>
341  
342 =back
343  
344 =head1 SUPPORT
345  
346 =head2 Issues
347  
348 Please report bugs and feature requests on GitHub at
349 L<https://github.com/Getty/langertha-raider/issues>.
350  
351 =head2 IRC
352  
353 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
354  
355 =head1 CONTRIBUTING
356  
357 Contributions are welcome! Please fork the repository and submit a pull request.
358  
359 =head1 AUTHOR
360  
361 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
362  
363 =head1 COPYRIGHT AND LICENSE
364  
365 This software is copyright (c) 2026 by Torsten Raudssus.
366  
367 This is free software; you can redistribute it and/or modify it under
368 the same terms as the Perl 5 programming language system itself.
369  
370 =cut
« blib/lib/Langertha/Raider/Hall.pm ↑ Summary blib/lib/Langertha/Raider/CLI/REPL.pm »