Class: Profitbricks::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout = $stdout) ⇒ CLI

Returns a new instance of CLI.



8
9
10
11
# File 'lib/profitbricks/cli.rb', line 8

def initialize(stdout = $stdout)
  @options = {:debug => false}
  @stdout = stdout
end

Instance Attribute Details

#stdoutObject

Returns the value of attribute stdout.



6
7
8
# File 'lib/profitbricks/cli.rb', line 6

def stdout
  @stdout
end

Instance Method Details

#call_instance_method(klass, method, arguments) ⇒ Object



90
91
92
93
94
# File 'lib/profitbricks/cli.rb', line 90

def call_instance_method(klass, method, arguments)
  id = arguments.delete(:id)
  obj = klass.send(:find, {:id => id})
  call_method(obj, method, arguments)
end

#call_method(klass, method, arguments) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/profitbricks/cli.rb', line 96

def call_method(klass, method, arguments)
   if arguments.length > 0
     klass.send(method, arguments)
   else
     klass.send(method)
  end
end

#call_singleton_method(klass, method, arguments) ⇒ Object



86
87
88
# File 'lib/profitbricks/cli.rb', line 86

def call_singleton_method(klass, method, arguments)
  call_method(klass, method, arguments)
end

#convert_arguments(options) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/profitbricks/cli.rb', line 62

def convert_arguments(options)
  arguments = Hash[options[2..-1].collect { |x| 
    a = x.split('=');
    a[1] = a[1].to_i if a[1] =~ /^\d+$/
    [a[0].to_sym, a[1]]  
  }]
  [options[0], options[1], arguments]
end

#get_instance_method(klass, method) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/profitbricks/cli.rb', line 78

def get_instance_method(klass, method)
  obj = klass.send(:new, {})
  methods = obj.public_methods(false)
  if methods.collect { |m| m.to_sym }.include?(method.to_sym)
    return obj.method(method)
  end
end

#get_singleton_method(klass, method) ⇒ Object



71
72
73
74
75
76
# File 'lib/profitbricks/cli.rb', line 71

def get_singleton_method(klass, method)
  methods = klass.singleton_methods(false)
  if methods.collect { |m| m.to_sym }.include?(method.to_sym)
    return klass.method(method.to_sym)
  end
end

#run(options) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/profitbricks/cli.rb', line 13

def run(options)
  options = OptionParser.new do |opts|
    opts.banner = "Usage: profitbricks [options] <class> <method> argument=value argument2=value2 .."
    opts.separator ""
    opts.separator "You have to supply your Profitbricks user name and password in the environmental variables PROFITBRICKS_USER and PROFITBRICKS_PASSWORD"
    opts.separator ""
    opts.on("-d", "--debug", "Enable debugging output") do |d|
      @options[:debug] = d
    end
    opts.on_tail("-h", "--help", "Show this message") do
      @stdout.puts opts
      return -1
    end
    if !ENV['PROFITBRICKS_USER'] or !ENV['PROFITBRICKS_PASSWORD'] or options.length < 2
      @stdout.puts opts
      return -1
    end

  end.parse!(options)

  Profitbricks.configure do |config|
    config.username = ENV['PROFITBRICKS_USER']
    config.password = ENV['PROFITBRICKS_PASSWORD']
    config.log = @options[:debug]
  end
  
  (klass, m, arguments) = convert_arguments(options)

  begin
    klass = Profitbricks.get_class(klass)
  rescue LoadError
    @stdout.puts "Invalid class name #{klass}."
    return -1
  end
  
  if method = get_singleton_method(klass, m)
    dump = PP.pp(call_singleton_method(klass, m, arguments), "")
    @stdout.puts dump
  elsif method = get_instance_method(klass, m)
    dump = PP.pp(call_instance_method(klass, m, arguments), "")
    @stdout.puts dump
  else
    @stdout.puts "#{klass} has no method #{m}"
    return -1
  end
  return 0
end