| 1 | package App::teai::Config; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 41 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 41 | ▶ | use Cwd (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 41 | ▶ | use Errno qw(EISDIR); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 41 | ▶ | use Fcntl qw(:flock); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 41 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 41 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 41 | ▶ | use App::teai::Err; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 41 | ▶ | use App::teai::Wire qw(valid_id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 41 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 41 | my @AGENTS = qw(claude codex agy muse kimi grok shell); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 41 | my %KEYS = map { $_ => 1 } qw(url user password creds tls_ca); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 41 | my %ENVKEY = (url => 'TEAI_URL', user => 'TEAI_USER', password => 'TEAI_PASSWORD', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | creds => 'TEAI_CREDS', tls_ca => 'TEAI_TLS_CA'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 151 | ▶ | sub _env ($n) { my $x = $ENV{$n}; defined $x && length $x ? $x : undef } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_env: called CC=3 SCAR=11.0
Branch: 1 branch
Condition100%3/3
defined $x and length $x
MC/DC100%2/2
defined $x and length $x
defined $x
length $x
Truth table100%3/3
A and B
A= defined $xB= length $x
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 27 | ▶ | sub _config_path () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_config_path: called CC=7 SCAR=20.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 27 | ▶ | if (defined(my $p = _env('TEAI_CONFIG'))) { return ($p, 1) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 6 | my $x = _env('XDG_CONFIG_HOME'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | return ("$x/teai/config", 0) if defined $x && $x =~ m{\A/}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $x and $x =~ m[\A/]u
MC/DC100%2/2
defined $x and $x =~ m[\A/]u
defined $x
$x =~ m[\A/]u
Truth table100%3/3
A and B
A= defined $xB= $x =~ m[\A/]u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | ▶ | my $home = _env('HOME') // (getpwuid $<)[7]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
_env("HOME") // (getpwuid $<)[7]
MC/DC0%0/2
_env("HOME") // (getpwuid $<)[7]
_env("HOME")
(getpwuid $<)[7]
Truth table33%1/3
A // B
A= _env("HOME")B= (getpwuid $<)[7]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 5 | ▶ | return (undef, 0) unless defined $home && length $home; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $home and length $home
MC/DC0%0/2
defined $home and length $home
defined $home
length $home
Truth table33%1/3
A and B
A= defined $homeB= length $home
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 5 | return ("$home/.config/teai/config", 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 41 | my %warned; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 27 | ▶ | sub load () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=25 SCAR=32.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 27 | my ($file, $explicit) = _config_path(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 27 | my %v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 27 | ▶ | if (defined $file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 27 | my $ok = open(my $fh, '<', $file); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 27 | ▶ | if ($ok && -d $fh) { $ok = 0; $! = EISDIR } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ok and -d $fh
MC/DC100%2/2
$ok and -d $fh
$ok
-d $fh
Truth table100%3/3
A and B
A= $okB= -d $fh
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 27 | ▶ | if ($ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 19 | ▶ | if (-f $fh && ((stat _)[2] & 044) && !$warned{$file}++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
-f $fh and (stat _)[2] & 36 and not $warned{$file}++
-f $fh and (stat _)[2] & 36
MC/DC66%2/3
-f $fh and (stat _)[2] & 36 and not $warned{$file}++
-f $fh
(stat _)[2] & 36
not $warned{$file}++
Truth table75%3/4
A and B and not(C)
A= -f $fhB= (stat _)[2] & 36C= not $warned{$file}++
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | warn "teai: $file is readable by others\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 19 | ▶ | while (my $l = <$fh>) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 25 | $l =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 25 | ▶ | next if $l eq '' || $l =~ /^#/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$l eq "" or $l =~ /^#/u
MC/DC100%2/2
$l eq "" or $l =~ /^#/u
$l eq ""
$l =~ /^#/u
Truth table100%3/3
A or B
A= $l eq ""B= $l =~ /^#/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 22 | ▶ | my ($k, $val) = $l =~ /\A(\w+)\s*=\s*(.*)\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | or App::teai::Err::fail(2, "$file:$.: bad config line: $l"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 21 | ▶ | App::teai::Err::fail(2, "$file:$.: unknown config key '$k'") unless $KEYS{$k}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 20 | $v{$k} = $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | elsif ($explicit) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | App::teai::Err::fail(2, "cannot read TEAI_CONFIG $file: $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | ▶ | elsif (!$!{ENOENT} && !$!{ENOTDIR}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$!{'ENOENT'} || $!{'ENOTDIR'}
MC/DC50%1/2
$!{'ENOENT'} || $!{'ENOTDIR'}
$!{'ENOENT'}
$!{'ENOTDIR'}
Truth table66%2/3
A || B
A= $!{'ENOENT'}B= $!{'ENOTDIR'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | App::teai::Err::fail(2, "cannot read $file: $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 20 | for my $k (keys %ENVKEY) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 100 | my $e = _env($ENVKEY{$k}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 100 | ▶ | $v{$k} = $e if defined $e; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 20 | ▶ | my $url = $v{url} // 'nats://127.0.0.1:4222'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$v{"url"} // "nats://127.0.0.1:4222"
MC/DC100%1/1
$v{"url"} // "nats://127.0.0.1:4222"
$v{"url"}
Truth table100%2/2
A
A= $v{"url"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 20 | ▶ | my ($scheme, $host, $port) = $url =~ m{\A(nats|tls)://(\[[0-9A-Fa-f:.]+\]|[^/:\[\]]+)(?::(\d+))?/?\z} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | or App::teai::Err::fail(2, "invalid url '$url'"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 19 | ▶ | App::teai::Err::fail(2, "invalid url '$url': bad port") if defined $port && ($port < 1 || $port > 65535); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $port and $port < 1 || $port > 65535
$port < 1 or $port > 65535
MC/DC100%3/3
defined $port and $port < 1 || $port > 65535
defined $port
$port < 1
$port > 65535
Truth table100%4/4
A and B or C
A= defined $portB= $port < 1C= $port > 65535
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 16 | $host =~ s/\A\[|\]\z//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | url => $url, host => $host, port => $port // 4222, tls => $scheme eq 'tls' ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | user => $v{user}, pass => $v{password}, creds => $v{creds}, tls_ca => $v{tls_ca}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 16 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$port // 4222
MC/DC100%1/1
$port // 4222
$port
Truth table100%2/2
A
A= $port
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 13 | ▶ | sub project ($dir = Cwd::getcwd()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
project: called CC=15 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 13 | my $id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 13 | ▶ | my $d = Cwd::abs_path($dir) // $dir; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
Cwd::abs_path($dir) // $dir
MC/DC0%0/2
Cwd::abs_path($dir) // $dir
Cwd::abs_path($dir)
$dir
Truth table33%1/3
A // B
A= Cwd::abs_path($dir)B= $dir
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 13 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 48 | my $f = "$d/.teai/id"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 48 | ▶ | if (stat $f) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | ▶ | App::teai::Err::fail(2, "cannot read $f: " . (-d _ ? do { $! = EISDIR; "$!" } : 'not a regular file')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | unless -f _; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | open my $fh, '<', $f or App::teai::Err::fail(2, "cannot read $f: $!"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | $id = <$fh> // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
readline $fh // ""
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | $id =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 44 | ▶ | App::teai::Err::fail(2, "cannot read $f: $!") unless $!{ENOENT} || $!{ENOTDIR}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$!{"ENOENT"} or $!{"ENOTDIR"}
MC/DC100%2/2
$!{"ENOENT"} or $!{"ENOTDIR"}
$!{"ENOENT"}
$!{"ENOTDIR"}
Truth table100%3/3
A or B
A= $!{"ENOENT"}B= $!{"ENOTDIR"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 43 | my $up = File::Spec->catdir($d, File::Spec->updir); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 43 | $up = Cwd::abs_path($up); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 43 | ▶ | last if !defined $up || $up eq $d; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $up or $up eq $d
MC/DC50%1/2
not defined $up or $up eq $d
not defined $up
$up eq $d
Truth table66%2/3
not(A) or B
A= not defined $upB= $up eq $d
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 35 | $d = $up; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 10 | ▶ | $id //= _env('TEAI_PROJECT'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$id //= _env("TEAI_PROJECT")
MC/DC100%2/2
$id //= _env("TEAI_PROJECT")
$id
_env("TEAI_PROJECT")
Truth table100%3/3
A //= B
A= $idB= _env("TEAI_PROJECT")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 10 | ▶ | return undef unless defined $id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 5 | ▶ | App::teai::Err::fail(2, "invalid project id '$id'") unless valid_id($id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | return $id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 5 | ▶ | sub role () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
role: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 5 | my $r = _env('TEAI_ROLE'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 5 | ▶ | return undef unless defined $r; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | ▶ | App::teai::Err::fail(2, "invalid role '$r'") unless valid_id($r); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | return $r; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub host () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
host: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my $h = _env('TEAI_HOST') // (split /\./, (POSIX::uname())[1] // '', 2)[0] // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/7
(_env("TEAI_HOST") // (split(/\./u, (POSIX::uname())[1] // "", 2))[0]) // ""
_env("TEAI_HOST") // (split(/\./u, (POSIX::uname())[1] // "", 2))[0]
(POSIX::uname())[1] // ""
MC/DC0%0/2
(_env("TEAI_HOST") // (split(/\./u, (POSIX::uname())[1] // "", 2))[0]) // ""
_env("TEAI_HOST")
(split(/\./u, (POSIX::uname())[1] // "", 2))[0]
MC/DC0%0/1
(POSIX::uname())[1] // ""
(POSIX::uname())[1]
Truth table0%0/3
A // B
A= _env("TEAI_HOST")B= (split(/\./u, (POSIX::uname())[1] // "", 2))[0]
Truth table0%0/2
A
A= (POSIX::uname())[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $h =~ s/[^A-Za-z0-9_-]/_/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $h = substr $h, 0, 64; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | return length $h ? $h : 'unknown'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | sub agent_of ($session) { (split /-/, $session, 2)[0] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
agent_of: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | ▶ | sub session () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
session: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | my $s = $ENV{TEAI_SESSION}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | App::teai::Err::fail(2, "no session: run 'teai join' and export TEAI_SESSION") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $s and length $s
MC/DC50%1/2
defined $s and length $s
defined $s
length $s
Truth table66%2/3
A and B
A= defined $sB= length $s
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | unless defined $s && length $s; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | App::teai::Err::fail(2, "invalid session id '$s'") unless valid_id($s); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | return $s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 10 | ▶ | sub valid_agent ($agent) { defined $agent && grep({ $_ eq $agent } @AGENTS) ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
valid_agent: called CC=3 SCAR=11.1
Pod
documented
Branch: 1 branch
Condition66%2/3
defined $agent and grep {$_ eq $agent;} @AGENTS
MC/DC50%1/2
defined $agent and grep {$_ eq $agent;} @AGENTS
defined $agent
grep {$_ eq $agent;} @AGENTS
Truth table66%2/3
A and B
A= defined $agentB= grep {$_ eq $agent;} @AGENTS
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | ▶ | sub new_session_id ($agent) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_session_id: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | ▶ | App::teai::Err::fail(2, "unknown agent '" . ($agent // '') . "'") unless valid_agent($agent); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$agent // ""
MC/DC0%0/1
$agent // ""
$agent
Truth table50%1/2
A
A= $agent
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | my @c = ('a' .. 'z', 0 .. 9); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | return "$agent-" . join '', map { $c[int rand @c] } 1 .. 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 8 | ▶ | sub _safe_dir ($d, $what = 'runtime') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_safe_dir: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 8 | my @st = lstat $d; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 8 | ▶ | App::teai::Err::fail(3, "unsafe $what dir $d") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
@st and -d _ and not -l _ and $st[4] == $<
@st and -d _ and not -l _
@st and -d _
MC/DC50%2/4
@st and -d _ and not -l _ and $st[4] == $<
@st
-d _
not -l _
$st[4] == $<
Truth table60%3/5
A and B and not(C) and D
A= @stB= -d _C= not -l _D= $st[4] == $<
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | unless @st && -d _ && !-l _ && $st[4] == $<; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 5 | ▶ | if (($st[2] & 07777) != 0700) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | chmod 0700, $d or App::teai::Err::fail(3, "unsafe $what dir $d"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 5 | return $d; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 7 | ▶ | sub runtime_path () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
runtime_path: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | return defined $ENV{XDG_RUNTIME_DIR} && length $ENV{XDG_RUNTIME_DIR} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 7 | ▶ | ? "$ENV{XDG_RUNTIME_DIR}/teai" : "/tmp/teai-$<"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ENV{"XDG_RUNTIME_DIR"} and length $ENV{"XDG_RUNTIME_DIR"}
MC/DC0%0/2
defined $ENV{"XDG_RUNTIME_DIR"} and length $ENV{"XDG_RUNTIME_DIR"}
defined $ENV{"XDG_RUNTIME_DIR"}
length $ENV{"XDG_RUNTIME_DIR"}
Truth table33%1/3
A and B
A= defined $ENV{"XDG_RUNTIME_DIR"}B= length $ENV{"XDG_RUNTIME_DIR"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 7 | ▶ | sub _private_dir ($d, $what) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_private_dir: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 7 | ▶ | unless (lstat $d) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | require File::Path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | ▶ | eval { File::Path::make_path($d, { mode => 0700 }); 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | or App::teai::Err::fail(3, "cannot create $what dir $d"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 7 | return _safe_dir($d, $what); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 7 | ▶ | sub runtime_dir () { _private_dir(runtime_path(), 'runtime') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
runtime_dir: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub state_dir () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
state_dir: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my $x = _env('XDG_STATE_HOME'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my $home = _env('HOME') // (getpwuid $<)[7]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
_env("HOME") // (getpwuid $<)[7]
MC/DC0%0/2
_env("HOME") // (getpwuid $<)[7]
_env("HOME")
(getpwuid $<)[7]
Truth table0%0/3
A // B
A= _env("HOME")B= (getpwuid $<)[7]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | my $d = defined $x && $x =~ m{\A/} ? "$x/teai" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
defined $x and $x =~ m[\A/]u
defined $home and length $home
MC/DC0%0/2
defined $x and $x =~ m[\A/]u
defined $x
$x =~ m[\A/]u
MC/DC0%0/2
defined $home and length $home
defined $home
length $home
Truth table0%0/3
A and B
A= defined $xB= $x =~ m[\A/]u
Truth table0%0/3
A and B
A= defined $homeB= length $home
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | : defined $home && length $home ? "$home/.local/state/teai" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | : App::teai::Err::fail(3, 'cannot find the state dir: no HOME'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | return _private_dir($d, 'state'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 3 | ▶ | sub reader_lock ($session, $nowait = 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reader_lock: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | ▶ | App::teai::Err::fail(2, "invalid session id '$session'") unless valid_id($session); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | my $path = runtime_dir() . "/$session.reader"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | ▶ | open my $fh, '>>', $path or App::teai::Err::fail(3, "cannot open $path: $!"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 3 | ▶ | my $op = $nowait ? LOCK_EX | LOCK_NB : LOCK_EX; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 3 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 4 | ▶ | return $fh if flock $fh, $op; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 2 | ▶ | next if $!{EINTR}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 41 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||