Skip to content

Commit af332eb

Browse files
committed
Add a wrapper for RunWithManagedStdio
Bug: b/434043969
1 parent ad992e8 commit af332eb

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

base/cvd/cuttlefish/common/libs/utils/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ cf_cc_library(
349349
hdrs = ["subprocess_managed_stdio.h"],
350350
deps = [
351351
"//cuttlefish/common/libs/fs",
352+
"//cuttlefish/common/libs/utils:result",
352353
"//cuttlefish/common/libs/utils:subprocess",
353354
"//libbase",
354355
],

base/cvd/cuttlefish/common/libs/utils/subprocess_managed_stdio.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ThreadJoiner {
5151

5252
} // namespace
5353

54-
int RunWithManagedStdio(Command&& cmd_tmp, const std::string* stdin_str,
54+
int RunWithManagedStdio(Command cmd_tmp, const std::string* stdin_str,
5555
std::string* stdout_str, std::string* stderr_str,
5656
SubprocessOptions options) {
5757
/*
@@ -140,4 +140,18 @@ int RunWithManagedStdio(Command&& cmd_tmp, const std::string* stdin_str,
140140
return code;
141141
}
142142

143+
Result<std::string> RunAndCaptureStdout(Command command) {
144+
std::string standard_out;
145+
std::string standard_err;
146+
std::string command_str = command.GetShortName();
147+
int exit_code = RunWithManagedStdio(std::move(command), nullptr,
148+
&standard_out, &standard_err);
149+
LOG(DEBUG) << "Ran " << command_str << " with stdout:\n" << standard_out;
150+
LOG(DEBUG) << "Ran " << command_str << " with stderr:\n" << standard_err;
151+
CF_EXPECTF(exit_code == 0,
152+
"Failed to execute '{}' <args>: exit code = {}, stdout = '{}', stderr = '{}'",
153+
command_str, exit_code, standard_out, standard_err);
154+
return standard_out;
155+
}
156+
143157
} // namespace cuttlefish

base/cvd/cuttlefish/common/libs/utils/subprocess_managed_stdio.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717

1818
#include <string>
1919

20+
#include "cuttlefish/common/libs/utils/result.h"
2021
#include "cuttlefish/common/libs/utils/subprocess.h"
2122

2223
namespace cuttlefish {
2324

25+
// Success is defined by the exit code being `0`.
26+
Result<std::string> RunAndCaptureStdout(Command);
27+
2428
/*
2529
* Consumes a Command and runs it, optionally managing the stdio channels.
2630
*
@@ -33,8 +37,8 @@ namespace cuttlefish {
3337
* If some setup fails, `command` fails to start, or `command` exits due to a
3438
* signal, the return value will be negative.
3539
*/
36-
int RunWithManagedStdio(Command&& cmd_tmp, const std::string* stdin,
37-
std::string* stdout, std::string* stderr,
40+
int RunWithManagedStdio(Command, const std::string* stdin, std::string* stdout,
41+
std::string* stderr,
3842
SubprocessOptions options = SubprocessOptions());
3943

4044
} // namespace cuttlefish

0 commit comments

Comments
 (0)