Class: Koma::CLI

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/koma/cli.rb', line 118

def method_missing(command)
  message = "\n  ((     ))\n((  _____  ))\n(U  \u25CF   \u25CF  U)\n  ((  \u25CF  ))  < Could not find command \"\#{command}\".\n\n"
  puts message
end

Instance Method Details

#execObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/koma/cli.rb', line 90

def exec
  backend = Koma::Backend::Exec.new(nil, options)
  gathered = backend.gather
  if options[:yaml]
    puts YAML.dump(gathered)
  elsif options[:csv]
    puts csv_dump(host, gathered)
  else
    puts JSON.pretty_generate(gathered)
  end
end

#help(command = nil) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/koma/cli.rb', line 110

def help(command = nil)
  if options[:version]
    puts Koma::VERSION
  else
    super
  end
end

#keysObject



103
104
105
106
107
108
# File 'lib/koma/cli.rb', line 103

def keys
  Koma::HostInventory.all_inventory_keys.each do |key|
    key += ' (disabled)' if Koma::HostInventory.disabled_keys.include?(key)
    puts key
  end
end

#run_command(host = nil, *commands) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/koma/cli.rb', line 48

def run_command(host = nil, *commands)
  if host.nil?
    STDERR.puts 'ERROR: "koma run-command" was called with no arguments'
    STDERR.puts 'Usage: "koma run-command <host1,host2,..> <command>"'
    return
  end
  if commands.empty?
    commands = [host]
    begin
      stdin = timeout(1) { $stdin.read }
    rescue Timeout::Error
    end
    if stdin.nil?
      STDERR.puts 'ERROR: "koma run-commands" was called with no arguments'
      STDERR.puts 'Usage: "koma run-commands <host1,host2,..> <commands>"'
      return
    end
    ret = stdin.split("\n").select { |line| line =~ /^Host ([^\s\*]+)/ }.map do |line|
      line =~ /^Host ([^\s]+)/
      Regexp.last_match[1]
    end
    host = ret.join(',')
  end
  backend = Koma::Backend::Ssh.new(host, options)
  backend.stdin = stdin if stdin
  gathered = backend.run_commands(commands)
  if options[:yaml]
    puts YAML.dump(gathered)
  elsif options[:csv]
    puts csv_dump(host, gathered)
  else
    puts JSON.pretty_generate(gathered)
  end
end

#ssh(host = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/koma/cli.rb', line 19

def ssh(host = nil)
  if host.nil?
    begin
      stdin = timeout(1) { $stdin.read }
    rescue Timeout::Error
    end
    ret = stdin.split("\n").select { |line| line =~ /^Host ([^\s\*]+)/ }.map do |line|
      line =~ /^Host ([^\s]+)/
      Regexp.last_match[1]
    end
    host = ret.join(',')
  end
  backend = Koma::Backend::Ssh.new(host, options)
  backend.stdin = stdin if stdin
  gathered = backend.gather
  if options[:yaml]
    puts YAML.dump(gathered)
  elsif options[:csv]
    puts csv_dump(host, gathered)
  else
    puts JSON.pretty_generate(gathered)
  end
end