Class: Endoscope::CLI
- Inherits:
-
Object
- Object
- Endoscope::CLI
- Defined in:
- lib/endoscope/cli.rb
Instance Attribute Summary collapse
-
#dyno_selector ⇒ Object
Returns the value of attribute dyno_selector.
-
#issued ⇒ Object
Returns the value of attribute issued.
-
#transport_opts ⇒ Object
Returns the value of attribute transport_opts.
Instance Method Summary collapse
- #eval_(command, out = $stdout) ⇒ Object
- #handle_response(res) ⇒ Object
-
#initialize(transport_opts = nil) ⇒ CLI
constructor
A new instance of CLI.
- #listen_to_command_responses ⇒ Object
- #re(out = $stdout) ⇒ Object
- #read ⇒ Object
- #repl ⇒ Object
- #responses_printer ⇒ Object
- #send_command(command, _out = $stdout) ⇒ Object
- #start(dyno_selector = 'all') ⇒ Object
- #start_responses_printing_thread ⇒ Object
- #start_shell ⇒ Object
- #transport ⇒ Object
Constructor Details
#initialize(transport_opts = nil) ⇒ CLI
Returns a new instance of CLI.
11 12 13 |
# File 'lib/endoscope/cli.rb', line 11 def initialize(transport_opts=nil) @transport_opts = transport_opts end |
Instance Attribute Details
#dyno_selector ⇒ Object
Returns the value of attribute dyno_selector.
9 10 11 |
# File 'lib/endoscope/cli.rb', line 9 def dyno_selector @dyno_selector end |
#issued ⇒ Object
Returns the value of attribute issued.
9 10 11 |
# File 'lib/endoscope/cli.rb', line 9 def issued @issued end |
#transport_opts ⇒ Object
Returns the value of attribute transport_opts.
9 10 11 |
# File 'lib/endoscope/cli.rb', line 9 def transport_opts @transport_opts end |
Instance Method Details
#eval_(command, out = $stdout) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/endoscope/cli.rb', line 83 def eval_(command, out = $stdout) case command when 'exit' throw(:break) when /^use / @dyno_selector = command.gsub('use ', '').strip puts "Now adressing commands to processes listening for #{dyno_selector.inspect}." else send_command(command, out) unless command.nil? || command.strip == '' end end |
#handle_response(res) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/endoscope/cli.rb', line 59 def handle_response(res) #p res #p issued #p res['id'] return unless issued.include?(res['id']) puts "From #{res['dyno_name']} :\n#{res['result']}\n\n" $stdout.flush end |
#listen_to_command_responses ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/endoscope/cli.rb', line 47 def listen_to_command_responses transport = Transport.new(transport_opts) transport.listen_to_responses do |response| handle_response(response) end rescue Redis::TimeoutError => _ retry rescue => err puts err.inspect puts err.backtrace.join("\n") end |
#re(out = $stdout) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/endoscope/cli.rb', line 76 def re(out = $stdout) command = read eval_(command, out) rescue Interrupt throw(:break) end |
#read ⇒ Object
95 96 97 98 |
# File 'lib/endoscope/cli.rb', line 95 def read read = gets read && read.chomp end |
#repl ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/endoscope/cli.rb', line 68 def repl catch(:break) do puts "\n\n ---\nRemote console ready:\n\n" $stdout.flush loop { re($stdout) } end end |
#responses_printer ⇒ Object
42 43 44 45 |
# File 'lib/endoscope/cli.rb', line 42 def responses_printer Thread.current[:name] = 'endoscope-responses-printing' listen_to_command_responses end |
#send_command(command, _out = $stdout) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/endoscope/cli.rb', line 106 def send_command(command, _out = $stdout) puts "Sending command #{command}..." command_id = SecureRandom.uuid issued << command_id transport.send_command(command_id, command, dyno_selector) end |
#start(dyno_selector = 'all') ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/endoscope/cli.rb', line 15 def start(dyno_selector = 'all') @dyno_selector = dyno_selector @issued = Set.new start_responses_printing_thread transport start_shell end |
#start_responses_printing_thread ⇒ Object
38 39 40 |
# File 'lib/endoscope/cli.rb', line 38 def start_responses_printing_thread @responses_thread = Thread.new(&method(:responses_printer)) end |
#start_shell ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/endoscope/cli.rb', line 23 def start_shell begin require "ripl" require "ripl/ripper" rescue LoadError => err puts err. abort("\nYou need to run:\n\ngem install ripl ripl-ripper\n\nThen launch this command again.") end cli = self Ripl::Shell.send(:define_method, :loop_eval) { |str| cli.eval_(str) } Ripl::Shell.send(:define_method, :print_result) { |_result| } Ripl.start( argv: [], irbrc: false, riplrc: false, ripper_prompt: ' | ') end |