Unverified Commit 0ce3b387 authored by Gavin Brown's avatar Gavin Brown
Browse files

moving towards some pipelining/scripting capabilities

parent 2d0bed82
#!/usr/bin/perl #!/usr/bin/perl
#
# Copyright CentralNic Group plc. # Copyright CentralNic Group plc.
# This program is Free Software; you can use it and/or modify it under the same # This program is Free Software; you can use it and/or modify it under the same
# terms as Perl itself. # terms as Perl itself.
#
# #
# Pepper::EPPClient is our own wrapper around Net::EPP::Simple # Pepper::EPPClient is our own wrapper around Net::EPP::Simple
...@@ -188,7 +190,6 @@ GetOptions($opt, ...@@ -188,7 +190,6 @@ GetOptions($opt,
'port=i', 'port=i',
'timeout=i', 'timeout=i',
'help', 'help',
'exec=s',
'insecure', 'insecure',
'lang=s', 'lang=s',
'debug', 'debug',
...@@ -236,18 +237,24 @@ if ($opt->{'help'}) { ...@@ -236,18 +237,24 @@ if ($opt->{'help'}) {
exit; exit;
} }
my $term = Term::ReadLine->new('pepper'); our $term;
$term = Term::ReadLine->new('pepper') if (-t STDIN && -t STDOUT);
my $outfh = ($term ? \*STDOUT : \*STDERR);
my $prompt = 'pepper> '; my $prompt = 'pepper> ';
my $histfile = $ENV{'HOME'}.'/.pepper_history'; my $histfile = $ENV{'HOME'}.'/.pepper_history';
my $xml = XML::LibXML->new; my $xml = XML::LibXML->new;
$term->ReadHistory($histfile) if ('Term::ReadLine::Gnu' eq $term->ReadLine); if ($term) {
$term->ReadHistory($histfile) if ('Term::ReadLine::Gnu' eq $term->ReadLine);
note('Welcome to pepper!'); note('Welcome to pepper!');
note(color('yellow').'> For best results, install Term::ReadLine::Gnu <'.color('reset')) if ('Term::ReadLine::Gnu' ne $term->ReadLine); note(color('yellow').'> For best results, install Term::ReadLine::Gnu <'.color('reset')) if ($term && 'Term::ReadLine::Gnu' ne $term->ReadLine);
}
my $epp = Pepper::EPPClient->new( my $epp = Pepper::EPPClient->new(
'host' => '', 'host' => '',
...@@ -260,8 +267,6 @@ my $epp = Pepper::EPPClient->new( ...@@ -260,8 +267,6 @@ my $epp = Pepper::EPPClient->new(
'lang' => ($opt->{'lang'} ? $opt->{'lang'} : 'en'), 'lang' => ($opt->{'lang'} ? $opt->{'lang'} : 'en'),
); );
my $interactive;
execute_command(sprintf('timeout %d', $opt->{'timeout'})) if ($opt->{'timeout'}); execute_command(sprintf('timeout %d', $opt->{'timeout'})) if ($opt->{'timeout'});
execute_command(sprintf('port %d', $opt->{'port'})) if ($opt->{'port'}); execute_command(sprintf('port %d', $opt->{'port'})) if ($opt->{'port'});
execute_command(sprintf('host "%s"', quotemeta($opt->{'host'}))) if ($opt->{'host'}); execute_command(sprintf('host "%s"', quotemeta($opt->{'host'}))) if ($opt->{'host'});
...@@ -279,19 +284,14 @@ if ($epp->{'user'} ne '' && $epp->{'pass'} ne '') { ...@@ -279,19 +284,14 @@ if ($epp->{'user'} ne '' && $epp->{'pass'} ne '') {
} }
if ($opt->{'exec'} ne '') { if ($term) {
if (!$epp->authenticated) { note("Entering interactive mode, exit with Ctrl-D, Ctrl-C or 'exit'");
fatal('not logged in');
} else { } else {
execute_command($opt->{'exec'}); note("Entering batch mode");
exit;
}
} }
$interactive = 1;
# #
# main loop # main loop
# #
...@@ -299,43 +299,68 @@ my $last; ...@@ -299,43 +299,68 @@ my $last;
while (1) { while (1) {
$prompt = sprintf('pepper (%s@%s)> ', $epp->{'user'}, $epp->{'host'}) if ($epp->authenticated); $prompt = sprintf('pepper (%s@%s)> ', $epp->{'user'}, $epp->{'host'}) if ($epp->authenticated);
my $command = $term->readline($prompt); my $command;
if ($term) {
$command = $term->readline($prompt);
} else {
$command = <STDIN>;
chomp($command);
# remove any trailing comment
$command =~ s/\#.*//g;
}
if (!defined($command)) { if (!defined($command)) {
print "\n";
last; last;
} elsif ($command ne '') { } elsif ($command ne '') {
if ($command eq '!!') { if ($term && $command eq '!!') {
execute_command($last) if ($last ne ''); execute_command($last) if ($last ne '');
} else { } else {
$last = $command; $last = $command;
note("Executing command '%s'", $command) if (!$term);
execute_command($command); execute_command($command);
} }
} }
} }
$term->WriteHistory($histfile) if ('Term::ReadLine::Gnu' eq $term->ReadLine); $term->WriteHistory($histfile) if ($term && 'Term::ReadLine::Gnu' eq $term->ReadLine);
handle_logout() if ($epp->connected && $epp->authenticated); handle_logout() if ($epp->connected && $epp->authenticated);
$epp->disconnect if ($epp->connected); $epp->disconnect if ($epp->connected);
note('Bye!'); note('Bye!');
exit(0);
sub execute_command { sub execute_command {
my $line = shift; my $line = shift;
return if ($line eq ''); return if ($line eq '');
my @args = shellwords($line); my @args = shellwords($line);
my $command = shift(@args); my $command = shift(@args);
my $fail;
if (!$term && $command =~/^!/) {
$fail = 1;
$command = substr($command, 1);
}
if (!defined($handlers->{$command})) { if (!defined($handlers->{$command})) {
error("Unknown command '$command'") if ($interactive); error("Unknown command '$command'");
handle_exit() if (!$term);
} else { } else {
&{$handlers->{$command}}(@args); &{$handlers->{$command}}(@args);
if (!$term && $fail) {
note("%s%04d%s %s", color($Net::EPP::Simple::Code < 2000 ? 'green' : 'red'), $Net::EPP::Simple::Code, color('reset'), $Net::EPP::Simple::Message);
fatal($Net::EPP::Simple::Message) if ($Net::EPP::Simple::Code > 1999);
}
} }
} }
...@@ -476,8 +501,13 @@ sub handle_login { ...@@ -476,8 +501,13 @@ sub handle_login {
$epp->{'quiet'} = ($verbose ? 0 : 1); $epp->{'quiet'} = ($verbose ? 0 : 1);
my $result = $epp->_login; my $result = $epp->_login;
$epp->{'quiet'} = 0; $epp->{'quiet'} = 0;
note("%s%04d%s %s", color($Net::EPP::Simple::Code < 2000 ? 'green' : 'red'), $Net::EPP::Simple::Code, color('reset'), $Net::EPP::Simple::Message); if ($result) {
note('Logged in OK!') if ($result); note('Logged in OK!');
} else {
error("%s%04d%s %s", color($Net::EPP::Simple::Code < 2000 ? 'green' : 'red'), $Net::EPP::Simple::Code, color('reset'), $Net::EPP::Simple::Message);
}
return $result; return $result;
} }
} }
...@@ -1233,14 +1263,20 @@ sub contact_update { ...@@ -1233,14 +1263,20 @@ sub contact_update {
sub note { sub note {
my ($fmt, @args) = @_; my ($fmt, @args) = @_;
my $msg = sprintf($fmt, @args); my $msg = '*** '.sprintf($fmt, @args);
print $msg."\n"; $outfh->print($msg."\n");
} }
sub error { sub error {
my ($fmt, @args) = @_; my ($fmt, @args) = @_;
note(color('red')."Error: ".color('reset').$fmt, @args); if ($term) {
return undef; note(color('red')."Error: ".color('reset').$fmt, @args);
return undef;
} else {
fatal(@_);
}
} }
sub fatal { sub fatal {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment