Sha256: dd7974fed4f8b314c8bb44312a0acdd9d35f93f751d39700c2853ed32791dd87

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

#!/usr/bin/ruby -w
# -*- ruby -*-

require 'logue/loggable'
require 'open3'
require 'command/cacheable/cachefile'

module Command
  module Cacheable
  end
end

module Command::Cacheable
  # A command line executable, by default not caching.
  class Command
    include Logue::Loggable

    CACHEDIR = "/tmp/cmdcache"

    attr_reader :args
    attr_reader :output
    attr_reader :error
    attr_reader :status

    def initialize *args, debug: false, caching: false, cachedir: nil
      @args     = args.dup
      @caching  = caching
      @cachedir = cachedir || CACHEDIR
    end

    def << arg
      @args << arg
    end

    def execute
      if @caching
        read_cache_file
      else
        exec
      end
    end

    def exec
      cmd = to_command
      Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thread|
        @output = stdout.readlines
        @error  = stderr.readlines
        @status = wait_thread.value
      end

      #$$$ this functionality isn't in Logue yet (dump of one element per line):
      # debug "output", @output
      # debug "error", @error

      debug "@status: #{@status}"      
      
      @output
    end

    def read_cache_file
      cachefile = CacheFile.new @cachedir, @args
      if cachefile.pathname.exist?
        @output = cachefile.pathname.read_file
      else
        exec
        cachefile.pathname.save_file @output
        @output
      end
    end

    def to_command
      @args.join ' '
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
command-cacheable-0.2.1 lib/command/cacheable/command.rb
command-cacheable-0.2.0 lib/command/cacheable/command.rb
command-cacheable-0.1.0 lib/command/cacheable/command.rb