| 1 | package Langertha::Knarr::CLI::Cmd::Models; | ||||||||||||
| 2 | 39 | our $VERSION = '1.103'; | |||||||||||
| 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=13 SCAR=29.6
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
| |||||||||||||
| 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
| |||||||||||||
| 34 | 1 | print STDERR "Configuration INVALID:\n"; | |||||||||||
| 35 | 1 | print STDERR " - $_\n" for @errors; | |||||||||||
| 36 | 1 | exit 1; | |||||||||||
| 37 | } | ||||||||||||
| 38 | 2 | ▶ | if ( $config->token_auth ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | 0 | ▶ | if ( $self->format eq 'json' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 0 | print "[]\n"; | |||||||||||
| 41 | 0 | return; | |||||||||||
| 42 | } | ||||||||||||
| 43 | 0 | print "Models come from the issued tokens (token_auth): there is no static model list.\n"; | |||||||||||
| 44 | 0 | print "Routes by the token's provider: ", join( ', ', sort keys %{ $config->routes } ), "\n"; | |||||||||||
| 45 | 0 | return; | |||||||||||
| 46 | } | ||||||||||||
| 47 | 2 | my $router = Langertha::Knarr::Router->new(config => $config); | |||||||||||
| 48 | 2 | my $models = $router->list_models; | |||||||||||
| 49 | |||||||||||||
| 50 | 2 | ▶ | if ($self->format eq 'json') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 1 | require JSON::MaybeXS; | |||||||||||
| 52 | 1 | print JSON::MaybeXS->new(pretty => 1, utf8 => 1, convert_blessed => 1)->encode($models); | |||||||||||
| 53 | 1 | return; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | # Table format | ||||||||||||
| 57 | 1 | ▶ | unless (@$models) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | print "No models configured.\n"; | |||||||||||
| 59 | 0 | print "Run 'knarr init' to scan your environment for API keys.\n"; | |||||||||||
| 60 | 0 | return; | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | 1 | my $max_id = _max_len(map { $_->{id} } @$models); | |||||||||||
| 64 | 1 | my $max_engine = _max_len(map { $_->{engine} } @$models); | |||||||||||
| 65 | 1 | my $max_model = _max_len(map { $_->{model} } @$models); | |||||||||||
| 66 | |||||||||||||
| 67 | 1 | ▶ | $max_id = 5 if $max_id < 5; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 1 | ▶ | $max_engine = 6 if $max_engine < 6; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | 1 | ▶ | $max_model = 5 if $max_model < 5; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | |||||||||||||
| 71 | 1 | printf "%-${max_id}s %-${max_engine}s %-${max_model}s %s\n", | |||||||||||
| 72 | 'ID', 'ENGINE', 'MODEL', 'SOURCE'; | ||||||||||||
| 73 | 1 | printf "%-${max_id}s %-${max_engine}s %-${max_model}s %s\n", | |||||||||||
| 74 | '-' x $max_id, '-' x $max_engine, '-' x $max_model, '------'; | ||||||||||||
| 75 | |||||||||||||
| 76 | 1 | for my $m (@$models) { | |||||||||||
| 77 | printf "%-${max_id}s %-${max_engine}s %-${max_model}s %s\n", | ||||||||||||
| 78 | 1 | $m->{id}, $m->{engine}, $m->{model}, $m->{source}; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 1 | ▶ | if (my $default = $config->default_engine) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 0 | print "\nDefault engine: $default->{engine}\n"; | |||||||||||
| 83 | } | ||||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 3 | ▶ | sub _max_len { | ||||||||||
|
Subroutine
_max_len: called CC=4 SCAR=14.3
| |||||||||||||
| 87 | 3 | my $max = 0; | |||||||||||
| 88 | 3 | for (@_) { | |||||||||||
| 89 | 3 | ▶ | my $l = length($_ // ''); | ||||||||||
|
Condition50%1/2
$_ // ""
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
| |||||||||||||
| 90 | 3 | ▶ | $max = $l if $l > $max; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | } | ||||||||||||
| 92 | 3 | return $max; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | 39 | 1; | |||||||||||
| 96 | |||||||||||||
| 97 | __END__ | ||||||||||||