Sha256: 19bf76a868431d93ad7d484b9b1663d6d584e3d5998d5279146d8e0fdf921854

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

#!/usr/bin/env ruby

gem 'trollop', '~> 1.16.2'
require "trollop"

require "text_tunnel/version"

config = Trollop.options do
  version "text_tunnel #{TextTunnel::VERSION}"
  banner <<-EOS
Usage:
       text_tunnel [options] <filename>
where [options] are:
  EOS

  opt :port, "Port", :type => :int, :default => 1777
  opt :fallback_editor, "Fallback editor if error connecting to text_tunneld web server", :default => "vi"
end

Trollop.die "text_tunnel requires exactly one filename argument" unless ARGV.size == 1
file_path = ARGV[0]

# Ensure that we will receive the interrupt (crtl+c) signal. When text_tunnel
# is launched by crontab -e it blocks or ignores the INT signal. This restores
# default functionality.
Signal.trap "INT", "DEFAULT"

def try_fallback_editor(fallback_editor, file_path)
  puts "Attempting fallback editor: #{fallback_editor} #{file_path}"
  success = system fallback_editor, file_path
  puts "ERROR: Fallback editor failed." unless success
end

require "text_tunnel/client"
begin
  client = Client.new(config[:port], file_path)
  puts "Editing #{file_path} via text_tunnel, CRTL+C to stop"

  loop do
    if client.poll
      puts "Wrote #{file_path} at #{Time.now}"
    end

    sleep 1
  end
rescue Errno::ECONNREFUSED => e
  puts "ERROR: Unable to connect to text_tunneld web server."
  try_fallback_editor config[:fallback_editor], file_path
rescue Errno::EISDIR
  puts "ERROR: #{file_path} is a directory."
rescue Errno::EACCES
  puts "ERROR: Access denied - #{file_path}"
rescue RestClient::Exception, UnexpectedResponseError => e
  puts "ERROR: Connected to a web server, but received an unexpected response."
  puts "HTTP Status Code: #{e.http_code}"
  puts "HTTP Body:"
  puts e.http_body
  try_fallback_editor config[:fallback_editor], file_path
rescue Interrupt
  puts "Exiting..."
ensure
  client.cleanup if client
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
text_tunnel-0.2.0 bin/text_tunnel