Skip to content

Commit

Permalink
Added an option to connect through a UNIX socket
Browse files Browse the repository at this point in the history
It defaults to ~/.rmate.socket
  • Loading branch information
bpiwowar authored and sorbits committed Jun 7, 2015
1 parent 677a34c commit 4a55b26
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
9 changes: 7 additions & 2 deletions README.mdown
Expand Up @@ -4,6 +4,10 @@ If you wish to activate TextMate from an ssh session you can do so by copying th

ssh -R 52698:localhost:52698 user@example.org

or, if using unix sockets (available with OpenSSH v6.7 and above):

ssh -R /home/user/.rmate.socket:localhost:52698 user@example.org

# Install

## Rubygems
Expand Down Expand Up @@ -33,10 +37,11 @@ If `~/bin` is not already in your `PATH` then you may want to add something like

Call `rmate --help` for a list of options. Default options can be set in `/etc/rmate.rc` or `~/.rmate.rc`, e.g.:

host: auto # prefer host from SSH_CONNECTION over localhost
host: auto # Prefer host from SSH_CONNECTION over localhost
port: 52698
unixsocket: ~/.rmate.socket # Use this socket file if it exists

You can also set the `RMATE_HOST` and `RMATE_PORT` environment variables.
You can also set the `RMATE_HOST`, `RMATE_PORT` and `RMATE_UNIXSOCKET` environment variables.

For more info see this [blog post about rmate](http://blog.macromates.com/2011/mate-and-rmate/ "TextMate Blog » mate and rmate").

Expand Down
48 changes: 30 additions & 18 deletions bin/rmate
Expand Up @@ -16,10 +16,10 @@ module Rmate
VERSION_STRING = "rmate version #{Rmate::VERSION} (#{Rmate::DATE})"

class Settings
attr_accessor :host, :port, :wait, :force, :verbose, :lines, :names, :types
attr_accessor :host, :port, :unixsocket, :wait, :force, :verbose, :lines, :names, :types

def initialize
@host, @port = 'localhost', 52698
@host, @port, @unixsocket = 'localhost', 52698, '~/.rmate.socket'

@wait = false
@force = false
Expand All @@ -30,8 +30,9 @@ module Rmate

read_disk_settings

@host = ENV['RMATE_HOST'].to_s if ENV.has_key? 'RMATE_HOST'
@port = ENV['RMATE_PORT'].to_i if ENV.has_key? 'RMATE_PORT'
@host = ENV['RMATE_HOST'].to_s if ENV.has_key? 'RMATE_HOST'
@port = ENV['RMATE_PORT'].to_i if ENV.has_key? 'RMATE_PORT'
@unixsocket = ENV['RMATE_UNIXSOCKET'].to_s if ENV.has_key? 'RMATE_UNIXSOCKET'

parse_cli_options

Expand All @@ -43,22 +44,25 @@ module Rmate
file = File.expand_path current_file
if File.exist? file
params = YAML::load(File.open(file))
@host = params["host"] unless params["host"].nil?
@port = params["port"] unless params["port"].nil?
@host = params["host"] unless params["host"].nil?
@port = params["port"] unless params["port"].nil?
@unixsocket = params["unixsocket"] unless params["unixsocket"].nil?
end
end
end

def parse_cli_options
OptionParser.new do |o|
o.on( '--host=name', "Connect to host.", "Use 'auto' to detect the host from SSH.", "Defaults to #{@host}.") { |v| @host = v }
o.on('-p', '--port=#', Integer, "Port number to use for connection.", "Defaults to #{@port}.") { |v| @port = v }
o.on('-w', '--[no-]wait', 'Wait for file to be closed by TextMate.') { |v| @wait = v }
o.on('-l', '--line [NUMBER]', 'Place caret on line [NUMBER] after loading file.') { |v| @lines <<= v }
o.on('-m', '--name [NAME]', 'The display name shown in TextMate.') { |v| @names <<= v }
o.on('-t', '--type [TYPE]', 'Treat file as having [TYPE].') { |v| @types <<= v }
o.on('-f', '--force', 'Open even if the file is not writable.') { |v| @force = v }
o.on('-v', '--verbose', 'Verbose logging messages.') { |v| @verbose = v }
o.on( '--host=name', "Connect to host.", "Use 'auto' to detect the host from SSH.", "Defaults to #{@host}.") { |v| @host = v }
o.on('-s', '--unixsocket=name', "UNIX socket path.", "Takes precedence over host/port if the file exists", \
"Default #{@unixsocket}") { |v| @unixsocket = v }
o.on('-p', '--port=#', Integer, "Port number to use for connection.", "Defaults to #{@port}.") { |v| @port = v }
o.on('-w', '--[no-]wait', 'Wait for file to be closed by TextMate.') { |v| @wait = v }
o.on('-l', '--line [NUMBER]', 'Place caret on line [NUMBER] after loading file.') { |v| @lines <<= v }
o.on('-m', '--name [NAME]', 'The display name shown in TextMate.') { |v| @names <<= v }
o.on('-t', '--type [TYPE]', 'Treat file as having [TYPE].') { |v| @types <<= v }
o.on('-f', '--force', 'Open even if the file is not writable.') { |v| @force = v }
o.on('-v', '--verbose', 'Verbose logging messages.') { |v| @verbose = v }
o.on_tail('-h', '--help', 'Show this message.') { puts o; exit }
o.on_tail( '--version', 'Show version.') { puts VERSION_STRING; exit }
o.parse!
Expand Down Expand Up @@ -153,8 +157,16 @@ module Rmate
end
end

def connect_and_handle_cmds(host, port, cmds)
socket = TCPSocket.new(host, port)
def connect_and_handle_cmds(host, port, unixsocketpath, cmds)
socket = nil
unixsocketpath = File.expand_path(unixsocketpath) unless unixsocketpath.nil?
if unixsocketpath.nil? || !File.exist?(unixsocketpath)
# socket is now ready to communicate.
socket = TCPSocket.new(host, port)
else
$stderr.puts "Using UNIX socket to connect: ‘#{unixsocketpath}’" if $settings.verbose
socket = UNIXSocket.new(unixsocketpath)
end
server_info = socket.readline.chomp
$stderr.puts "Connect: ‘#{server_info}’" if $settings.verbose

Expand Down Expand Up @@ -204,9 +216,9 @@ end

unless $settings.wait
pid = fork do
Rmate::connect_and_handle_cmds($settings.host, $settings.port, cmds)
Rmate::connect_and_handle_cmds($settings.host, $settings.port, $settings.unixsocket, cmds)
end
Process.detach(pid)
else
Rmate::connect_and_handle_cmds($settings.host, $settings.port, cmds)
Rmate::connect_and_handle_cmds($settings.host, $settings.port, $settings.unixsocket, cmds)
end

0 comments on commit 4a55b26

Please sign in to comment.