Class: SshShort::CLI

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

Class Method Summary collapse

Class Method Details

.add_options_if_present(node, args, options) ⇒ Object



49
50
51
52
53
54
# File 'lib/ssh_short/cli.rb', line 49

def self.add_options_if_present(node, args, options)
  options.each { |option|
    node[option] = args[option] if args.include?(option)
  }
  node
end

.run(argv) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ssh_short/cli.rb', line 15

def self.run(argv)
  config = SshShort::Parser.parse_config
  args = SshShort::Parser.parse_input(config, argv)
  key_set = SshShort::KeySet.new(config[:keys_dir])
  node_mapper = SshShort::NodeMapper.new

  if args[:action] == :list_aliases
    node_mapper.get_aliases.each { |node_alias| puts node_alias }
    exit
  end

  node = node_mapper.get_node args[:node]
  node ||= {:host => args[:node]}

  if node[:key].nil? or args[:force_key_prompt]
    node[:key] = key_set.prompt_for_key
  end
  node = CLI.add_options_if_present(node, args, [:alias, :user])

  node_mapper.update_node(node)
  key_path = key_set.get_key node[:key]

  case args[:action]
    when :connect
      Connection.connect node[:host], node[:user], key_path
    when :push
      Connection.push node[:host], node[:user], key_path, args[:source], args[:target]
    when :pull
      Connection.pull node[:host], node[:user], key_path, args[:source], args[:target]
    else
      abort 'Unknown action'
  end
end