|
6 | 6 | #ifndef CLOYSTERHPC_RUNNER_H_ |
7 | 7 | #define CLOYSTERHPC_RUNNER_H_ |
8 | 8 |
|
| 9 | +#include <boost/process.hpp> |
| 10 | + |
9 | 11 | #include <string> |
10 | 12 | #include <vector> |
11 | 13 |
|
12 | 14 | namespace cloyster::services { |
13 | 15 |
|
| 16 | +/** |
| 17 | + * @struct CommandProxy |
| 18 | + * @brief A command proxy to capture the command output while the command is |
| 19 | + * running. |
| 20 | + * |
| 21 | + * This structure is used to capture the output of a command in real-time, |
| 22 | + * useful for displaying progress in a dialog. |
| 23 | + */ |
| 24 | +struct CommandProxy { |
| 25 | + bool valid = false; |
| 26 | + boost::process::child child; |
| 27 | + boost::process::ipstream pipe_stream; |
| 28 | + |
| 29 | + /** |
| 30 | + * @brief Gets a line of output from the command. |
| 31 | + * |
| 32 | + * @return An optional string containing a line of output if available, |
| 33 | + * otherwise std::nullopt. |
| 34 | + */ |
| 35 | + std::optional<std::string> getline(); |
| 36 | + std::optional<std::string> getUntil(char chr); |
| 37 | +}; |
| 38 | + |
| 39 | +enum class Stream : std::uint8_t { Stdout, Stderr }; |
| 40 | + |
14 | 41 | /** |
15 | 42 | * Works as an abstraction for command execution. |
16 | 43 | */ |
17 | 44 | class IRunner { |
18 | 45 | public: |
| 46 | + IRunner() = default; |
| 47 | + IRunner(const IRunner&) = default; |
| 48 | + IRunner(IRunner&&) = delete; |
| 49 | + IRunner& operator=(const IRunner&) = default; |
| 50 | + IRunner& operator=(IRunner&&) = delete; |
| 51 | + virtual ~IRunner() = default; |
| 52 | + |
19 | 53 | virtual int executeCommand(const std::string&) = 0; |
| 54 | + virtual CommandProxy executeCommandIter(const std::string&, Stream out = Stream::Stdout) = 0; |
20 | 55 | virtual void checkCommand(const std::string&) = 0; |
21 | 56 | virtual std::vector<std::string> checkOutput(const std::string&) = 0; |
22 | 57 |
|
23 | 58 | virtual int downloadFile(const std::string& url, const std::string& file); |
24 | | - |
25 | | - virtual ~IRunner() = default; |
26 | 59 | }; |
27 | 60 |
|
28 | | -class Runner : public IRunner { |
| 61 | +class Runner final : public IRunner { |
29 | 62 | public: |
30 | | - int executeCommand(const std::string&) override; |
31 | | - void checkCommand(const std::string&) override; |
32 | | - std::vector<std::string> checkOutput(const std::string&) override; |
33 | | - |
34 | | - virtual ~Runner() = default; |
| 63 | + CommandProxy executeCommandIter(const std::string& cmd, Stream out = Stream::Stdout) override; |
| 64 | + int executeCommand(const std::string& cmd) override; |
| 65 | + void checkCommand(const std::string& cmd) override; |
| 66 | + std::vector<std::string> checkOutput(const std::string& cmd) override; |
| 67 | + int downloadFile(const std::string& url, const std::string& file) override; |
35 | 68 | }; |
36 | 69 |
|
37 | | -class DryRunner : public IRunner { |
| 70 | +class DryRunner final : public IRunner { |
38 | 71 | public: |
39 | | - int executeCommand(const std::string&) override; |
40 | | - void checkCommand(const std::string&) override; |
41 | | - std::vector<std::string> checkOutput(const std::string&) override; |
42 | | - |
43 | | - virtual ~DryRunner() = default; |
| 72 | + CommandProxy executeCommandIter(const std::string& cmd, Stream out = Stream::Stdout) override; |
| 73 | + int executeCommand(const std::string& cmd) override; |
| 74 | + void checkCommand(const std::string& cmd) override; |
| 75 | + std::vector<std::string> checkOutput(const std::string& cmd) override; |
| 76 | + int downloadFile(const std::string& url, const std::string& file) override; |
44 | 77 | }; |
45 | 78 |
|
46 | | -class MockRunner : public IRunner { |
| 79 | +class MockRunner final : public IRunner { |
47 | 80 | public: |
48 | | - int executeCommand(const std::string&) override; |
49 | | - void checkCommand(const std::string&) override; |
50 | | - std::vector<std::string> checkOutput(const std::string&) override; |
51 | | - |
52 | | - virtual ~MockRunner() = default; |
| 81 | + CommandProxy executeCommandIter(const std::string& cmd, Stream out = Stream::Stdout) override; |
| 82 | + int executeCommand(const std::string& cmd) override; |
| 83 | + void checkCommand(const std::string& cmd) override; |
| 84 | + std::vector<std::string> checkOutput(const std::string& cmd) override; |
| 85 | + int downloadFile(const std::string& url, const std::string& file) override; |
53 | 86 |
|
54 | 87 | [[nodiscard]] const std::vector<std::string>& listCommands() const; |
55 | 88 |
|
|
0 commit comments