Sha256: d3122b16ba5605cbcd615f02324f59e6e22206e928b3f03c955a0cd2015dedfc

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

require 'logger'
require 'singleton'
require 'paint'

module RCoLi
  
  class ApplicationContext
    
    include Singleton
    
    attr_accessor :debug
    
  end
  
  class Log
    
    include Singleton
    
    def initialize
      @log = Logger.new(STDOUT)
      @log.level = Logger::INFO
      @log.formatter = proc do |severity, datetime, progname, msg|
        case severity
        when "DEBUG"
          color = 'gray27'
        else
          color = :white
        end
        
        if STDOUT.tty?
          Paint["#{msg}\n", color]
        else
          "#{msg}\n"
        end
        
      end
    end
    
    def logger
      @log
    end
    
  end
  
  class SystemExecutor
    
    include Singleton
    
    def initialize
    end
    
    def register(file)
      @source = file
      log.debug("Loading commands from file #{file}")
      @commands = YAML::load(File.open(file))
    end
    
    def execute(command, *args)
      cmnd = @commands[command.to_s]
      if cmnd
        cmnd.scan(/\$\{([^\s]+)\}/).each do |s|
          context = args[0]
          (s[0].split('.').each{|key| context = (context.is_a? Hash) ? context[key] : nil})
          cmnd = cmnd.sub("${#{s[0]}}", context) if context if context
        end
        log.debug("EXEC: #{cmnd}")
        system(cmnd)
      else
        raise ApplicationError, "The command #{command} isn't configured. Check the file #{@source}"
      end
    end
    
  end
  
end

def log
  RCoLi::Log.instance.logger
end

def sysexec(command, *args)
  RCoLi::SystemExecutor.instance.execute(command, args[0])
end

def load_commands(file)
  RCoLi::SystemExecutor.instance.register(file)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rcoli-0.5.9 lib/rcoli/utils.rb