AndreyAntipov/ssh-bookmarks-cli

View on GitHub
bin/ssh-bookmarks

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# encoding: UTF-8

# resolve dependencies
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')

# dead simple ssh connection manager
class Application
  require 'highline/import'
  require 'ssh/bookmarks'

  # CLI interface for SSH manager
  class Interface
    def initialize
      config = SSH::Bookmarks::Config.new
      show_menu config.data, 'host'

      # ignore errors when use ctrl-c or ctrl-d
    rescue SystemExit, Interrupt, EOFError
      puts "\nexiting..."
    end

    # show menu
    # @param list [Array] all paramerers from config
    # @param title [String] determine which parameter will be used for title
    def show_menu(list, title)
      t = title.to_sym
      proposal = 'Please choose the server which you would like to connect?'
      choose do |menu|
        menu.prompt = proposal
        list.each do |item|
          unless item[:options][:hidden]
            menu.choice(item[t]) { SSH.connect item[t] }
          end
        end
      end
    end
  end
  Interface.new
end