Class: Xlogin::APIClient::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(args = ARGV) ⇒ Object



38
39
40
# File 'lib/xlogin/apiclient/cli.rb', line 38

def self.run(args = ARGV)
  Xlogin::APIClient::CLI.new.run(args)
end

Instance Method Details

#run(args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/xlogin/apiclient/cli.rb', line 42

def run(args)
  config = {api: XLOGIN_API_URL}
  parser = OptionParser.new
  parser.banner = "#{File.basename($0)} HOST [Options]"
  parser.version = Xlogin::APIClient::VERSION

  parser.on('-a API',  '--api',       String, 'The Xlogin API URL.')        { |v| config[:api]  = v }
  parser.on('-t TYPE', '--type',      String, 'The TYPE of the device.')    { |v| config[:type] = v }
  parser.on('-i PATH', '--inventory', String, 'The PATH to the inventory file..') { |v| config[:inventory]  = v }
  parser.on('-e COMMAND', '--exec',   String, 'Execute commands and quit.') { |v| config[:exec] = v }

  args = parser.parse!(args)
  host = args.shift
  hostinfo = if config[:inventory]
               factory = Factory.instance
               factory.instance_eval(IO.read(config[:inventory]))
               factory.get_hostinfo(host) || {}
             else
               {type: config[:type], uri: host}
             end

  raise "Argument error - type='#{hostinfo[:type]}' uri='#{hostinfo[:uri]}'" unless hostinfo[:type] && hostinfo[:uri]

  APIClient.base_url = config[:api]
  client = APIClient.new(**hostinfo)
  client.cmd(config[:exec]) { |c| $stdout.print c }
rescue => e
  $stderr.puts e, '', parser
end