blib/lib/Zed/Plugin/Host/Batch.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 91 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Zed::Plugin::Host::Batch;
2 use strict;
3  
4 use Zed::Config::Space;
5 use Zed::Output;
6 use Zed::Plugin;
7 use Zed::Config::Env;
8  
9 =head1 SYNOPSIS
10  
11 batch space using regex
12 ex:
13 batch idc 2
14  
15 =cut
16  
17 invoke "batch" => sub {
18 my( $group, $list, $batch, @ret, %group );
19  
20 ($group, $batch) = @_;
21  
22 $group = env('batch')->{$group};
23 $group = qr/$group/||undef if $group;
24  
25 $list ||= usespace();
26 $batch ||= 1;
27  
28 debug("group:", $group);
29 debug("list:", $list);
30  
31 info("illegal batch($batch)") and return unless $batch > 0;
32 info( "input list error" ) and return unless $list;
33  
34 my @host = space($list);
35 info("no hosts in $list") and return unless @host;
36
37 $batch = scalar @host * $batch if $batch > 0 && $batch < 1;
38  
39 map{$_ =~ $group; debug('grep:',$1); push @{ $group{ $1 || undef} }, $_ }@host if $group;
40 $group{all} = \@host unless keys %group;
41  
42 debug('groups:', \%group);
43  
44 while(keys %group)
45 {
46 my @group;
47 map{
48 my $value = $group{$_} ;
49 push @group, splice @$value, 0, $batch;
50 delete $group{$_} unless @$value;
51 }keys %group;
52 push @ret, \@group;
53 }
54
55  
56 debug("ret:", \@ret);
57  
58 return unless @ret;
59  
60 space_clean($list);
61 for my $c(0..(scalar @ret - 1))
62 {
63 my( $key, $v ) = ($list.$c, $ret[$c]);
64 space($key, $v);
65 info("add $key hosts[", scalar @$v, "] suc!");
66 }
67 };
68 1;