blib/lib/Langertha/Knarr/CLI/Cmd/Models.pm

filter: Statementstmt 89.2% 50 / 56 Branchbran 61.1% 11 / 18 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 80.7% 67 / 83 CC 14 SCAR 21.6
CC
14
Cov
82%
execute
12.3
_max_len
4.2
BEGIN
1.0
CRAP 8.6
« blib/lib/Langertha/Knarr/Config.pm ↑ Summary blib/lib/Langertha/Knarr/Handler/Engine.pm »
1 package Langertha::Knarr::CLI::Cmd::Models;
2 39 our $VERSION = '1.102';
3 # ABSTRACT: List configured models and their backends
4 39 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 with 'Langertha::Knarr::CLI::Role::GlobalOptions';
6 39 ▶use MooX::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use MooX::Options protect_argv => 0, usage_string => 'USAGE: knarr models [options]';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 39 option format => (
11 is => 'ro',
12 format => 's',
13 short => 'f',
14 doc => 'Output format: table, json (default: table)',
15 default => 'table',
16 );
17  
18 3 ▶sub execute {
Subroutine execute: called CC=11 SCAR=25.1
Pod undocumented
19 3 my ($self, $args, $chain) = @_;
20 3 my $config_file = $self->config_file($chain);
21  
22 3 ▶ unless (-f $config_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $config_file) 0 3 3
23 0 print STDERR "Config file not found: $config_file\n";
24 0 exit 1;
25 }
26  
27 3 require Langertha::Knarr::Config;
28 3 require Langertha::Knarr::Router;
29  
30 3 my $config = Langertha::Knarr::Config->new(file => $config_file);
31 # Like check and start (k67): stdout is the model list, errors go to stderr
32 3 my @errors = $config->validate;
33 3 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 1 2 3
34 1 print STDERR "Configuration INVALID:\n";
35 1 print STDERR " - $_\n" for @errors;
36 1 exit 1;
37 }
38 2 my $router = Langertha::Knarr::Router->new(config => $config);
39 2 my $models = $router->list_models;
40  
41 2 ▶ if ($self->format eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format eq "json") 1 1 2
42 1 require JSON::MaybeXS;
43 1 print JSON::MaybeXS->new(pretty => 1, utf8 => 1, convert_blessed => 1)->encode($models);
44 1 return;
45 }
46  
47 # Table format
48 1 ▶ unless (@$models) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$models) 0 1 1
49 0 print "No models configured.\n";
50 0 print "Run 'knarr init' to scan your environment for API keys.\n";
51 0 return;
52 }
53  
54 1 my $max_id = _max_len(map { $_->{id} } @$models);
55 1 my $max_engine = _max_len(map { $_->{engine} } @$models);
56 1 my $max_model = _max_len(map { $_->{model} } @$models);
57  
58 1 ▶ $max_id = 5 if $max_id < 5;
Branch: 1 branch
BranchTrueFalseTotal
if $max_id < 5 0 1 1
59 1 ▶ $max_engine = 6 if $max_engine < 6;
Branch: 1 branch
BranchTrueFalseTotal
if $max_engine < 6 0 1 1
60 1 ▶ $max_model = 5 if $max_model < 5;
Branch: 1 branch
BranchTrueFalseTotal
if $max_model < 5 0 1 1
61  
62 1 printf "%-${max_id}s %-${max_engine}s %-${max_model}s %s\n",
63 'ID', 'ENGINE', 'MODEL', 'SOURCE';
64 1 printf "%-${max_id}s %-${max_engine}s %-${max_model}s %s\n",
65 '-' x $max_id, '-' x $max_engine, '-' x $max_model, '------';
66  
67 1 for my $m (@$models) {
68 printf "%-${max_id}s %-${max_engine}s %-${max_model}s %s\n",
69 1 $m->{id}, $m->{engine}, $m->{model}, $m->{source};
70 }
71  
72 1 ▶ if (my $default = $config->default_engine) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $default = $config->default_engine) 0 1 1
73 0 print "\nDefault engine: $default->{engine}\n";
74 }
75 }
76  
77 3 ▶sub _max_len {
Subroutine _max_len: called CC=4 SCAR=14.3
78 3 my $max = 0;
79 3 for (@_) {
80 3 ▶ my $l = length($_ // '');
Condition50%1/2
$_ // ""
l!l
30
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
81 3 ▶ $max = $l if $l > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $l > $max 3 0 3
82 }
83 3 return $max;
84 }
85  
86 39 1;
87  
88 __END__
89  
90 =pod
91  
92 =encoding UTF-8
93  
94 =head1 NAME
95  
96 Langertha::Knarr::CLI::Cmd::Models - List configured models and their backends
97  
98 =head1 VERSION
99  
100 version 1.102
101  
102 =head1 DESCRIPTION
103  
104 Implements the C<knarr models> command. Loads and validates the config
105 file (an invalid one exits C<1> with the errors of C<knarr check>, on
106 stderr), triggers auto-discovery (if enabled), and prints the full model
107 list as a table or JSON. Each row shows the model ID, engine class, backend model name, and
108 whether it was explicitly configured or auto-discovered.
109  
110 See L<knarr> for option details and L<Langertha::Knarr::Config> for the
111 configuration file format.
112  
113 =head1 SEE ALSO
114  
115 =over
116  
117 =item * L<knarr> — CLI synopsis and option reference
118  
119 =item * L<Langertha::Knarr::Router/list_models> — Underlying data source
120  
121 =back
122  
123 =head1 SUPPORT
124  
125 =head2 Issues
126  
127 Please report bugs and feature requests on GitHub at
128 L<https://github.com/Getty/langertha-knarr/issues>.
129  
130 =head2 IRC
131  
132 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
133  
134 =head1 CONTRIBUTING
135  
136 Contributions are welcome! Please fork the repository and submit a pull request.
137  
138 =head1 AUTHOR
139  
140 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
141  
142 =head1 COPYRIGHT AND LICENSE
143  
144 This software is copyright (c) 2026 by Torsten Raudssus.
145  
146 This is free software; you can redistribute it and/or modify it under
147 the same terms as the Perl 5 programming language system itself.
148  
149 =cut
« blib/lib/Langertha/Knarr/Config.pm ↑ Summary blib/lib/Langertha/Knarr/Handler/Engine.pm »