class Retscli::Shell

Constants

EXIT_COMMANDS

Public Class Methods

new(client) click to toggle source
# File lib/retscli/shell.rb, line 10
def initialize(client)
  @stty_save = `stty -g`.chomp
  setup_readline_autocomplete
  @client = client
  @client.login
  @display_adapter = Retscli::DisplayAdapter.new(client)
  @colorer = ::Thor::Shell::Color.new
  retrieve_metadata
end

Public Instance Methods

execute_shell_command(line, out=$stdout) click to toggle source

NOTE: this should probably be private, but making it public allowed for easier testing without having to deal with mocking readline. Can we find a better way?

# File lib/retscli/shell.rb, line 33
def execute_shell_command(line, out=$stdout)
  begin
    Retscli::ShellCommands.start(split_line(line), :display_adapter => @display_adapter)
  rescue => e
    out.puts @colorer.set_color(e.message, :red)
  end
end
start() click to toggle source
# File lib/retscli/shell.rb, line 20
def start
  while line = readline_with_hist_management
    if EXIT_COMMANDS.include?(line)
      close
    else
      execute_shell_command(line)
    end
  end
end

Private Instance Methods

close() click to toggle source
# File lib/retscli/shell.rb, line 87
def close
  system('stty', @stty_save)

  begin
    @client.logout
  rescue => e
    puts @colorer.set_color(e.message, :red)
  end

  exit
end
prompt() click to toggle source
# File lib/retscli/shell.rb, line 79
def prompt
  @prompt ||=
    begin
      uri = URI.parse(@client.login_url)
      @colorer.set_color("#{@client.options[:username]}@#{uri.host} > ", :blue)
    end
end
readline_with_hist_management() click to toggle source
# File lib/retscli/shell.rb, line 68
def readline_with_hist_management
  line = Readline.readline(prompt, true)
  return nil if line.nil?

  if line =~ /^\s*$/ || Readline::HISTORY.to_a[-2] == line
    Readline::HISTORY.pop
  end

  line
end
retrieve_metadata() click to toggle source
# File lib/retscli/shell.rb, line 48
def retrieve_metadata
  spinner = TTY::Spinner.new("Retrieving metadata #{@colorer.set_color(':spinner', :yellow)} ...", format: :box_bounce)
  spinner.start

  begin
    @client.metadata
  rescue => e
    puts "Error retrieving metadata. #{e.message}"
  ensure
    spinner.stop(@colorer.set_color('done', :green))
    puts ''
  end
end
setup_readline_autocomplete() click to toggle source
# File lib/retscli/shell.rb, line 62
def setup_readline_autocomplete
  comp = proc { |s| Retscli::ShellCommands.command_list.grep( /^#{Regexp.escape(s)}/ ) }
  Readline.completion_append_character = " "
  Readline.completion_proc = comp
end
split_line(line) click to toggle source

Split line, immitating ARGV behavior where it splits on spaces except when quoted

# File lib/retscli/shell.rb, line 44
def split_line(line)
  CSV.parse_line(line, :col_sep => ' ')
end