module RHC::Commands

Public Class Methods

add(opts) click to toggle source
# File lib/rhc/commands.rb, line 143
def self.add(opts)
  commands[opts[:name]] = opts
end
deprecated!() click to toggle source
# File lib/rhc/commands.rb, line 150
def self.deprecated!
  instance = Commander::Runner.instance
  command_name = instance.command_name_from_args
  command = instance.active_command

  if new_cmd = command.deprecated(command_name)
    new_cmd = "rhc #{command.name}" if new_cmd == true
    RHC::Helpers.deprecated_command new_cmd
  end
end
global_option(*args, &block) click to toggle source
# File lib/rhc/commands.rb, line 146
def self.global_option(*args, &block)
  global_options << [args.freeze, block]
end
load() click to toggle source
# File lib/rhc/commands.rb, line 137
def self.load
  Dir[File.join(File.dirname(__FILE__), "commands", "*.rb")].each do |file|
    require file
  end
  self
end
needs_configuration!(cmd, options, config) click to toggle source
# File lib/rhc/commands.rb, line 161
def self.needs_configuration!(cmd, options, config)
  if not (cmd.class.suppress_wizard? or
          options.noprompt or
          options.help or
          config.has_local_config? or
          config.has_opts_config?)

    $stderr.puts RHC::Helpers.color("You have not yet configured the OpenShift client tools. Please run 'rhc setup'.", :yellow)
  end
end
to_commander(instance=Commander::Runner.instance) click to toggle source
# File lib/rhc/commands.rb, line 172
def self.to_commander(instance=Commander::Runner.instance)
  global_options.each do |args, block|
    args = args.dup
    opts = (args.pop if Hash === args.last) || {}
    option = instance.global_option(*args, &block).last
    option.merge!(opts)
  end
  commands.each_pair do |name, opts|
    name = Array(name)
    names = [name.reverse.join('-'), name.join(' ')] if name.length > 1
    name = name.join('-')

    instance.command name do |c|
      c.description = opts[:description]
      c.summary = opts[:summary]
      c.syntax = opts[:syntax]
      c.default_action = opts[:default]

      c.info = opts

      (options_metadata = Array(opts[:options])).each do |o|
        option_data = [o[:switches], o[:description]].flatten(1)
        c.option *option_data
        o[:arg] = Commander::Runner.switch_to_sym(Array(o[:switches]).last)
      end

      (args_metadata = Array(opts[:args])).each do |meta|
        switches = meta[:switches]
        unless switches.nil? or switches.empty?
          switches << meta[:description]
          c.option *switches
        end
      end

      Array(opts[:aliases]).each do |a|
        action = Array(a[:action])
        [' ', '-'].each do |s|
          cmd = action.join(s)
          instance.alias_command cmd, name
        end
      end

      if names
        names.each{ |alt| instance.alias_command alt, name }
      else
        c.root = true
      end

      c.when_called do |args, options|
        deprecated!

        config = c.instance_variable_get(:@config)

        cmd = opts[:class].new
        cmd.options = options
        cmd.config = config

        args = fill_arguments(cmd, options, args_metadata, options_metadata, args)
        needs_configuration!(cmd, options, config)
        execute(cmd, opts[:method], args)
      end
    end
  end
  self
end

Protected Class Methods

commands() click to toggle source
# File lib/rhc/commands.rb, line 313
def self.commands
  @commands ||= {}
end
execute(cmd, method, args) click to toggle source
# File lib/rhc/commands.rb, line 239
def self.execute(cmd, method, args)
  cmd.send(method, *args)
end
fill_arguments(cmd, options, args_metadata, options_metadata, args) click to toggle source
# File lib/rhc/commands.rb, line 243
def self.fill_arguments(cmd, options, args_metadata, options_metadata, args)
  Commander::Runner.instance.options.each do |opt|
    if opt[:context]
      arg = Commander::Runner.switch_to_sym(opt[:switches].last)
      options.__hash__[arg] ||= lambda{ cmd.send(opt[:context]) }
    end
  end

  # 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(", ") }
      RHC::Helpers.deprecated_option(incorrect, correct)
    end

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

  available = args.dup
  slots = Array.new(args_metadata.length)
  args_metadata.each_with_index do |arg, i|
    option = arg[:option_symbol]
    context_helper = arg[:context_helper]

    value = options.__hash__[option] if option

    if value.nil?
      value =
        if arg[:arg_type] == :list
          all = []
          while available.first && available.first != '--'
            all << available.shift
          end
          available.shift if available.first == '--'
          all
        else
          available.shift
        end
    end

    value = cmd.send(context_helper) if value.nil? and context_helper

    if value.nil?
      raise ArgumentError, "Missing required argument '#{arg[:name]}'." unless arg[:optional]
      break if available.empty?
    else
      value = Array(value) if arg[:arg_type] == :list
      slots[i] = value
      options.__hash__[option] = value if option
    end
  end

  raise ArgumentError, "Too many arguments passed in: #{available.reverse.join(" ")}" unless available.empty?

  slots
end
global_options() click to toggle source
# File lib/rhc/commands.rb, line 316
def self.global_options
  @options ||= []
end