Permalink
Browse files

commented lots of code

its a good start but would be great with more detail
  • Loading branch information...
1 parent 7c02796 commit ceb6679ba8b073dd9782065ee51eaa88cb4caaea @madwire committed Apr 16, 2012
Showing with 35 additions and 3 deletions.
  1. +7 −2 lib/trooper/action.rb
  2. +7 −0 lib/trooper/arsenal.rb
  3. +12 −1 lib/trooper/configuration.rb
  4. +9 −0 lib/trooper/runner.rb
View
@@ -11,33 +11,38 @@ class Action
attr_reader :name, :description, :config, :block
attr_accessor :commands
+ # expects a name, description and a block
+ # Action.new(:my_action, 'Does great things') { run 'touch file' }
def initialize(name, description, &block)
@name, @description, @config = name, description, {}
@commands, @block = [], block
end
+ # eval's the block passed on initialize and returns the command array
def call(configuration)
@config = configuration
eval_block(&block)
commands
end
alias :execute :call
+ # validates the action
def ok?
true
end
+ # run is the base run command used by the dsl
def run(command)
commands << command if command != ''
end
- def method_missing(method_sym, *arguments, &block)
+ def method_missing(method_sym, *arguments, &block) # :nodoc:
config[method_sym] || super
end
private
- def eval_block(&block)
+ def eval_block(&block) # :nodoc:
if block_given?
if block.arity == 1
block.call self
View
@@ -2,11 +2,13 @@ module Trooper
class Arsenal < Array
+ # find an arsenal by its name
def find_by_name(name)
detect { |weapon| weapon.name == name }
end
alias :[] :find_by_name
+ # add a weapon to the arsenal
def add(weapon)
if weapon.ok?
remove weapon.name
@@ -15,24 +17,29 @@ def add(weapon)
weapon
end
+ # remove a weapon from the arsenal
def remove(name)
self.delete_if {|w| w.name == name}
end
+ # clears arsenal
def clear!
self.clear
end
class << self
+ # returns the strategies arsenal
def strategies
@strategies ||= new
end
+ # returns the strategies arsenal
def actions
@actions ||= new
end
+ # clears all default arsenal
def reset!
@strategies, @actions = nil
end
@@ -26,6 +26,8 @@ def self.init
end
end
+ # initialize a new configuration object, will parse the given troopfile.
+ # Configuration.new({:my_override => 'settings'})
def initialize(options = {})
@loaded = false
@file_name = options[:file_name] || FILE_NAME
@@ -35,24 +37,31 @@ def initialize(options = {})
load_troopfile! options
end
+ # returns a terminal friendly version of the configuration
def to_s
self.map {|k,v| "#{k}: #{v}" }.join("\n")
end
+ # will find and execute the strategy name passed
+ # @config.execute(:my_strategy_name)
def execute(strategy_name)
Arsenal.strategies[strategy_name].execute self
end
+ # a way to set variables that will be available to all actions
+ # set(:my_variable => 'sdsd') # => available as method in an action
def set(hash)
self.merge! hash
end
+ # returns true if the troopfile was loaded
def loaded?
@loaded
end
private
+ # loads the troopfile and sets the environment up
def load_troopfile!(options)
if troopfile?
eval troopfile.read
@@ -65,10 +74,12 @@ def load_troopfile!(options)
end
end
+ # returns the troopfile file object
def troopfile
File.open(@file_name)
end
-
+
+ # returns true if the troopfile exists
def troopfile?
File.exists?(@file_name)
end
View
@@ -7,16 +7,22 @@ class Runner
attr_reader :host, :user, :connection
+ # pass the host, user and any net/ssh config options
+ # Runner.new('my.example.com', 'admin', :forward_agent => false)
def initialize(host, user, options = { :forward_agent => true })
@host = host
@user = user
@connection = Net::SSH.start(host, user, options)
end
+ # returns user@host as a string
def to_s
"#{user}@#{host}"
end
+ # execute a set of commands via net/ssh, returns and array or raises an exception
+ # runner.execute(['cd to/path', 'touch file']) # => ['cd to/path && touch file', :stdout, '']
+ # runner.execute('cat file') # => ['cat file', :stdout, 'file content']
def execute(command, options = {})
commands = parse command
Trooper.logger.debug commands
@@ -27,12 +33,15 @@ def execute(command, options = {})
end
end
+ # close net/ssh connection
def close
connection.close
end
private
+ # parse command, expects a string or array
+ # parse(['cd to/path', 'touch file']) # => 'cd to/path && touch file'
def parse(command)
case command.class.name.downcase.to_sym #Array => :array
when :array

0 comments on commit ceb6679

Please sign in to comment.