Class: Smith::Command
Direct Known Subclasses
Defined Under Namespace
Classes: UnknownCommandError
Class Method Summary collapse
-
.agency? ⇒ Boolean
Check to see if the command is an agency or smithctl command.
-
.command_type(command) ⇒ Object
What type of command is it?.
- .commands(type = :all) ⇒ Object
- .instantiate(command) ⇒ Object
-
.load_command(command) ⇒ Object
Determine whether the command is an agency or smithctl command and load accordingly.
-
.run(command, args, vars) ⇒ Object
Load and run the command specified.
Methods included from Logger
Class Method Details
.agency? ⇒ Boolean
Check to see if the command is an agency or smithctl command.
85 86 87 |
# File 'lib/smith/command.rb', line 85 def self.agency? Smith.constants.include?(:Agency) end |
.command_type(command) ⇒ Object
What type of command is it?
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/smith/command.rb', line 56 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) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/smith/command.rb', line 67 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) ⇒ Object
51 52 53 |
# File 'lib/smith/command.rb', line 51 def self.instantiate(command) Commands.const_get(Extlib::Inflection.camelize(command)).new end |
.load_command(command) ⇒ Object
Determine whether the command is an agency or smithctl command and load accordingly.
47 48 49 |
# File 'lib/smith/command.rb', line 47 def self.load_command(command) require command_path(command) end |
.run(command, args, vars) ⇒ Object
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.
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 |
# File 'lib/smith/command.rb', line 18 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.(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.}.\n")) rescue Trollop::HelpNeeded vars[:responder].succeed(clazz.format_help) end end |