blib/lib/MYDan/Util/Go.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 155 CC 14 SCAR 39.3
CC
14
Cov
0%
go
90.0
new
42.0
BEGIN
2.0
CRAP 51.1
1 package MYDan::Util::Go;
2 use strict;
3 use warnings;
4  
5 use Expect;
6 use YAML::XS;
7 use Authen::OATH;
8 use Convert::Base32 qw( decode_base32 );
9  
10 our $TIMEOUT = 20;
11  
12 =head1 SYNOPSIS
13  
14 use MYDan::Util::Go;
15 MYDan::Util::Go->new( '/path/go')->go( 'grep' );
16  
17 =cut
18  
19 sub new
20 {
21 my ( $class, $path ) = @_;
22  
23 $path = "$path.private" if -f "$path.private";
24 my $conf = $path && -e $path ? eval{ YAML::XS::LoadFile $path } : +{};
25 die "load $path fail:$@" if $@;
26  
27 $conf = YAML::XS::Dump $conf;
28 map{ $conf =~ s/\$ENV\{$_\}/$ENV{$_}/g; }keys %ENV;
29 $conf = eval{ YAML::XS::Load $conf };
30 die "load conf fail:$@" if $@;
31  
32 my $range = MYDan::Node->new( MYDan::Util::OptConf->load()->dump( 'range') );
33 my $hosts = MYDan::Util::Hosts->new();
34 for my $k ( keys %$conf )
35 {
36 my $v = $conf->{$k};
37 next unless my $node = delete $v->{range};
38 delete $conf->{$k};
39 map{
40 my %v = %$v;
41 my %h = $hosts->match( $_ );
42 my $n = $h{$_} || $_;
43 $v{go} =~ s/{}/$n/g;
44 $conf->{"$k:$_"} = \%v;
45 }$range->load( $node )->list;
46 }
47  
48 bless $conf, ref $class || $class;
49 }
50  
51 sub go
52 {
53  
54 my ( $this, $grep ) = @_;
55  
56 my @host = sort keys %$this;
57  
58 GOTO:
59  
60 @host = grep{ $_ =~ /$grep/ }@host if defined $grep;
61  
62 return unless @host;
63  
64 my $i = 0;
65 if( @host > 1 )
66 {
67 my @host = map { sprintf "[ %d ] %s", $_ + 1, $host[$_] } 0 .. $#host;
68 print STDERR join "\n", @host, "please select: [ 1 ] ";
69  
70 my $x = <STDIN>;
71 if( $x && $x =~ s/^\/// ) { $grep = $x; chomp $grep; goto GOTO; }
72 $i = $1 - 1 if $x =~ /(\d+)/ && $1 && $1 <= @host;
73 }
74  
75 my $conf = $this->{$host[$i]};
76  
77 my $exp = Expect->new();
78 $SIG{WINCH} = sub
79 {
80 $exp->slave->clone_winsize_from( \*STDIN );
81 kill WINCH => $exp->pid if $exp->pid;
82 local $SIG{WINCH} = $SIG{WINCH};
83 };
84  
85 $conf->{expect} = +{} unless $conf->{expect} && ref $conf->{expect} eq 'HASH';
86  
87 for ( keys %{$conf->{expect}} )
88 {
89 next unless $conf->{expect}{$_} =~ /googlecode\s*:\s*(\w+)/;
90 $conf->{expect}{$_} = Authen::OATH->new->totp( decode_base32( $1 ));
91 }
92  
93 $exp->slave->clone_winsize_from( \*STDIN );
94 $exp->spawn( $conf->{go} );
95 $exp->expect
96 (
97 $TIMEOUT,
98 [ qr/[#\$%] $/ => sub { $exp->interact; } ],
99 [ qr/yes\/no/ => sub { $exp->send( "yes\n" ); exp_continue; } ],
100 map{ my $v = $conf->{expect}{$_};[ qr/$_/ => sub { $exp->send( "$v\n" ); exp_continue; } ] }keys %{ $conf->{expect} }
101 );
102 }
103  
104 1;