| 1 | package MYDan::VSSH; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use MYDan::VSSH::Execute; | ||
| 6 | use MYDan::VSSH::Comp; | ||
| 7 | use MYDan::VSSH::History; | ||
| 8 | use MYDan::VSSH::Print; | ||
| 9 | |||
| 10 | $|++; | ||
| 11 | |||
| 12 | our %RUN = ( max => 128, timeout => 300 ); | ||
| 13 | |||
| 14 | sub new | ||
| 15 | { | ||
| 16 | my ( $class, %self ) = @_; | ||
| 17 | |||
| 18 | $self{config} = +{}; | ||
| 19 | |||
| 20 | bless \%self, ref $class || $class; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub run | ||
| 24 | { | ||
| 25 | my ( $this, %run, %busy ) = ( shift, %RUN, @_ ); | ||
| 26 | |||
| 27 | my $history = MYDan::VSSH::History->new(); | ||
| 28 | my $execute = MYDan::VSSH::Execute->new( node => $this->{node} ); | ||
| 29 | my $print = MYDan::VSSH::Print->new(); | ||
| 30 | $print->welcome(); | ||
| 31 | |||
| 32 | my $c = scalar @{$this->{node}}; | ||
| 33 | while ( 1 ) | ||
| 34 | { | ||
| 35 | next unless my $cmd = $this->_comp( $c, @run{qw( user sudo )} ); | ||
| 36 | exit if $cmd eq 'exit' || $cmd eq 'quit' || $cmd eq 'logout'; | ||
| 37 | |||
| 38 | my %result = $execute->run( %run, cmd => $cmd ); | ||
| 39 | $print->result( %result ); | ||
| 40 | |||
| 41 | $history->push( $cmd ); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _comp | ||
| 46 | { | ||
| 47 | my ( $self, $c ) = splice @_, 0, 2; | ||
| 48 | my $tc = MYDan::VSSH::Comp->new( | ||
| 49 | 'clear' => qr/\cl/, | ||
| 50 | 'reverse' => qr/\cr/, | ||
| 51 | 'wipe' => qr/\cw/, | ||
| 52 | prompt => sprintf( "%s ($c)sh#", join( ':', grep{$_}@_)|| 'mydan' ), | ||
| 53 | choices => [ ], | ||
| 54 | up => qr/\x1b\[[A]/, | ||
| 55 | down => qr/\x1b\[[B]/, | ||
| 56 | left => qr/\x1b\[[D]/, | ||
| 57 | right => qr/\x1b\[[C]/, | ||
| 58 | quit => qr/[\cc]/, | ||
| 59 | ); | ||
| 60 | my ( $cmd, $danger ) = $tc->complete(); | ||
| 61 | return $cmd unless $danger; | ||
| 62 | while( 1 ) | ||
| 63 | { | ||
| 64 | print "$cmd [y/n]:"; | ||
| 65 | my $in = <STDIN>; | ||
| 66 | next unless $in; | ||
| 67 | return $cmd if $in eq "y\n"; | ||
| 68 | return undef if $in eq "n\n"; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; |