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.
-
.agency_command?(cmd) ⇒ Boolean
Is the command an agency command?.
-
.agency_path ⇒ Object
Return the agency command base path.
-
.base_path ⇒ Object
Return the command base path.
-
.command_path(command) ⇒ Object
Return the full path of the ruby class.
-
.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.
-
.smithctl_command?(cmd) ⇒ Boolean
Is the command a smithctl command?.
-
.smithctl_path ⇒ Object
Return the smithctl command base path.
- .to_command_name(path) ⇒ Object
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 |
.agency_command?(cmd) ⇒ Boolean
Is the command an agency command?
97 98 99 |
# File 'lib/smith/command.rb', line 97 def self.agency_command?(cmd) agency_path.join(cmd).sub_ext('.rb').exist? end |
.agency_path ⇒ Object
Return the agency command base path.
107 108 109 |
# File 'lib/smith/command.rb', line 107 def self.agency_path base_path.join('agency') end |
.base_path ⇒ Object
Return the command base path.
121 122 123 |
# File 'lib/smith/command.rb', line 121 def self.base_path @c64a6f4f ||= Smith.root_path.join('lib').join("smith").join('commands') end |
.command_path(command) ⇒ Object
Return the full path of the ruby class.
92 93 94 |
# File 'lib/smith/command.rb', line 92 def self.command_path(command) send("#{command_type(command)}_path").join(command) 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 " instance_variable_set(:\"@\#{k}\", v)\n def \#{k}=(z); @\#{k} = z; end\n def \#{k}; @\#{k}; end\n EOM\n end\n\n clazz.execute\n\n rescue Trollop::CommandlineError => e\n vars[:responder].succeed(clazz.format_help(:prefix => \"Error: \#{e.message}.\\n\"))\n rescue Trollop::HelpNeeded\n vars[:responder].succeed(clazz.format_help)\n end\nend\n", __FILE__, __LINE__ + 1 |
.smithctl_command?(cmd) ⇒ Boolean
Is the command a smithctl command?
102 103 104 |
# File 'lib/smith/command.rb', line 102 def self.smithctl_command?(cmd) smithctl_path.join(cmd).sub_ext('.rb').exist? end |
.smithctl_path ⇒ Object
Return the smithctl command base path.
112 113 114 |
# File 'lib/smith/command.rb', line 112 def self.smithctl_path base_path.join('smithctl') end |
.to_command_name(path) ⇒ Object
116 117 118 |
# File 'lib/smith/command.rb', line 116 def self.to_command_name(path) path.basename(".rb").to_s end |