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 =
<<-EOS
Usage: runssh [global_options] COMMAND [options] <path>

A utility to bookmark multiple ssh connections in heirarchial order.
For a better understanding of host definitions and bookmarks, Read
the provided README.rdoc or go to http://github.com/babysnakes/runssh.

COMMAND : One of the commands mentioned below. It's possible to
          type only part of the command as long as it's not ambiguous.
<path>  : A space-separated list of names (e.g, one two three) that
          leads to a host definition. For available completions
          append " ?" to the end of path.

Available commands:
* shell  : Open ssh shell on remote host
* scp    : Copy files from/to remote host
* add    : Add host definition
* del    : Delete host definition
* update : Update host definition
* print  : Print host definition
* import : Import configuration
* export : Export configuration
* cpid   : Copy ssh public key to authorized_keys on remote host

For help on commands run:
runssh help COMMAND

Global options:
EOS

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