class Balmora::Command

Public Class Methods

new(state, command) click to toggle source
# File lib/balmora/command.rb, line 5
def initialize(state, command)
  @state = state

  @logger = state.logger
  @shell = state.shell
  @config = state.config
  @contexts = state.contexts
  @variables = state.variables
  @balmora = state.balmora

  @command = command
end

Public Instance Methods

_execute() click to toggle source
# File lib/balmora/command.rb, line 54
def _execute()
  if @command.instance_of?(::Hash) && !@contexts.check(@command[:context])
    @logger.debug("Skip: #{@command.inspect()}")
    return
  end

  init()

  if !@require.nil?()
    option(:require).each() { |file|
      require file
    }
  end

  @logger.debug("Run: #{@command.inspect()}")

  execute = self.method(:run)

  if !@sudo.nil?()
    execute_sudo = execute
    execute = Proc.new() {
      @shell.sudo!(option(:sudo)) {
        execute_sudo.call()
      }
    }
  end

  execute.call()
rescue => error
  @logger.error("#{error.inspect()}; failed to run " +
    "command: #{@command.inspect()}")

  if @fallback
    @logger.debug("Executing fallback for command #{@command.inspect()}")
    @balmora.execute(@fallback)
  end

  if option(:ignore_error) == true
    return
  end

  raise error
end
execute() click to toggle source
# File lib/balmora/command.rb, line 41
def execute()
  if @command.has_key?(:chdir)
    dir = @command[:chdir]
    dir = @variables.inject(dir)
    dir = @shell.expand(dir)
    Dir.chdir(dir) {
      _execute()
    }
  else
    _execute()
  end
end
init() click to toggle source
# File lib/balmora/command.rb, line 18
def init()
  if (@command.keys() - [:command] - options()).length != 0
    raise Error.new("Unknown options #{(@command.keys() - [:command] -
      options()).inspect()}")
  end

  options().each() { |key|
    if self.instance_variable_defined?(:"@#{key}")
      raise Error.new("Can not use #{key} as option")
    end

    option = @command.fetch(key, nil)
    self.instance_variable_set(:"@#{key}", option)
  }

  verify()
end
option(option) click to toggle source
# File lib/balmora/command.rb, line 102
def option(option)
  return @variables.inject(self.instance_variable_get(:"@#{option}"))
end
options() click to toggle source
# File lib/balmora/command.rb, line 36
def options()
  return [:ignore_error, :fallback, :extensions, :sudo, :context, :chdir,
    :require]
end
run() click to toggle source
# File lib/balmora/command.rb, line 98
def run()
  raise Error.new("Run should be implemented in subclass")
end