Sha256: 6c3285e57bc36a42a9f9456ae71a1ed0bfc83050eb3f642dff0a86bdb57c52d6

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require 'open3'

module Autowow
  class Command

    include EasyLogging

    def self.run(*args)
      Command.new(*args).explain.chronic_execute
    end

    def self.run_dry(*args)
      Command.new(*args).execute
    end

    def self.popen3_reader(*args)
      args.each do |arg|
        reader = <<-EOF
          def #{arg}
            @#{arg} = @#{arg}.read.rstrip unless @#{arg}.is_a?(String)
            return @#{arg}
          end
        EOF
        class_eval(reader)
      end
    end

    popen3_reader :stdin, :stdout, :stderr
    attr_reader :wait_thr

    def initialize(*args)
      @cmd = args
    end

    def explain
      logger.debug(@cmd.join(' ')) unless @cmd.empty?
      self
    end

    def execute
      @stdin, @stdout, @stderr, @wait_thr = Open3.popen3(*@cmd)
      self
    end

    def chronic_execute
      @stdin, @stdout, @stderr, @wait_thr = Open3.popen3(*@cmd)
      logger.error(stderr) unless stderr.empty?
      self
    end

    def output_matches?(matcher)
      stdout.match(matcher)
    end

    def output_does_not_match?(matcher)
      !output_matches?(matcher)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
autowow-0.1.0 lib/autowow/command.rb