31 lines
880 B
C++
31 lines
880 B
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <cstdio>
|
|
|
|
class CommandHelper {
|
|
public:
|
|
static std::string exec(const char *cmd) {
|
|
std::array<char, 128> buffer;
|
|
std::string result;
|
|
std::unique_ptr<FILE, int(*)(FILE*)> pipe(popen(cmd, "r"), pclose);
|
|
if (!pipe) {
|
|
throw std::runtime_error("popen() failed!");
|
|
}
|
|
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr) {
|
|
result += buffer.data();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static void execNoOutput(std::string cmd) {
|
|
std::string command = cmd + " > /dev/null 2>&1";
|
|
int ret = std::system(command.c_str());
|
|
if (ret != 0) {
|
|
throw std::runtime_error("Command failed with return code: " + std::to_string(ret));
|
|
}
|
|
}
|
|
}; |