quick commit
This commit is contained in:
31
include/helpers/command.hpp
Normal file
31
include/helpers/command.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#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));
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user