Class: Profitbricks::CLI
- Inherits:
-
Object
- Object
- Profitbricks::CLI
- Defined in:
- lib/profitbricks/cli.rb
Instance Attribute Summary (collapse)
-
- (Object) stdout
Returns the value of attribute stdout.
Instance Method Summary (collapse)
- - (Object) call_instance_method(klass, method, arguments)
- - (Object) call_method(klass, method, arguments)
- - (Object) call_singleton_method(klass, method, arguments)
- - (Object) convert_arguments(options)
- - (Object) get_instance_method(klass, method)
- - (Object) get_singleton_method(klass, method)
-
- (CLI) initialize(stdout = $stdout)
constructor
A new instance of CLI.
- - (Object) run(options)
Constructor Details
- (CLI) initialize(stdout = $stdout)
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
- (Object) stdout
Returns the value of attribute stdout
6 7 8 |
# File 'lib/profitbricks/cli.rb', line 6 def stdout @stdout end |
Instance Method Details
- (Object) call_instance_method(klass, method, arguments)
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 |
- (Object) call_method(klass, method, arguments)
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 |
- (Object) call_singleton_method(klass, method, arguments)
86 87 88 |
# File 'lib/profitbricks/cli.rb', line 86 def call_singleton_method(klass, method, arguments) call_method(klass, method, arguments) end |
- (Object) convert_arguments(options)
62 63 64 65 66 67 68 69 |
# File 'lib/profitbricks/cli.rb', line 62 def convert_arguments() arguments = Hash[[2..-1].collect { |x| a = x.split('='); a[1] = a[1].to_i if a[1] =~ /^\d+$/ [a[0].to_sym, a[1]] }] [[0], [1], arguments] end |
- (Object) get_instance_method(klass, method)
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 |
- (Object) get_singleton_method(klass, method)
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 |
- (Object) run(options)
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() = OptionParser.new do |opts| opts. = "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 .length < 2 @stdout.puts opts return -1 end end.parse!() Profitbricks.configure do |config| config.username = ENV['PROFITBRICKS_USER'] config.password = ENV['PROFITBRICKS_PASSWORD'] config.log = @options[:debug] end (klass, m, arguments) = convert_arguments() 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 |