class RHC::Commands::Base

Attributes

config[RW]
messages[R]
options[RW]

Public Class Methods

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

Protected Class Methods

alias_action(action, options={}) click to toggle source
# File lib/rhc/commands/base.rb, line 171
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 190
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

  option_symbol = Commander::Runner.switch_to_sym(switches.last)
  args_metadata << {:name => name,
                    :description => description,
                    :switches => switches,
                    :option_symbol => option_symbol,
                    :arg_type => arg_type}
end
default_action(action) click to toggle source
# File lib/rhc/commands/base.rb, line 203
def self.default_action(action)
  options[:default] = action unless action == :help
  define_method(:run) { |*args| send(action, *args) }
end
deprecated(msg) click to toggle source
# File lib/rhc/commands/base.rb, line 160
def self.deprecated(msg)
  options[:deprecated] = msg
end
description(*args) click to toggle source
# File lib/rhc/commands/base.rb, line 151
def self.description(*args)
  options[:description] = args.join(' ')
end
inherited(klass) click to toggle source
# File lib/rhc/commands/base.rb, line 119
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 124
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.gsub("_", "-")
  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 142
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 181
def self.option(switches, description, options={})
  options_metadata << {:switches => switches,
                       :description => description,
                       :context_helper => options[:context],
                       :required => options[:required],
                       :deprecated => options[:deprecated]
                      }
end
summary(value) click to toggle source
# File lib/rhc/commands/base.rb, line 154
def self.summary(value)
  options[:summary] = value
end
suppress_wizard() click to toggle source
# File lib/rhc/commands/base.rb, line 163
def self.suppress_wizard
  @suppress_wizard = true
end
suppress_wizard?() click to toggle source
# File lib/rhc/commands/base.rb, line 167
def self.suppress_wizard?
  @suppress_wizard
end
syntax(value) click to toggle source
# File lib/rhc/commands/base.rb, line 157
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 21
def validate_args_and_options(args_metadata, options_metadata, args)
  # process options
  options_metadata.each do |option_meta|
    arg = option_meta[:arg]

    # Check to see if we've provided a value for an option tagged as deprecated
    if (!(val = @options.__hash__[arg]).nil? && dep_info = option_meta[:deprecated])
      # Get the arg for the correct option and what the value should be
      (correct_arg, default) = dep_info.values_at(:key, :value)
      # Set the default value for the correct option to the passed value
      ## Note: If this isn't triggered, then the original default will be honored
      ## If the user specifies any value for the correct option, it will be used
      options.default correct_arg => default
      # Alert the users if they're using a deprecated option
      (correct, incorrect) = [options_metadata.find{|x| x[:arg] == correct_arg },option_meta].flatten.map{|x| x[:switches].join(", ") }
      deprecated_option(incorrect, correct)
    end

    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[:option_symbol]] unless arg_meta[:option_symbol].nil?
    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: #{fill_args.reverse.join(" ")}") unless fill_args.empty?

  arg_slots
end

Protected Instance Methods

application() click to toggle source
# File lib/rhc/commands/base.rb, line 82
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 76
def config
  @config ||= begin
    RHC::Config.new
  end
end
debug?() click to toggle source
# File lib/rhc/commands/base.rb, line 113
def debug?
  @options.debug
end
help(*args) click to toggle source
# File lib/rhc/commands/base.rb, line 109
def help(*args)
  raise ArgumentError, "Please specify an action to take"
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 96
def rest_client
  @rest_client ||= begin
    username = config.username
    unless username
      username = ask "Login to #{openshift_server}: "
      config.config_user(username)
    end
    config.password = config.password || RHC::get_password

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