Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make einhornsh script-friendly

Closes #8
  • Loading branch information...
commit f1f5d061e567cd07680eae9aeb2155d779787b5b 1 parent dbd6f56
Greg Brockman gdb authored
Showing with 31 additions and 12 deletions.
  1. +31 −12 bin/einhornsh
43 bin/einhornsh
View
@@ -15,21 +15,21 @@ module Einhorn
end
def run
- puts "Enter 'help' if you're not sure what to do."
- puts
- puts 'Type "quit" or "exit" to quit at any time'
+ emit("Enter 'help' if you're not sure what to do.")
+ emit
+ emit('Type "quit" or "exit" to quit at any time')
while line = Readline.readline('> ', true)
if ['quit', 'exit'].include?(line)
- puts "Goodbye!"
+ emit("Goodbye!")
return
end
begin
response = @client.command({'command' => line})
rescue Errno::EPIPE => e
- puts "einhornsh: Error communicating with Einhorn: #{e} (#{e.class})"
- puts "einhornsh: Attempting to reconnect..."
+ emit("einhornsh: Error communicating with Einhorn: #{e} (#{e.class})")
+ emit("einhornsh: Attempting to reconnect...")
reconnect
retry
@@ -43,7 +43,7 @@ module Einhorn
@client = Einhorn::Client.for_path(@path_to_socket)
rescue Errno::ENOENT => e
# TODO: The exit here is a biit of a layering violation.
- puts <<EOF
+ Einhorn::EinhornSH.emit(<<EOF)
Could not connect to Einhorn master process:
#{e}
@@ -53,12 +53,28 @@ should pass einhornsh the cmd_name (-c argument) provided to Einhorn.
EOF
exit(1)
end
- ehlo
+ ehlo if interactive?
end
def ehlo
response = @client.command('command' => 'ehlo', 'user' => ENV['USER'])
- puts response['message']
+ emit(response['message'])
+ end
+
+ def self.emit(message=nil, force=false)
+ $stderr.puts(message || '') if interactive? || force
+ end
+
+ def self.interactive?
+ $stdin.isatty
+ end
+
+ def emit(*args)
+ self.class.emit(*args)
+ end
+
+ def interactive?
+ self.class.interactive?
end
end
end
@@ -75,7 +91,7 @@ as a positional argument or using `-c`. If you're running your Einhorn
with a `-d`, provide the same argument here."
opts.on('-h', '--help', 'Display this message') do
- puts opts
+ Einhorn::EinhornSH.emit(opts, true)
exit(1)
end
@@ -90,11 +106,14 @@ with a `-d`, provide the same argument here."
optparse.parse!
if ARGV.length > 1
- puts optparse
+ Einhorn::EinhornSH.emit(optparse, true)
return 1
end
- Signal.trap("INT") {puts; exit(0)}
+ Signal.trap("INT") do
+ Einhorn::EinhornSH.emit
+ exit(0)
+ end
path_to_socket = options[:socket_path]
Please sign in to comment.
Something went wrong with that request. Please try again.