lib/koma/cli.rb in koma-0.7.3 vs lib/koma/cli.rb in koma-0.8.0

- old
+ new

@@ -2,11 +2,11 @@ require 'json' require 'yaml' module Koma class CLI < Thor - desc 'ssh', 'stdout remote host inventory' + desc 'ssh <host1,host2,..>', 'stdout remote host inventory' option :key, type: :string, banner: '<key1,key2,..>', desc: 'inventory keys', aliases: :k @@ -27,11 +27,27 @@ Koma::HostInventory.disabled_keys.each do |key| option "with-#{key}", type: :boolean, desc: "enable #{key}" end - def ssh(host) + def ssh(host = nil) + if host.nil? + begin + stdin = timeout(5) do + $stdin.read + end + rescue Timeout::Error + STDERR.puts 'ERROR: "koma ssh" was called with no arguments' + STDERR.puts 'Usage: "koma ssh <host1,host2,..>"' + 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) if options[:yaml] puts YAML.dump(backend.gather) else puts JSON.pretty_generate(backend.gather) @@ -68,10 +84,10 @@ key += ' (disabled)' if Koma::HostInventory.disabled_keys.include?(key) puts key end end - option :version, type: :boolean, aliases: :v + option :version, type: :boolean, aliases: :V def help(version = nil) if version puts Koma::VERSION else super