| 1 | package Langertha::Knarr::CLI::Cmd::Init; | ||||||||||
| 2 | 38 | our $VERSION = '1.103'; | |||||||||
| 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__ | ||||||||||