Get stdout and stderr from command

This commit is contained in:
sophia 2021-12-17 17:10:47 -06:00 committed by Paul Hinze
parent c63e7936b2
commit 8e85561f61
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
3 changed files with 14 additions and 5 deletions

View File

@ -40,9 +40,12 @@ module Vagrant
@client.upload(@machine, from, to)
end
def execute(cmd, opts=nil)
def execute(cmd, opts=nil, &block)
@logger.debug("remote communicator, executing command")
@client.execute(@machine, cmd, opts)
res = @client.execute(@machine, cmd, opts)
yield :stdout, res.stdout if block_given?
yield :stderr, res.stderr if block_given?
res.exit_code
end
def sudo(cmd, opts=nil)

View File

@ -131,7 +131,7 @@ module VagrantPlugins
@logger.debug("excuting")
res = client.execute(req)
@logger.debug("excution result: #{res}")
res.exit_code
res
end
# @param [Vagrant::Machine]

View File

@ -208,11 +208,17 @@ module VagrantPlugins
plugin = Vagrant.plugin("2").manager.communicators[plugin_name.to_s.to_sym]
communicator = plugin.new(machine)
opts.transform_keys!(&:to_sym)
exit_code = communicator.execute(cmd.command, opts)
output = {stdout: '', stderr: ''}
exit_code = communicator.execute(cmd.command, opts) {
|type, data| output[type] << data if output[type]
}
logger.debug("command exit code: #{exit_code}")
logger.debug("command output: #{output}")
SDK::Communicator::ExecuteResp.new(
exit_code: exit_code
exit_code: exit_code,
stdout: output[:stdout],
stderr: output[:stderr]
)
end
end