enhance repl command
[scpubgit/Tak.git] / lib / Tak / Role / ScriptActions.pm
CommitLineData
f2d82fb2 1package Tak::Role::ScriptActions;
2
3use Moo::Role;
4no warnings::illegalproto;
5
6sub every_exec (stream|s) {
7 my ($self, $remotes, $options, @command) = @_;
8
9 my @requests;
10
11 $_->ensure(command_service => 'Tak::CommandService') for @$remotes;
12
13 foreach my $remote (@$remotes) {
14 if ($options->{stream}) {
15 my $stdout = $self->stdout;
16 my $host = $remote->host;
17 push @requests, $remote->start(
18 {
19 on_result => sub { $self->print_exec_result($remote, @_) },
20 on_progress => sub {
21 $stdout->print($host.' '.$_[0].': '.$_[1]);
22 $stdout->print("\n") unless $_[1] =~ /\n\Z/;
23 }
24 },
25 command_service => stream_exec => \@command
26 );
27 } else {
28 push @requests, $remote->start(
29 { on_result => sub { $self->print_exec_result($remote, @_) } },
30 command_service => exec => \@command
31 );
32 }
33 }
34 Tak->await_all(@requests);
35}
36
37sub print_exec_result {
38 my ($self, $remote, $result) = @_;
39
40 my $res = eval { $result->get }
41 or do {
42 $self->stderr->print("Host ${\$remote->host}: Error: $@\n");
43 return;
44 };
45
46 my $code = $res->{exit_code};
47 $self->stdout->print(
48 "Host ${\$remote->host}: ".($code ? "NOT OK ${code}" : "OK")."\n"
49 );
50 if ($res->{stderr}) {
51 $self->stdout->print("Stderr:\n${\$res->{stderr}}\n");
52 }
53 if ($res->{stdout}) {
54 $self->stdout->print("Stdout:\n${\$res->{stdout}}\n");
55 }
56}
57
5c11b21f 58sub each_repl (I=s@;m=s@;M=s@) {
59 my ($self, $remote, $options) = @_;
f2d82fb2 60 require Tak::REPL;
5c11b21f 61 require B;
f2d82fb2 62 $remote->ensure(eval_service => 'Tak::EvalService');
5c11b21f 63 foreach my $lib (@{$options->{'I'}||[]}) {
64 $remote->do(eval_service => eval => "lib->import(${\B::perlstring($lib)})");
65 }
66 foreach my $module (@{$options->{'m'}||[]}) {
67 $remote->do(eval_service => eval => "use ${module} ()");
68 }
69 foreach my $spec (@{$options->{'M'}||[]}) {
70 my ($module, $import) = split('=', $spec);
71 my $extra = '';
72 if ($import) {
73 $extra = ' '.join(', ', map B::perlstring($_), split(',',$import));
74 }
75 $remote->do(eval_service => eval => "use ${module}${extra}");
76 }
f2d82fb2 77 Tak::REPL->new(client => $remote->curry('eval_service'))->run;
78}
79
801;