Sha256: 89e4e447af59d7b9bb4f441a91c936900627c6ead84931577f42bfffceb46033

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require "shellator/version"

require "open3"

module Shellator
  def self.noninteractive(cmd, in_directory: nil, stdin_content: [], stdin_newlines: true, stdout:, stderr:)
    raise "cmd must be a String or array of Strings." \
      unless cmd.is_a?(String) || (cmd.is_a?(Array) && cmd.all? { |c| c.is_a?(Stirng) })
    raise "stdout must be a Proc." unless stdout.is_a?(Proc)
    raise "stderr must be a Proc." unless stderr.is_a?(Proc)

    stdin_content = [stdin_content].flatten
    raise "stdin_content must be an Array of Strings." \
      unless stdin_content.is_a?(Array) && stdin_content.all? { |c| c.is_a?(String) }

    computed_cmd = [cmd].flatten.join(" ")

    Dir.chdir(in_directory || Dir.pwd) do
      Open3.popen3(computed_cmd) do |stdin_stream, stdout_stream, stderr_stream, thread|
        stdin_content.each do |input_line|
          stdin_stream.write(input_line)

          stdin_stream.write("\n") unless input_line[-1] == "\n" || !stdin_newlines
        end

        stdin_stream.close

        { stdout_stream => stdout, stderr_stream => stderr }.each_pair do |stream, callback|
          Thread.new do
            until (line = stream.gets).nil?
              callback.call(line)
            end
          end
        end

        thread.join
        thread.value
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shellator-0.1.0 lib/shellator.rb