|
9 | 9 | #include <boost/process.hpp> |
10 | 10 | #include <fmt/format.h> |
11 | 11 |
|
| 12 | +#include <stdexcept> |
12 | 13 | #include <string> |
13 | 14 | #include <vector> |
14 | 15 |
|
|
17 | 18 | #include <cloysterhpc/services/options.h> |
18 | 19 | #include <cloysterhpc/services/scriptbuilder.h> |
19 | 20 |
|
20 | | -namespace cloyster::services::runner { |
| 21 | +namespace cloyster::services::runner::shell { |
| 22 | + |
| 23 | +namespace unsafe { |
| 24 | + template <typename... Args> |
| 25 | + [[nodiscard]] |
| 26 | + int fmt(std::vector<std::string>& output, |
| 27 | + fmt::format_string<Args...> format, Args&&... args) |
| 28 | + { |
| 29 | + auto command = fmt::format(format, std::forward<Args>(args)...); |
| 30 | + |
| 31 | + auto opts = cloyster::Singleton<cloyster::services::Options>::get(); |
| 32 | + if (!opts->dryRun) { |
| 33 | + LOG_DEBUG("Running shell command: {}", command); |
| 34 | + boost::process::ipstream pipe_stream; |
| 35 | + boost::process::child child("/bin/bash", "-c", command, |
| 36 | + boost::process::std_out > pipe_stream); |
| 37 | + |
| 38 | + std::string line; |
| 39 | + while (pipe_stream && std::getline(pipe_stream, line)) { |
| 40 | + output.emplace_back(line); |
| 41 | + LOG_TRACE("{}", line); |
| 42 | + } |
| 43 | + |
| 44 | + child.wait(); |
| 45 | + LOG_DEBUG("Exit code: {}", child.exit_code()); |
| 46 | + return child.exit_code(); |
| 47 | + } else { |
| 48 | + LOG_INFO("Dry Run: {}", command); |
| 49 | + return 0; |
| 50 | + } |
| 51 | + } |
21 | 52 |
|
22 | | -template <typename... Args> |
23 | | -int shellfmt(fmt::format_string<Args...> fmt, Args&&... args) |
24 | | -{ |
25 | | - auto command = fmt::format(fmt, std::forward<Args>(args)...); |
26 | | - |
27 | | - auto opts = cloyster::Singleton<cloyster::services::Options>::get(); |
28 | | - if (!opts->dryRun) { |
29 | | - LOG_DEBUG("Running shell command: {}", command); |
30 | | - boost::process::ipstream pipe_stream; |
31 | | - boost::process::child child( |
32 | | - "/bin/bash", "-c", command, boost::process::std_out > pipe_stream); |
33 | | - |
34 | | - std::string line; |
35 | | - while (pipe_stream && std::getline(pipe_stream, line)) { |
36 | | - LOG_TRACE("{}", line); |
| 53 | + template <typename... Args> |
| 54 | + [[nodiscard]] |
| 55 | + int fmt(fmt::format_string<Args...> format, Args&&... args) |
| 56 | + { |
| 57 | + auto command = fmt::format(format, std::forward<Args>(args)...); |
| 58 | + |
| 59 | + auto opts = cloyster::Singleton<cloyster::services::Options>::get(); |
| 60 | + if (!opts->dryRun) { |
| 61 | + LOG_DEBUG("Running shell command: {}", command); |
| 62 | + boost::process::ipstream pipe_stream; |
| 63 | + boost::process::child child("/bin/bash", "-c", command, |
| 64 | + boost::process::std_out > pipe_stream); |
| 65 | + |
| 66 | + std::string line; |
| 67 | + while (pipe_stream && std::getline(pipe_stream, line)) { |
| 68 | + LOG_TRACE("{}", line); |
| 69 | + } |
| 70 | + |
| 71 | + child.wait(); |
| 72 | + LOG_DEBUG("Exit code: {}", child.exit_code()); |
| 73 | + return child.exit_code(); |
| 74 | + } else { |
| 75 | + LOG_INFO("Dry Run: {}", command); |
| 76 | + return 0; |
37 | 77 | } |
| 78 | + } |
| 79 | +} |
38 | 80 |
|
39 | | - child.wait(); |
40 | | - LOG_DEBUG("Exit code: {}", child.exit_code()); |
41 | | - return child.exit_code(); |
42 | | - } else { |
43 | | - LOG_INFO("Dry Run: {}", command); |
44 | | - return 0; |
| 81 | +template <typename... Args> |
| 82 | +void fmt(fmt::format_string<Args...> format, Args&&... args) |
| 83 | +{ |
| 84 | + const std::string command |
| 85 | + = fmt::format(format, std::forward<Args>(args)...); |
| 86 | + const auto exitCode = unsafe::fmt("{}", command); |
| 87 | + if (exitCode != 0) { |
| 88 | + throw std::runtime_error(fmt::format( |
| 89 | + "Command {} failed with exit code {}", command, exitCode)); |
45 | 90 | } |
46 | 91 | } |
47 | 92 |
|
48 | | -int shell(std::string_view cmd); |
| 93 | +void cmd(std::string_view cmd); |
| 94 | + |
| 95 | +template <typename... Args> |
| 96 | +[[nodiscard]] |
| 97 | +std::string output(fmt::format_string<Args...> format, Args&&... args) |
| 98 | +{ |
| 99 | + std::vector<std::string> output; |
| 100 | + // Cosntruct a command here because it will be used twice and |
| 101 | + // we can't use args twice without hurting the perfect forwarding |
| 102 | + const std::string command |
| 103 | + = fmt::format(format, std::forward<Args>(args)...); |
| 104 | + const auto exitCode = unsafe::fmt(output, "{}", command); |
| 105 | + if (exitCode != 0) { |
| 106 | + throw std::runtime_error(fmt::format( |
| 107 | + "Command {} failed with exit code {}", command, exitCode)); |
| 108 | + } |
| 109 | + return fmt::format("{}", fmt::join(output, "\n")); |
| 110 | +} |
49 | 111 |
|
50 | 112 | } |
51 | 113 |
|
|
0 commit comments