Sha256: 79669b9d9a96f0729873d36338effcbeb4034d8c0e50ba69ddf23adf8b63c051

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 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)
      @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

4 entries across 4 versions & 1 rubygems

Version Path
vectory-0.5.0 lib/vectory/system_call.rb
vectory-0.4.2 lib/vectory/system_call.rb
vectory-0.4.1 lib/vectory/system_call.rb
vectory-0.4.0 lib/vectory/system_call.rb