Class: Endoscope::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/endoscope/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_selectorObject

Returns the value of attribute dyno_selector.



9
10
11
# File 'lib/endoscope/cli.rb', line 9

def dyno_selector
  @dyno_selector
end

#issuedObject

Returns the value of attribute issued.



9
10
11
# File 'lib/endoscope/cli.rb', line 9

def issued
  @issued
end

#transport_optsObject

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_responsesObject



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

#readObject



95
96
97
98
# File 'lib/endoscope/cli.rb', line 95

def read
  read = gets
  read && read.chomp
end

#replObject



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_printerObject



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_threadObject



38
39
40
# File 'lib/endoscope/cli.rb', line 38

def start_responses_printing_thread
  @responses_thread = Thread.new(&method(:responses_printer))
end

#start_shellObject



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.message
    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

#transportObject



100
101
102
103
104
# File 'lib/endoscope/cli.rb', line 100

def transport
  @transport ||= begin
    Transport.new(transport_opts)
  end
end