74 lines
2.8 KiB
C++
74 lines
2.8 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <chrono>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Process
|
|
{
|
|
|
|
/// Captured output from a completed subprocess.
|
|
struct Output {
|
|
int exit_code{0};
|
|
std::string out; ///< captured stdout
|
|
std::string err; ///< captured stderr
|
|
};
|
|
|
|
/// Live line callbacks for run_streaming(). Each fires (on the calling
|
|
/// thread, as output arrives) with one line of output, newline stripped.
|
|
/// Either may be left empty. The full output is still accumulated into the
|
|
/// returned Output.
|
|
struct StreamCallbacks {
|
|
std::function<void(std::string_view line)> on_stdout;
|
|
std::function<void(std::string_view line)> on_stderr;
|
|
};
|
|
|
|
/// Run an executable synchronously, capturing stdout and stderr separately.
|
|
///
|
|
/// @param executable Path to the binary (searched via PATH when relative).
|
|
/// @param args Additional arguments passed after argv[0].
|
|
/// @param stdin_data Data written to the process's stdin before reading.
|
|
/// @param timeout Wall-clock limit; zero means no limit. On expiry the
|
|
/// process receives SIGTERM and the call returns.
|
|
///
|
|
/// Returns an error only when process creation itself fails (pipe or fork
|
|
/// failure). A process that exits non-zero still yields a successful Result
|
|
/// with the appropriate exit_code. A process whose executable is not found
|
|
/// exits with code 127 (POSIX convention).
|
|
[[nodiscard]] Void::Result::Result<Output, Void::Result::Error>
|
|
run(const std::filesystem::path& executable,
|
|
std::vector<std::string> args = {},
|
|
std::string_view stdin_data = {},
|
|
std::chrono::milliseconds timeout = std::chrono::milliseconds{0});
|
|
|
|
/// Like run(), but invokes `callbacks` line-by-line as stdout/stderr
|
|
/// arrive, for progress reporting on long-running or streaming processes.
|
|
/// The complete output is still captured into the returned Output.
|
|
[[nodiscard]] Void::Result::Result<Output, Void::Result::Error>
|
|
run_streaming(
|
|
const std::filesystem::path& executable,
|
|
std::vector<std::string> args,
|
|
const StreamCallbacks& callbacks,
|
|
std::string_view stdin_data = {},
|
|
std::chrono::milliseconds timeout = std::chrono::milliseconds{0});
|
|
|
|
/// Run a command through /bin/sh -c "command".
|
|
[[nodiscard]] Void::Result::Result<Output, Void::Result::Error>
|
|
shell(std::string_view command,
|
|
std::chrono::milliseconds timeout = std::chrono::milliseconds{0});
|
|
|
|
/// Replace the current process image while preserving its PID and inherited
|
|
/// environment. Returns only when the platform does not support re-exec or
|
|
/// exec fails.
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
|
reexec(const std::filesystem::path& executable,
|
|
const std::vector<std::string>& arguments);
|
|
|
|
} // namespace Void::Process
|