simple client with remoting
[scpubgit/Tak.git] / jreplclient
CommitLineData
ff0abfba 1#!/usr/bin/env perl
2
3use strictures 1;
4use IPC::Open2;
5use Term::ReadLine;
6use JSON::PP qw(encode_json decode_json);
7
8my $cmd = do {
9 if (my $host = $ARGV[0]) {
10 'cat jsonrepl.packed - | ssh '.$host.' perl -'
11 } else {
12 'perl jsonrepl'
13 }
14};
15
16my $pid = open2(my $out, my $in, $cmd);
17
18my $read = Term::ReadLine->new('REPL');
19
20while (1) {
21 my $line = $read->readline('re.pl$ ');
22 exit unless defined $line;
23 next unless length $line;
24 print $in encode_json([ EVAL => $line ])."\n";
25 my $reply = decode_json scalar readline($out);
26 if ($reply->[0] eq 'MISTAKE') {
27 die "Botch: ".join(': ', @{$reply}[1,2]);
28 }
29 my $ret = $reply->[1];
30 print $ret->{return};
31 if ($ret->{stdout}) {
32 chomp($ret->{stdout});
33 print "STDOUT:\n${\$ret->{stdout}}\n";
34 }
35 if ($ret->{stderr}) {
36 chomp($ret->{stderr});
37 print "STDERR:\n${\$ret->{stderr}}\n";
38 }
39}