Class: RunSSHLib::CLI

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

Constant Summary collapse

COMMAND =
%w(shell add del update print import export cpid scp)
MAIN_HELP =
"Usage: runssh [global_options] COMMAND [options] <path>\n\nA utility to bookmark multiple ssh connections in heirarchial order.\nFor a better understanding of host definitions and bookmarks, Read\nthe provided README.rdoc or go to http://github.com/babysnakes/runssh.\n\nCOMMAND : One of the commands mentioned below. It's possible to\n          type only part of the command as long as it's not ambiguous.\n<path>  : A space-separated list of names (e.g, one two three) that\n          leads to a host definition. For available completions\n          append \" ?\" to the end of path.\n\nAvailable commands:\n* shell  : Open ssh shell on remote host\n* scp    : Copy files from/to remote host\n* add    : Add host definition\n* del    : Delete host definition\n* update : Update host definition\n* print  : Print host definition\n* import : Import configuration\n* export : Export configuration\n* cpid   : Copy ssh public key to authorized_keys on remote host\n\nFor help on commands run:\nrunssh help COMMAND\n\nGlobal options:\n"

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Initialize new CLI instance and parse the supplied arguments.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/runsshlib/cli.rb', line 56

def initialize(args)
  args.unshift '-h' if args.empty?
  @global_options = parse_args(args)
  exit_with_help if args == ['help']
  return if @global_options[:update_config]

  # workaround to enable 'help COMMAND' functionality.
  if args.first == 'help'; args.shift; args << '-h'; end
  # indicate path completion request
  @completion_requested = args.delete('?') ? true : false # flag for required options
  @cmd = extract_subcommand(args)
  @options = parse_subcommand(@cmd, args)
  @c = init_config
  # path in ConfigFile uses symbols and not strings
  @path = args.map { |e| e.to_sym }
rescue ConfigError, InvalidSubCommandError, Errno::ENOENT => e
  Trollop.die e.message
rescue OlderConfigVersionError => e
  message = construct_update_config_message e.message
  HighLine.new.say(message)
  exit 1
end

Instance Method Details

#runObject

run



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/runsshlib/cli.rb', line 80

def run
  if @global_options[:update_config]
    run_update_config
  elsif @completion_requested
    puts @c.list_groups(@path)
  else
    command_name = 'run_' + @cmd
    m = method(command_name.to_sym)
    m.call(@path)
  end
rescue ConfigError, ParametersError => e
  Trollop.die e.message
rescue AbortError => e
  abort e.message
end