| 1 | package Shell::Carapace; | ||
| 2 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 2 | our $VERSION = "0.22"; | |
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Shell::Carapace - Simple realtime output for ssh and shell commands | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | use Shell::Carapace::Local; | ||
| 13 | |||
| 14 | # A callback is required. It can be used to log commands, output, errors | ||
| 15 | my $shell_callback = sub { | ||
| 16 | my ($category, $message, $host) = @_; | ||
| 17 | print " $host $message\n" if $category =~ /output/ && $message; | ||
| 18 | print "Running $message\n" if $category eq 'command'; | ||
| 19 | print "ERROR: cmd failed\n" if $category eq 'error'; | ||
| 20 | }; | ||
| 21 | |||
| 22 | my $shell = Shell::Carapace->shell(callback => $callback); | ||
| 23 | $shell->run(@cmd); # throws an exception if @cmd fails | ||
| 24 | |||
| 25 | my $ssh = Shell::Carapace->ssh( | ||
| 26 | callback => $callback, # required | ||
| 27 | host => $hostname, # required | ||
| 28 | ssh_options => $ssh_options, # a hash for Net::OpenSSH | ||
| 29 | ); | ||
| 30 | $ssh->run(@cmd); # throws an exception if @cmd fails | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Ever run a script that takes 30 minutes to run and have to wait | ||
| 35 | 30 minutes to see the output? This module solve that problem. | ||
| 36 | |||
| 37 | Shell::Carapace is a small wrapper around IPC::Open3::Simple and Net::OpenSSH. | ||
| 38 | It provides a callback so you can easily log or process cmd output in realtime. | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | =head2 shell(%options) | ||
| 43 | |||
| 44 | Creates and returns a Shell::Carapace::Shell object. All parameters are | ||
| 45 | optional except 'callback'. The following parameters are accepted: | ||
| 46 | |||
| 47 | callback : Required. A coderef which is executed in realtime as output | ||
| 48 | |||
| 49 | =head2 ssh(%options) | ||
| 50 | |||
| 51 | Creates and returns a Shell::Carapace::SSH object. All parameters are optional | ||
| 52 | except 'callback'. The following parameters are accepted: | ||
| 53 | |||
| 54 | callback : Required. A coderef which is executed in realtime as output | ||
| 55 | is emitted from the command. | ||
| 56 | host : Required. A string like 'localhost' or 'user@hostname' which | ||
| 57 | is passed to Net::OpenSSH. Net::OpenSSH defaults the username | ||
| 58 | to the current user. | ||
| 59 | ssh_options : A hash which is passed to Net::OpenSSH. | ||
| 60 | |||
| 61 | =head2 $shell->run(@cmd) | ||
| 62 | |||
| 63 | Execute the command locally via IPC::Open3::Simple. Calls the callback in | ||
| 64 | realtime for each line of output emitted from the command. | ||
| 65 | |||
| 66 | =head2 $ssh->run(@cmd) | ||
| 67 | |||
| 68 | Execute the command on a remote host via Net::OpenSSH. Calls the callback in | ||
| 69 | realtime for each line of output emitted from the command. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | 1 | ▶ | sub shell { |
|
Subroutine
shell: called CC=1 SCAR=0.0
Pod
documented
| |||
| 74 | 1 | my ($class, %args) = @_; | |
| 75 | 1 | require Shell::Carapace::Shell; | |
| 76 | 1 | return Shell::Carapace::Shell->new(%args); | |
| 77 | } | ||
| 78 | |||
| 79 | 0 | ▶ | sub ssh { |
|
Subroutine
ssh: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 80 | 0 | my ($class, %args) = @_; | |
| 81 | 0 | require Shell::Carapace::SSH; | |
| 82 | 0 | return Shell::Carapace::SSH->new(%args); | |
| 83 | } | ||
| 84 | |||
| 85 | =head1 ABOUT THE NAME | ||
| 86 | |||
| 87 | Carapace: n. A protective, shell-like covering likened to that of a turtle or crustacean | ||
| 88 | |||
| 89 | =head1 LICENSE | ||
| 90 | |||
| 91 | This library is free software; you can redistribute it and/or modify | ||
| 92 | it under the same terms as Perl itself. | ||
| 93 | |||
| 94 | =head1 AUTHOR | ||
| 95 | |||
| 96 | Eric Johnson E<lt>eric.git@iijo.orgE<gt> | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | 2 | 1; | |