class RHC::Commands::Base

Attributes

config[RW]
options[RW]

Public Class Methods

new(options=Commander::Command::Options.new, config=nil) click to toggle source
# File lib/rhc/commands/base.rb, line 14
def initialize(options=Commander::Command::Options.new,
               config=nil)
  @options, @config = options, config

  # apply timeout here even though it isn't quite a global
  $rest_timeout = @options.timeout ? @options.timeout.to_i : nil
end

Protected Class Methods

alias_action(action, options={}) click to toggle source
# File lib/rhc/commands/base.rb, line 148
def self.alias_action(action, options={})
  # if it is a root_command we simply alias it to the passed in action
  # if not we prepend the current resource to the action
  # default == false
  options[:root_command] ||= false
  options[:action] = action
  options[:deprecated] ||= false
  aliases << options
end
argument(name, description, switches, options={}) click to toggle source
# File lib/rhc/commands/base.rb, line 166
def self.argument(name, description, switches, options={})
  arg_type = options[:arg_type]
  raise ArgumentError("Only the last argument descriptor for an action can be a list") if arg_type == :list and list_argument_defined?
  list_argument_defined true if arg_type == :list

  args_metadata << {:name => name, :description => description, :switches => switches, :arg_type => arg_type}
end
default_action(action) click to toggle source
# File lib/rhc/commands/base.rb, line 174
def self.default_action(action)
  define_method(:run) { |*args| send(action, *args) }
end
description(value) click to toggle source
# File lib/rhc/commands/base.rb, line 130
def self.description(value)
  options[:description] = value
end
inherited(klass) click to toggle source
# File lib/rhc/commands/base.rb, line 99
def self.inherited(klass)
  unless klass == RHC::Commands::Base
  end
end
method_added(method) click to toggle source
# File lib/rhc/commands/base.rb, line 104
def self.method_added(method)
  return if self == RHC::Commands::Base
  return if private_method_defined? method
  return if protected_method_defined? method

  method_name = method.to_s == 'run' ? nil : method.to_s
  name = [method_name]
  name.unshift(self.object_name).compact!
  raise InvalidCommand, "Either object_name must be set or a non default method defined" if name.empty?
  RHC::Commands.add((@options || {}).merge({
    :name => name.join(' '),
    :class => self,
    :method => method
  }));
  @options = nil
end
object_name(value=nil) click to toggle source
# File lib/rhc/commands/base.rb, line 121
def self.object_name(value=nil)
  @object_name ||= begin
      value ||= if self.name && !self.name.empty?
        self.name.split('::').last
      end
      value.to_s.split(%r(?=[A-Z])/).join('-').downcase if value
    end
end
option(switches, description, options={}) click to toggle source
# File lib/rhc/commands/base.rb, line 158
def self.option(switches, description, options={})
  options_metadata << {:switches => switches,
                       :description => description,
                       :context_helper => options[:context],
                       :required => options[:required]
                      }
end
summary(value) click to toggle source
# File lib/rhc/commands/base.rb, line 133
def self.summary(value)
  options[:summary] = value
end
suppress_wizard() click to toggle source
# File lib/rhc/commands/base.rb, line 140
def self.suppress_wizard
  @suppress_wizard = true
end
suppress_wizard?() click to toggle source
# File lib/rhc/commands/base.rb, line 144
def self.suppress_wizard?
  @suppress_wizard
end
syntax(value) click to toggle source
# File lib/rhc/commands/base.rb, line 136
def self.syntax(value)
  options[:syntax] = value
end

Public Instance Methods

validate_args_and_options(args_metadata, options_metadata, args) click to toggle source
# File lib/rhc/commands/base.rb, line 22
def validate_args_and_options(args_metadata, options_metadata, args)
  # process options
  options_metadata.each do |option_meta|
    arg = option_meta[:arg]

    context_helper = option_meta[:context_helper]
    @options.__hash__[arg] = self.send(context_helper) if @options.__hash__[arg].nil? and context_helper
    raise ArgumentError.new("Missing required option '#{arg}'.") if option_meta[:required] and @options.__hash__[arg].nil?
  end

  # process args
  arg_slots = [].fill(nil, 0, args_metadata.length)
  fill_args = args.reverse
  args_metadata.each_with_index do |arg_meta, i|
    # check options
    value = @options.__hash__[arg_meta[:name]]
    if value
      arg_slots[i] = value
    elsif arg_meta[:arg_type] == :list
      arg_slots[i] = fill_args.reverse
      fill_args = []
    else
      raise ArgumentError.new("Missing required argument '#{arg_meta[:name]}'.") if fill_args.empty?
      arg_slots[i] = fill_args.pop
    end
  end

  raise ArgumentError.new("Too many arguments passed in.") unless fill_args.empty?

  arg_slots
end

Protected Instance Methods

application() click to toggle source
# File lib/rhc/commands/base.rb, line 69
def application
  #@application ||= ... identify current application or throw,
  #                     indicating one is needed.  Should check
  #                     options (commands which have it as an ARG
  #                     should set it onto options), then check
  #                     current git repo for remote, fail.
end
config() click to toggle source

The implicit config object provides no defaults.

# File lib/rhc/commands/base.rb, line 63
def config
  @config ||= begin
    RHC::Config.new
  end
end
rest_client() click to toggle source

Return a client object capable of making calls to the OpenShift API that transforms intent and options, to remote calls, and then handle the output (or failures) into exceptions and formatted object output. Most interactions should be through this call pattern.

# File lib/rhc/commands/base.rb, line 83
def rest_client
  @rest_client ||= begin
    username = config.username
    unless username
      username = ask "To connect to #{openshift_server} enter your OpenShift login (email or Red Hat login id): "
      config.config_user(username)
    end
    password = RHC::Config.password || RHC::get_password

    RHC::Rest::Client.new(openshift_rest_node, username, password, @options.debug)
  end
end