1 package Tak::Role::ScriptActions;
4 no warnings::illegalproto;
6 sub every_exec (stream|s) {
7 my ($self, $remotes, $options, @command) = @_;
11 $_->ensure(command_service => 'Tak::CommandService') for @$remotes;
13 foreach my $remote (@$remotes) {
14 if ($options->{stream}) {
15 my $stdout = $self->stdout;
16 my $host = $remote->host;
17 push @requests, $remote->start(
19 on_result => sub { $self->print_exec_result($remote, @_) },
21 $stdout->print($host.' '.$_[0].': '.$_[1]);
22 $stdout->print("\n") unless $_[1] =~ /\n\Z/;
25 command_service => stream_exec => \@command
28 push @requests, $remote->start(
29 { on_result => sub { $self->print_exec_result($remote, @_) } },
30 command_service => exec => \@command
34 Tak->await_all(@requests);
37 sub print_exec_result {
38 my ($self, $remote, $result) = @_;
40 my $res = eval { $result->get }
42 $self->stderr->print("Host ${\$remote->host}: Error: $@\n");
46 my $code = $res->{exit_code};
48 "Host ${\$remote->host}: ".($code ? "NOT OK ${code}" : "OK")."\n"
51 $self->stdout->print("Stderr:\n${\$res->{stderr}}\n");
54 $self->stdout->print("Stdout:\n${\$res->{stdout}}\n");
58 sub each_repl (I=s@;m=s@;M=s@) {
59 my ($self, $remote, $options) = @_;
62 $remote->ensure(eval_service => 'Tak::EvalService');
63 foreach my $lib (@{$options->{'I'}||[]}) {
64 $remote->do(eval_service => eval => "lib->import(${\B::perlstring($lib)})");
66 foreach my $module (@{$options->{'m'}||[]}) {
67 $remote->do(eval_service => eval => "use ${module} ()");
69 foreach my $spec (@{$options->{'M'}||[]}) {
70 my ($module, $import) = split('=', $spec);
73 $extra = ' '.join(', ', map B::perlstring($_), split(',',$import));
75 $remote->do(eval_service => eval => "use ${module}${extra}");
77 Tak::REPL->new(client => $remote->curry('eval_service'))->run;