Sha256: 88bf57471c44d876932ddd58426ec820af317e76285d47a958c6fb76911ddf28

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

module Jazzy
  module Executable
    class IO < Array
      def initialize(io = nil)
        @io = io
      end

      def <<(value)
        super
      ensure
        @io << value.to_s if @io
      end

      def to_s
        join("\n")
      end
    end

    class << self
      def execute_command(executable, args, raise_on_failure)
        require 'shellwords'
        bin = `which #{executable.to_s.shellescape}`.strip
        raise "Unable to locate the executable `#{executable}`" if bin.empty?

        require 'open4'

        stdout, stderr = IO.new, IO.new($stderr)

        options = { stdout: stdout, stderr: stderr, status: true }
        status  = Open4.spawn(bin, *args, options)
        unless status.success?
          full_command = "#{bin.shellescape} #{args.map(&:shellescape)}"
          output  = stdout.to_s << stderr.to_S
          if raise_on_failure
            raise "#{full_command}\n\n#{output}"
          else
            warn("[!] Failed: #{full_command}")
          end
        end
        [stdout.to_s, status]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jazzy-0.2.0 lib/jazzy/executable.rb
jazzy-0.1.6 lib/jazzy/executable.rb
jazzy-0.1.5 lib/jazzy/executable.rb