Sha256: f8a68ec7054ce4a46c3f015d79d919b85b8b68cbf673cf36fb29b504edbbce57

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 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, env: {})
        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(env, 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

5 entries across 5 versions & 1 rubygems

Version Path
jazzy-0.3.2 lib/jazzy/executable.rb
jazzy-0.3.1 lib/jazzy/executable.rb
jazzy-0.3.0 lib/jazzy/executable.rb
jazzy-0.2.4 lib/jazzy/executable.rb
jazzy-0.2.3 lib/jazzy/executable.rb