protect against re-upgrading
[scpubgit/Tak.git] / lib / Tak.pm
CommitLineData
31a246e4 1package Tak;
2
3use Tak::Loop;
4use strictures 1;
5
51c8325b 6our $VERSION = '0.001001'; # 0.1.1
7
fb0f5964 8our ($loop, $did_upgrade);
31a246e4 9
10sub loop { $loop ||= Tak::Loop->new }
11
addc801e 12sub loop_upgrade {
fb0f5964 13 return if $did_upgrade;
addc801e 14 require IO::Async::Loop;
15 my $new_loop = IO::Async::Loop->new;
16 $loop->pass_watches_to($new_loop) if $loop;
17 $loop = $new_loop;
fb0f5964 18 $did_upgrade = 1;
addc801e 19}
20
31a246e4 21sub loop_until {
22 my ($class, $done) = @_;
23 return if $done;
addc801e 24 $class->loop->loop_once until $_[1];
31a246e4 25}
26
934a93db 27sub await_all {
28 my ($class, @requests) = @_;
29 @requests = grep !$_->is_done, @requests;
30 return unless @requests;
31 my %req = map +("$_" => "$_"), @requests;
32 my $done;
934a93db 33 my %on_r = map {
b1aab0cf 34 my $orig = $_->{on_result};
934a93db 35 my $tag = $req{$_};
36 ($_ => sub { delete $req{$tag}; $orig->(@_); $done = 1 unless keys %req; })
37 } @requests;
b1aab0cf 38 my $call = sub { $class->loop_until($done) };
39 foreach (@requests) {
40 my $req = $_;
41 my $inner = $call;
42 $call = sub { local $req->{on_result} = $on_r{$req}; $inner->() };
43 }
44 $call->();
934a93db 45 return;
46}
47
51c8325b 48"for lexie";
49
50=head1 NAME
51
52Tak - Multi host remote control over ssh
53
54=head1 SYNOPSIS
55
56 bin/tak -h user1@host1 -h user2@host2 exec cat /etc/hostname
57
58or
59
60 in Takfile:
61
62 package Tak::MyScript;
63
64 use Tak::Takfile;
65 use Tak::ObjectClient;
66
67 sub each_get_homedir {
68 my ($self, $remote) = @_;
69 my $oc = Tak::ObjectClient->new(remote => $remote);
70 my $home = $oc->new_object('Path::Class::Dir')->absolute->stringify;
71 $self->stdout->print(
72 $remote->host.': '.$home."\n"
73 );
74 }
75
76 1;
77
78then
79
80 tak -h something get-homedir
81
82=head1 WHERE'S THE REST?
83
84A drink leaked in my bag on the way back from LPW. You'll get more once I
85get my laptop's drive into an enclosure and decant the slides.
86
87=head1 AUTHOR
88
89mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
90
91=head1 CONTRIBUTORS
92
93None required yet. Maybe this module is perfect (hahahahaha ...).
94
95=head1 COPYRIGHT
96
97Copyright (c) 2011 the strictures L</AUTHOR> and L</CONTRIBUTORS>
98as listed above.
99
100=head1 LICENSE
101
102This library is free software and may be distributed under the same terms
103as perl itself.
104
105=cut