lib/cli/console_helper.rb in vmc-0.3.17 vs lib/cli/console_helper.rb in vmc-0.3.18
- old
+ new
@@ -48,17 +48,18 @@
prompt = nil
err_msg = "Login attempt timed out."
5.times do
begin
results = @telnet_client.login("Name"=>auth_info["username"],
- "Password"=>auth_info["password"]) {|line|
- if line =~ /[$%#>] \z/n
- prompt = line
- elsif line =~ /Login failed/
- err_msg = line
- end
- }
+ "Password"=>auth_info["password"])
+ lines = results.sub("Login: Password: ", "").split("\n")
+ last_line = lines.pop
+ if last_line =~ /[$%#>] \z/n
+ prompt = last_line
+ elsif last_line =~ /Login failed/
+ err_msg = last_line
+ end
break
rescue TimeoutError
sleep 1
rescue EOFError
#This may happen if we login right after app starts
@@ -102,33 +103,35 @@
def telnet_client(port)
Net::Telnet.new({"Port"=>port, "Prompt"=>/[$%#>] \z|Login failed/n, "Timeout"=>30, "FailEOF"=>true})
end
def readline_with_history(prompt)
- line = Readline.readline(prompt, true)
- return '' if line.nil?
- #Don't keep blank or repeat commands in history
- if line =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == line
- Readline::HISTORY.pop
- end
+ line = Readline::readline(prompt)
+ return nil if line == nil || line == 'quit' || line == 'exit'
+ Readline::HISTORY.push(line) if not line =~ /^\s*$/ and Readline::HISTORY.to_a[-1] != line
line
end
def run_console(prompt)
- while(cmd = readline_with_history(prompt))
- if(cmd == "exit" || cmd == "quit")
- #TimeoutError expected, as exit doesn't return anything
- @telnet_client.cmd("String"=>cmd,"Timeout"=>1) rescue TimeoutError
- close_console
+ prev = trap("INT") { |x| exit_console; prev.call(x); exit }
+ prev = trap("TERM") { |x| exit_console; prev.call(x); exit }
+ loop do
+ cmd = readline_with_history(prompt)
+ if(cmd == nil)
+ exit_console
break
end
- if !cmd.empty?
- prompt = send_console_command_display_results(cmd, prompt)
- end
+ prompt = send_console_command_display_results(cmd, prompt)
end
end
+ def exit_console
+ #TimeoutError expected, as exit doesn't return anything
+ @telnet_client.cmd("String"=>"exit","Timeout"=>1) rescue TimeoutError
+ close_console
+ end
+
def send_console_command_display_results(cmd, prompt)
begin
lines = send_console_command cmd
#Assumes the last line is a prompt
prompt = lines.pop
@@ -152,6 +155,6 @@
Readline.completion_proc = proc {|s|
console_tab_completion_data s
}
end
end
-end
\ No newline at end of file
+end