class Smith::Command

Public Class Methods

agency?() click to toggle source

Check to see if the command is an agency or smithctl command.

# File lib/smith/command.rb, line 84
def self.agency?
  Smith.constants.include?(:Agency)
end
command_type(command) click to toggle source

What type of command is it?

# File lib/smith/command.rb, line 55
def self.command_type(command)
  case
  when agency_command?(command)
    :agency
  when smithctl_command?(command)
    :smithctl
  else
    raise UnknownCommandError, "Unknown command: #{command}"
  end
end
commands(type=:all) click to toggle source
# File lib/smith/command.rb, line 66
def self.commands(type=:all)
  types = case type
  when :all
    ['agency', 'smithctl']
  when :agency
    types = ['agency']
  when :smithctl
    types = ['smithctl']
  else
    raise ArgumentError, "Unknown command type"
  end

  types.map do |type|
    Pathname.glob(base_path.join(type).join("**/*.rb"))
  end.flatten.map {|p| to_command_name(p) }
end
instantiate(command) click to toggle source
# File lib/smith/command.rb, line 50
def self.instantiate(command)
  Commands.const_get(Extlib::Inflection.camelize(command)).new
end
load_command(command) click to toggle source

Determine whether the command is an agency or smithctl command and load accordingly.

# File lib/smith/command.rb, line 46
def self.load_command(command)
  require command_path(command)
end
run(command, args, vars) click to toggle source

Load and run the command specified. This method takes: target which is either a list of agents or the agency vars variables to be passed in to the command. This takes the form of a hash and accessor methods are generated named after the key of the hash.

# File lib/smith/command.rb, line 17
    def self.run(command, args, vars)
      # Change _ to - underscores look so ugly as a command name.
      command = command.gsub(/-/, '_')
      load_command(command)

      clazz = Commands.const_get(Extlib::Inflection.camelize(command)).new

      begin
        clazz.parse_options(args)

        vars.each do |k,v|
          clazz.instance_eval <<-EOM, __FILE__, __LINE__ + 1
            instance_variable_set(:"@#{k}", v)
            def #{k}=(z); @#{k} = z; end
            def #{k}; @#{k}; end
          EOM
        end

        clazz.execute

      rescue Trollop::CommandlineError => e
        vars[:responder].succeed(clazz.format_help(:prefix => "Error: #{e.message}.\n"))
      rescue Trollop::HelpNeeded
        vars[:responder].succeed(clazz.format_help)
      end
    end

Private Class Methods

agency_command?(cmd) click to toggle source

Is the command an agency command?

# File lib/smith/command.rb, line 96
def self.agency_command?(cmd)
 agency_path.join(cmd).sub_ext('.rb').exist?
end
agency_path() click to toggle source

Return the agency command base path.

# File lib/smith/command.rb, line 106
def self.agency_path
  base_path.join('agency')
end
base_path() click to toggle source

Return the command base path.

# File lib/smith/command.rb, line 120
def self.base_path
  @c64a6f4f ||= Smith.root_path.join('lib').join("smith").join('commands')
end
command_path(command) click to toggle source

Return the full path of the ruby class.

# File lib/smith/command.rb, line 91
def self.command_path(command)
  send("#{command_type(command)}_path").join(command)
end
smithctl_command?(cmd) click to toggle source

Is the command a smithctl command?

# File lib/smith/command.rb, line 101
def self.smithctl_command?(cmd)
  smithctl_path.join(cmd).sub_ext('.rb').exist?
end
smithctl_path() click to toggle source

Return the smithctl command base path.

# File lib/smith/command.rb, line 111
def self.smithctl_path
  base_path.join('smithctl')
end
to_command_name(path) click to toggle source
# File lib/smith/command.rb, line 115
def self.to_command_name(path)
  path.basename(".rb").to_s
end