| 1 | package Langertha::Knarr::CLI::Cmd::Init; | ||||||||||
| 2 | 38 | our $VERSION = '1.102'; | |||||||||
| 3 | # ABSTRACT: Scan environment and generate Knarr configuration | ||||||||||
| 4 | 38 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 38 | ▶ | use MooX::Cmd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 38 | ▶ | use MooX::Options protect_argv => 0, usage_string => 'USAGE: knarr init [options]'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | option env_file => ( | ||||||||||
| 10 | is => 'ro', | ||||||||||
| 11 | format => 's@', | ||||||||||
| 12 | short => 'e', | ||||||||||
| 13 | doc => 'Additional .env file(s) to scan (repeatable)', | ||||||||||
| 14 | 1 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 38 | ); | |||||||||
| 16 | |||||||||||
| 17 | option listen => ( | ||||||||||
| 18 | is => 'ro', | ||||||||||
| 19 | format => 's@', | ||||||||||
| 20 | short => 'l', | ||||||||||
| 21 | doc => 'Listen address(es), repeatable (default: 127.0.0.1:8080 + 127.0.0.1:11434)', | ||||||||||
| 22 | 2 | ▶ | default => sub { ['127.0.0.1:8080', '127.0.0.1:11434'] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 38 | ); | |||||||||
| 24 | |||||||||||
| 25 | 38 | option output => ( | |||||||||
| 26 | is => 'ro', | ||||||||||
| 27 | format => 's', | ||||||||||
| 28 | short => 'o', | ||||||||||
| 29 | doc => 'Output file (default: stdout)', | ||||||||||
| 30 | ); | ||||||||||
| 31 | |||||||||||
| 32 | 1 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: called CC=7 SCAR=24.3
Pod
undocumented
| |||||||||||
| 33 | 1 | my ($self, $args, $chain) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 1 | require Langertha::Knarr::Config; | |||||||||
| 36 | |||||||||||
| 37 | # Scan default .env locations too | ||||||||||
| 38 | 1 | my @env_files = @{$self->env_file}; | |||||||||
| 39 | 1 | for my $default_file ('.env', '.env.local', "$ENV{HOME}/.env") { | |||||||||
| 40 | 3 | ▶ | push @env_files, $default_file if -f $default_file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 1 | my $found = Langertha::Knarr::Config->scan_env(env_files => \@env_files); | |||||||||
| 44 | |||||||||||
| 45 | 1 | my $yaml = Langertha::Knarr::Config->generate_config( | |||||||||
| 46 | engines => $found, | ||||||||||
| 47 | listen => $self->listen, | ||||||||||
| 48 | ); | ||||||||||
| 49 | |||||||||||
| 50 | 1 | ▶ | if (my $out = $self->output) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | ▶ | open my $fh, '>', $out or die "Cannot write $out: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | print $fh $yaml; | |||||||||
| 53 | 0 | close $fh; | |||||||||
| 54 | 0 | my $count = scalar keys %$found; | |||||||||
| 55 | 0 | print STDERR "Generated $out with $count engine(s) found\n"; | |||||||||
| 56 | 0 | ▶ | print STDERR "Found: ", join(', ', sort keys %$found), "\n" if $count; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | } else { | ||||||||||
| 58 | 1 | print $yaml; | |||||||||
| 59 | 1 | my $count = scalar keys %$found; | |||||||||
| 60 | 1 | print STDERR "# Found $count engine(s) from environment\n"; | |||||||||
| 61 | 1 | ▶ | print STDERR "# Engines: ", join(', ', sort keys %$found), "\n" if $count; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 38 | 1; | |||||||||
| 66 | |||||||||||
| 67 | __END__ | ||||||||||
| 68 | |||||||||||
| 69 | =pod | ||||||||||
| 70 | |||||||||||
| 71 | =encoding UTF-8 | ||||||||||
| 72 | |||||||||||
| 73 | =head1 NAME | ||||||||||
| 74 | |||||||||||
| 75 | Langertha::Knarr::CLI::Cmd::Init - Scan environment and generate Knarr configuration | ||||||||||
| 76 | |||||||||||
| 77 | =head1 VERSION | ||||||||||
| 78 | |||||||||||
| 79 | version 1.102 | ||||||||||
| 80 | |||||||||||
| 81 | =head1 DESCRIPTION | ||||||||||
| 82 | |||||||||||
| 83 | Implements the C<knarr init> command. Scans C<%ENV> and any C<.env> files | ||||||||||
| 84 | for known API key variables (C<.env> and C<.env.local> in the current | ||||||||||
| 85 | directory and C<$HOME/.env> are always scanned when they exist), then | ||||||||||
| 86 | generates a complete YAML configuration and writes it to stdout or a file. | ||||||||||
| 87 | |||||||||||
| 88 | knarr init > knarr.yaml | ||||||||||
| 89 | knarr init -e .env.production -o production.yaml | ||||||||||
| 90 | knarr init -l 0.0.0.0:8080 -l 0.0.0.0:11434 > knarr.yaml # for a container | ||||||||||
| 91 | |||||||||||
| 92 | The generated config listens on C<127.0.0.1:8080> and C<127.0.0.1:11434> | ||||||||||
| 93 | unless C<-l> says otherwise, enables C<auto_discover>, sets OpenAI as the | ||||||||||
| 94 | default engine when an OpenAI key was found, names the variable each key | ||||||||||
| 95 | was found in as C<api_key_env> (the default engine included), and has no | ||||||||||
| 96 | C<passthrough:> section. | ||||||||||
| 97 | |||||||||||
| 98 | See L<knarr> for full option details, L<Langertha::Knarr::Config/scan_env> | ||||||||||
| 99 | for the env scanning logic, and L<Langertha::Knarr::Config> for the config | ||||||||||
| 100 | format. | ||||||||||
| 101 | |||||||||||
| 102 | =head1 SEE ALSO | ||||||||||
| 103 | |||||||||||
| 104 | =over | ||||||||||
| 105 | |||||||||||
| 106 | =item * L<knarr> — CLI synopsis and option reference | ||||||||||
| 107 | |||||||||||
| 108 | =item * L<Langertha::Knarr::Config/scan_env> — API key detection | ||||||||||
| 109 | |||||||||||
| 110 | =item * L<Langertha::Knarr::Config/generate_config> — YAML generation | ||||||||||
| 111 | |||||||||||
| 112 | =back | ||||||||||
| 113 | |||||||||||
| 114 | =head1 SUPPORT | ||||||||||
| 115 | |||||||||||
| 116 | =head2 Issues | ||||||||||
| 117 | |||||||||||
| 118 | Please report bugs and feature requests on GitHub at | ||||||||||
| 119 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||
| 120 | |||||||||||
| 121 | =head2 IRC | ||||||||||
| 122 | |||||||||||
| 123 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||
| 124 | |||||||||||
| 125 | =head1 CONTRIBUTING | ||||||||||
| 126 | |||||||||||
| 127 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||
| 128 | |||||||||||
| 129 | =head1 AUTHOR | ||||||||||
| 130 | |||||||||||
| 131 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||
| 132 | |||||||||||
| 133 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 134 | |||||||||||
| 135 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||
| 136 | |||||||||||
| 137 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 138 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 139 | |||||||||||
| 140 | =cut | ||||||||||