Sha256: ac039660fd38c1755d93f5853f12045ad1085e2d53c1698fe9359c543afe81b2

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

#!/usr/bin/env ruby
=begin =======================================================================

# TORK-REMOTE 1 2012-09-26 18.2.3

## NAME

tork-remote - controls tork(1) programs

## SYNOPSIS

`tork-remote` [*OPTION*]... *PROGRAM*

## DESCRIPTION

This program sends single-line JSON messages read from its stdin to the given
*PROGRAM* which is already running in the same working directory as this one.

If lines read from stdin are not single-line JSON messages, then they are
split into an array of words, using the same word-splitting algorithm as
sh(1), before being sent to the *PROGRAM* as a single-line JSON message.

If the *PROGRAM* sends any messages in response, then they are printed to
stdout if they are valid single-line JSON messages or to stderr otherwise.

## OPTIONS

`-h`, `--help`
  Show this help manual.

## EXIT STATUS

1
  Could not connect to the *PROGRAM*.

2
  Lost connection to the *PROGRAM*.

## SEE ALSO

tork(1), tork-driver(1), tork-engine(1), tork-master(1)

=end =========================================================================

$0 = File.basename(__FILE__) # for easier identification in ps(1) output

require 'binman'
BinMan.help

require 'socket'
require 'shellwords'
require 'tork/server'

program = ARGV.shift or raise ArgumentError, 'PROGRAM not given'
address = Tork::Server.address(program)

begin
  UNIXSocket.open(address) do |socket|
    # messages to remote from server
    Thread.new do
      while input = socket.gets
        stream = input =~ Tork::Server::JSON_REGEXP ? STDOUT : STDERR
        stream.puts input
        stream.flush
      end
      warn "#{$0}: lost connection to #{program}"
      exit 2
    end

    # messages from remote to server
    while output = STDIN.gets
      socket.puts output
    end
  end
# ECONNREFUSED is for abstract namespace UNIXSocket
rescue Errno::ENOENT, Errno::ECONNREFUSED => error
  warn "#{$0}: could not connect to #{program}"
  warn "#{error}#{address.inspect}"
  exit 1
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tork-19.0.0 bin/tork-remote