Class: Qingcloud::Cli::App

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

Constant Summary collapse

COMMAND_MAP =
{
  'describe-instances' => 'Get the list of host computers.',
  'run-instances' => 'Create host computers.',
  'terminate-instances' => 'Destroy host computers.'
}

Instance Method Summary collapse

Constructor Details

#initialize(client, args = []) ⇒ App

Returns a new instance of App.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/qingcloud/cli/app.rb', line 10

def initialize(client, args=[])
  @client = client

  @mainopts = Optimist::options do
    synopsis "A Command Line Interface for QingCloud API."
    version "qingcloud v#{Qingcloud::Cli::VERSION}"
    
    banner "Usage:"
    banner "  qingcloud [options] [<command> [suboptions]]\n \n"
    banner "Options:"
    opt :version, "Print version and exit" 
    opt :help, "Show help message" 
    stop_on COMMAND_MAP.keys
    stop_on_unknown
    banner "\nCommands:"
    COMMAND_MAP.each { |cmd, desc| banner format("  %-20s %s", cmd, desc) }
  end

  @command = args.shift
  @subopts = nil
end

Instance Method Details

#call!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/qingcloud/cli/app.rb', line 32

def call!
  command_array = COMMAND_MAP.keys

  if @command.nil?
    puts "Error: must with options or command."
    puts "Try --help for help."
    return
  elsif command_array.include?(@command)
    @subopts = self.send("opt_#{@command.gsub("-", "_")}")

    ret = 
      Qingcloud::Api::Iaas.new(
        @client, 
        @command.split('-').map(&:capitalize).join, 
        @subopts.select { |k, v| !k.to_s.end_with?("_given") && (v.is_a?(Array) ? !v.empty? : v) }
      ).invoke
   
    puts "\n\nResponse: "
    ap ret.parsed_response
  elsif (sug = command_array.select { |x| x.start_with? @command }.compact) && !sug.empty? 
    puts sug
    return 
  else 
    puts "Error: unknown command #{@command}."
    puts "Try --help for help."
    return
  end
end