From 8e85561f613a3663667e3bfd1f09b0ef5d457eb6 Mon Sep 17 00:00:00 2001 From: sophia Date: Fri, 17 Dec 2021 17:10:47 -0600 Subject: [PATCH] Get stdout and stderr from command --- lib/vagrant/plugin/remote/communicator.rb | 7 +++++-- plugins/commands/serve/client/communicator.rb | 2 +- plugins/commands/serve/service/communicator_service.rb | 10 ++++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/plugin/remote/communicator.rb b/lib/vagrant/plugin/remote/communicator.rb index 3de0f2f00..fd30396aa 100644 --- a/lib/vagrant/plugin/remote/communicator.rb +++ b/lib/vagrant/plugin/remote/communicator.rb @@ -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) diff --git a/plugins/commands/serve/client/communicator.rb b/plugins/commands/serve/client/communicator.rb index 23f7f3c1f..0da66bb22 100644 --- a/plugins/commands/serve/client/communicator.rb +++ b/plugins/commands/serve/client/communicator.rb @@ -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] diff --git a/plugins/commands/serve/service/communicator_service.rb b/plugins/commands/serve/service/communicator_service.rb index 22d798cfd..3bafd1938 100644 --- a/plugins/commands/serve/service/communicator_service.rb +++ b/plugins/commands/serve/service/communicator_service.rb @@ -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