Sha256: b040bd3cd19ec68a353a2fa2658635629566cc8d82f8390cf28f0e09e23ba0f5

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require 'vagrant/util/busy'
require 'vagrant/util/subprocess'

module VagrantPlugins
  module ProviderLocal
    module Executor
      # This class is used to execute commands as subprocess.
      class Exec
        # When we need the command's exit code we should set parameter
        # exit_code to true, otherwise this method will return executed
        # command's stdout
        def execute(exit_code, *cmd, **_opts, &block)
          # Append in the options for subprocess
          cmd << { notify: %i[stdout stderr] }

          cmd.unshift('sh', '-c')
          interrupted = false
          # Lambda to change interrupted to true
          int_callback = -> { interrupted = true }
          result = ::Vagrant::Util::Busy.busy(int_callback) do
            ::Vagrant::Util::Subprocess.execute(*cmd, &block)
          end
          return result.exit_code if exit_code

          result.stderr.gsub!("\r\n", "\n")
          result.stdout.gsub!("\r\n", "\n")
          puts "Command Failed: #{cmd}" if result.exit_code != 0 || interrupted
          puts "Exit Results: #{result.stderr}" if result.exit_code != 0 || interrupted
          raise Errors::ExecuteError if result.exit_code != 0 || interrupted

          result.stdout[0..-2]
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vagrant-local-0.0.3 lib/vagrant-local/executor.rb
vagrant-local-0.0.2 lib/vagrant-local/executor.rb
vagrant-local-0.0.1 lib/vagrant-local/executor.rb