From: Matt S Trout Date: Fri, 11 Nov 2011 13:39:57 +0000 (+0000) Subject: move exec code to Client.pm, add get_homedir example to Takfile X-Git-Tag: v0.001001~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5e5069cae55b5d9ff26a9524b4bc9e027a8fdd43;p=scpubgit%2FTak.git move exec code to Client.pm, add get_homedir example to Takfile --- diff --git a/Takfile b/Takfile index 62b8135..618f348 100644 --- a/Takfile +++ b/Takfile @@ -1,57 +1,15 @@ package Tak::MyScript; use Tak::Takfile; +use Tak::ObjectClient; -sub every_exec (stream|s) { - my ($self, $remotes, $options, @command) = @_; - - my @requests; - - $_->ensure(command_service => 'Tak::CommandService') for @$remotes; - - foreach my $remote (@$remotes) { - if ($options->{stream}) { - my $stdout = $self->stdout; - my $host = $remote->host; - push @requests, $remote->start( - { - on_result => sub { $self->print_exec_result($remote, @_) }, - on_progress => sub { - $stdout->print($host.' '.$_[0].': '.$_[1]); - $stdout->print("\n") unless $_[1] =~ /\n\Z/; - } - }, - command_service => stream_exec => \@command - ); - } else { - push @requests, $remote->start( - { on_result => sub { $self->print_exec_result($remote, @_) } }, - command_service => exec => \@command - ); - } - } - Tak->await_all(@requests); -} - -sub print_exec_result { - my ($self, $remote, $result) = @_; - - my $res = eval { $result->get } - or do { - $self->stderr->print("Host ${\$remote->host}: Error: $@\n"); - return; - }; - - my $code = $res->{exit_code}; +sub each_get_homedir { + my ($self, $remote) = @_; + my $oc = Tak::ObjectClient->new(remote => $remote); + my $home = $oc->new_object('Path::Class::Dir')->absolute->stringify; $self->stdout->print( - "Host ${\$remote->host}: ".($code ? "NOT OK ${code}" : "OK")."\n" + $remote->host.': '.$home."\n" ); - if ($res->{stderr}) { - $self->stdout->print("Stderr:\n${\$res->{stderr}}\n"); - } - if ($res->{stdout}) { - $self->stdout->print("Stdout:\n${\$res->{stdout}}\n"); - } } 1; diff --git a/lib/Tak/ObjectClient.pm b/lib/Tak/ObjectClient.pm index bf3e5e3..0158964 100644 --- a/lib/Tak/ObjectClient.pm +++ b/lib/Tak/ObjectClient.pm @@ -12,7 +12,7 @@ has object_service => (is => 'lazy'); sub _build_object_service { my ($self) = @_; my $remote = $self->remote; - $remote->do(meta => register => object_service => 'Tak::ObjectService'); + $remote->ensure(object_service => 'Tak::ObjectService'); $remote->curry('object_service'); } diff --git a/lib/Tak/Script.pm b/lib/Tak/Script.pm index 00208d2..446b434 100644 --- a/lib/Tak/Script.pm +++ b/lib/Tak/Script.pm @@ -10,6 +10,8 @@ use Log::Contextual qw(:log); use Log::Contextual::SimpleLogger; use Moo; +with 'Tak::Role::ScriptActions'; + has options => (is => 'ro', required => 1); has env => (is => 'ro', required => 1); @@ -76,6 +78,7 @@ sub run { return $self->local_help; } my $cmd = shift(@argv); + $cmd =~ s/-/_/g; if (my $code = $self->can("local_$cmd")) { return $self->_run($cmd, $code, @argv); } elsif ($code = $self->can("each_$cmd")) {