Sha256: fda8a4d96e6c077e1fcb2a3fdad79804fce65fe8ab8e53d7af7c14de769e9436

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require "open3"

module Vectory
  class SystemCall
    TIMEOUT = 60

    attr_reader :status, :stdout, :stderr, :cmd

    def initialize(cmd, timeout = TIMEOUT)
      @cmd = cmd
      @timeout = timeout
    end

    def call
      log_cmd(@cmd)

      execute(@cmd)

      log_result

      raise_error unless @status.success?

      self
    end

    private

    def log_cmd(cmd)
      Vectory.ui.debug("Cmd: '#{cmd}'")
    end

    def execute(cmd)
      result = Utils.capture3_with_timeout(cmd,
                                           timeout: @timeout,
                                           kill_after: @timeout)
      Vectory.ui.error(result.inspect)
      @stdout = result[:stdout]
      @stderr = result[:stderr]
      @status = result[:status]
    rescue Errno::ENOENT => e
      raise SystemCallError, e.inspect
    end

    def log_result
      Vectory.ui.debug("Status: #{@status.inspect}")
      Vectory.ui.debug("Stdout: '#{@stdout.strip}'")
      Vectory.ui.debug("Stderr: '#{@stderr.strip}'")
    end

    def raise_error
      raise SystemCallError,
            "Failed to run #{@cmd},\n  " \
            "status: #{@status.exitstatus},\n  " \
            "stdout: '#{@stdout.strip}',\n  " \
            "stderr: '#{@stderr.strip}'"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vectory-0.3.0 lib/vectory/system_call.rb