Tak/Script.pm

filter: Statementstmt 30.8% 37 / 120 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 31.0% 9 / 29 Podpod 0.0% 0 / 4 totaltotal 24.8% 46 / 185 SCAR 46.9
CC
15
Cov
25%
run
42.0
_build_config
12.0
setup_logger
12.0
CRAP 109.3
« Tak/Role/ScriptActions.pm ↑ Summary Tak/ConnectorService.pm »
1 package Tak::Script;
2  
3 1 ▶use Getopt::Long qw(GetOptionsFromArray :config posix_defaults bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Config::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Tak::Client::Router;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Tak::Client::RemoteRouter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Tak::Router;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Log::Contextual qw(:log);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Log::Contextual::SimpleLogger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 with 'Tak::Role::ScriptActions';
14  
15 1 has options => (is => 'ro', required => 1);
16 1 has env => (is => 'ro', required => 1);
17  
18 1 has log_level => (is => 'rw');
19  
20 1 has stdin => (is => 'lazy');
21 1 has stdout => (is => 'lazy');
22 1 has stderr => (is => 'lazy');
23  
24 0 ▶sub _build_stdin { shift->env->{stdin} }
Subroutine _build_stdin: not called CC=1 SCAR=6.9
25 0 ▶sub _build_stdout { shift->env->{stdout} }
Subroutine _build_stdout: not called CC=1 SCAR=6.9
26 0 ▶sub _build_stderr { shift->env->{stderr} }
Subroutine _build_stderr: not called CC=1 SCAR=6.9
27  
28 1 has config => (is => 'lazy');
29  
30 sub _build_config {
31 0 ▶ my ($self) = @_;
Subroutine _build_config: not called CC=3 SCAR=24.8
32 0 ▶ my $file = $self->options->{config} || '.tak/default.conf';
Condition0%0/2
$self->options->{'config'} || '.tak/default.conf'
l!l
00
MC/DC0%0/1
$self->options->{'config'} || '.tak/default.conf'
$self->options->{'config'}
Truth table0%0/2
A
A= $self->options->{'config'}
Aresult
11
00
33 0 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 0 0 0
34 0 Config::Settings->new->parse_file($file);
35 } else {
36 0 {};
37 }
38 }
39  
40 1 has local_client => (is => 'lazy');
41  
42 sub _build_local_client {
43 0 ▶ my ($self) = @_;
Subroutine _build_local_client: not called CC=1 SCAR=6.9
44 0 Tak::Client::Router->new(service => Tak::Router->new);
45 }
46  
47 sub BUILD {
48 0 ▶ shift->setup_logger;
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
49 }
50  
51 sub setup_logger {
52 0 ▶ my ($self) = @_;
Subroutine setup_logger: not called CC=3 SCAR=24.8
Pod undocumented
53 0 my @level_names = qw(fatal error warn info debug trace);
54 0 my $options = $self->options;
55 0 ▶ my $level = 2 + ($options->{verbose}||0) - ($options->{quiet}||0);
Condition0%0/4
$options->{'verbose'} || 0
l!l
00
$options->{'quiet'} || 0
l!l
00
MC/DC0%0/1
$options->{'verbose'} || 0
$options->{'verbose'}
MC/DC0%0/1
$options->{'quiet'} || 0
$options->{'quiet'}
Truth table0%0/2
A
A= $options->{'verbose'}
Aresult
11
00
Truth table0%0/2
A
A= $options->{'quiet'}
Aresult
11
00
56 0 my $upto = $level_names[$level];
57 0 $self->log_level($upto);
58 Log::Contextual::set_logger(
59 Log::Contextual::SimpleLogger->new({
60 levels_upto => $upto,
61 0 ▶ coderef => sub { print STDERR '<local> ', @_ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 })
63 0 );
64 }
65  
66 sub _parse_options {
67 0 ▶ my ($self, $string, $argv) = @_;
Subroutine _parse_options: not called CC=1 SCAR=6.9
68 0 my @spec = split ';', $string;
69 0 my %opt;
70 0 GetOptionsFromArray($argv, \%opt, @spec);
71 0 return \%opt;
72 }
73  
74 sub run {
75 0 ▶ my ($self) = @_;
Subroutine run: not called CC=6 SCAR=37.4
Pod undocumented
76 0 my @argv = @{$self->env->{argv}};
77 0 ▶ unless (@argv && $argv[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@argv and $argv[0]) 0 0 0
Condition0%0/3
@argv and $argv[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@argv and $argv[0]
@argv $argv[0]
Truth table0%0/3
A and B
A= @argvB= $argv[0]
ABresult
0X0
100
111
78 0 return $self->local_help;
79 }
80 0 my $cmd = shift(@argv);
81 0 $cmd =~ s/-/_/g;
82 0 ▶ if (my $code = $self->can("local_$cmd")) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $code = $self->can("local_$cmd")) { } 0 0 0
elsif ($code = $self->can("each_$cmd")) { } 0 0 0
elsif ($code = $self->can("every_$cmd")) { } 0 0 0
83 0 return $self->_run($cmd, $code, @argv);
84 } elsif ($code = $self->can("each_$cmd")) {
85 0 return $self->_run_each($cmd, $code, @argv);
86 } elsif ($code = $self->can("every_$cmd")) {
87 0 return $self->_run_every($cmd, $code, @argv);
88 }
89 0 $self->stderr->print("No such command: ${cmd}\n");
90 0 return $self->local_help;
91 }
92  
93 sub _load_file {
94 0 ▶ my ($self, $file) = @_;
Subroutine _load_file: not called CC=1 SCAR=6.9
95 0 $self->_load_file_in_my_script($file);
96 }
97  
98 sub local_help {
99 0 ▶ my ($self) = @_;
Subroutine local_help: not called CC=1 SCAR=6.9
Pod undocumented
100 0 $self->stderr->print("Help unimplemented\n");
101 }
102  
103 sub _maybe_parse_options {
104 0 ▶ my ($self, $code, $argv) = @_;
Subroutine _maybe_parse_options: not called CC=2 SCAR=17.9
105 0 ▶ if (my $proto = prototype($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proto = prototype $code) { } 0 0 0
106 0 $self->_parse_options($proto, $argv);
107 } else {
108 0 {};
109 }
110 }
111  
112 sub _run_local {
113 0 ▶ my ($self, $cmd, $code, @argv) = @_;
Subroutine _run_local: not called CC=1 SCAR=6.9
114 0 my $opt = $self->_maybe_parse_options($code, \@argv);
115 0 $self->$code($opt, @argv);
116 }
117  
118 sub _run_each {
119 0 ▶ my ($self, $cmd, $code, @argv) = @_;
Subroutine _run_each: not called CC=3 SCAR=24.8
120 0 my @targets = $self->_host_list_for($cmd);
121 0 ▶ unless (@targets) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@targets) 0 0 0
122 0 $self->stderr->print("No targets for ${cmd}\n");
123 0 return;
124 }
125 0 my $opt = $self->_maybe_parse_options($code, \@argv);
126 0 $self->local_client->ensure(connector => 'Tak::ConnectorService');
127 0 foreach my $target (@targets) {
128 0 my $remote = $self->_connection_to($target);
129 0 $self->$code($remote, $opt, @argv);
130 }
131 }
132  
133 sub _run_every {
134 0 ▶ my ($self, $cmd, $code, @argv) = @_;
Subroutine _run_every: not called CC=2 SCAR=17.9
135 0 my @targets = $self->_host_list_for($cmd);
136 0 ▶ unless (@targets) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@targets) 0 0 0
137 0 $self->stderr->print("No targets for ${cmd}\n");
138 0 return;
139 }
140 0 my $opt = $self->_maybe_parse_options($code, \@argv);
141 0 $self->local_client->ensure(connector => 'Tak::ConnectorService');
142 0 my @remotes = map $self->_connection_to($_), @targets;
143 0 $self->$code(\@remotes, $opt, @argv);
144 }
145  
146 sub _host_list_for {
147 0 ▶ my ($self, $command) = @_;
Subroutine _host_list_for: not called CC=2 SCAR=17.9
148 0 my @host_spec = map split(' ', $_), @{$self->options->{host}};
149 0 ▶ unshift(@host_spec, '-') if $self->options->{local};
Branch: 1 branch
BranchTrueFalseTotal
if $self->options->{"local"} 0 0 0
150 0 return @host_spec;
151 }
152  
153 sub _connection_to {
154 0 ▶ my ($self, $target) = @_;
Subroutine _connection_to: not called CC=1 SCAR=6.9
155 0 ▶ log_debug { "Connecting to ${target}" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 0 my @path = $self->local_client->do(
157 connector => create => $target, log_level => $self->log_level
158 );
159 0 my ($local, $remote) =
160 map $self->local_client->curry(connector => connection => @path => $_),
161 qw(local remote);
162 0 $local->ensure(module_sender => 'Tak::ModuleSender');
163 0 $remote->ensure(
164 module_loader => 'Tak::ModuleLoader',
165 expose => { module_sender => [ 'remote', 'module_sender' ] }
166 );
167 0 $remote->do(module_loader => 'enable');
168 0 ▶ log_debug { "Setup connection to ${target}" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 0 Tak::Client::RemoteRouter->new(
170 %$remote, host => $target
171 );
172 }
173  
174 1 1;
« Tak/Role/ScriptActions.pm ↑ Summary Tak/ConnectorService.pm »