Module: Addons::CLI

Extended by:
CLI
Included in:
CLI
Defined in:
lib/addons-client/cli.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



48
49
50
# File 'lib/addons-client/cli.rb', line 48

def client
  @client ||= Addons::Client
end

#load_settings!Object



56
57
58
59
60
61
# File 'lib/addons-client/cli.rb', line 56

def load_settings!
  Settings.use :commandline
  Settings.resolve!
rescue RuntimeError => e
  raise Addons::UserError.new(e)
end

#puts(string) ⇒ Object



52
53
54
# File 'lib/addons-client/cli.rb', line 52

def puts(string)
  STDOUT.puts(string)
end

#run!Object



5
6
7
8
# File 'lib/addons-client/cli.rb', line 5

def run!
  load_settings!
  run_command!
end

#run_command!Object



10
11
12
13
14
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
# File 'lib/addons-client/cli.rb', line 10

def run_command!
  command = Settings.rest.first
  case command
  when /list/i
    response = client.list(Settings.rest[1..-1].join(" "))
    response.data.each do |addon_plan|
      puts "#{addon_plan["name"]} $%0.2f/#{addon_plan["price_unit"]}" % (BigDecimal.new(addon_plan["price_cents"].to_s) / 100)
    end
  when /deprovision/i
    resource_id = Settings.rest[1]
    raise Addons::UserError, "Must supply resource id" unless resource_id
    response = client.deprovision!(resource_id)
    puts "Deprovisioned #{resource_id}"
    puts response
  when /provision/i
    slug = Settings.rest[1]
    raise Addons::UserError, "Must supply add-on:plan" unless slug
    response = client.provision!(slug, :options => Settings[:options],
                                       :consumer_id => Settings[:consumer_id])
    puts "Provisioned #{slug}"
    puts response
  when /plan-change/i
    resource_id = Settings.rest[1]
    raise Addons::UserError, "Must supply resource id" unless resource_id
    plan = Settings.rest[2]
    raise Addons::UserError, "Must supply plan after resource id" unless plan
    response = client.plan_change!(resource_id, plan)
    puts "Plan Changed to #{plan}"
    puts response
  else
    if command
      puts "#{command} is not a valid command"
    else
      puts "Command must be one of: provision, deprovision, plan-change"
    end
  end
end