Kylin/Universal/ProcessUtility.inl
2025-01-23 10:29:45 +08:00

49 lines
1.6 KiB
C++

#ifndef __PROCESSUTILITY_INL__
#define __PROCESSUTILITY_INL__
#include "BoostLog.h"
#include "ProcessUtility.h"
#include <boost/algorithm/string/trim.hpp>
#include <boost/process.hpp>
#if __cplusplus >= 201703L
#include <filesystem>
#elif __cplusplus >= 201402L && defined(__GNUC__)
#include <experimental/filesystem>
#else
#error "C++14 or later is required"
#endif
template <typename... Args>
void DockerUtility::runScript(std::string_view container, const std::string &script, Args &&...scriptArgs) {
using namespace boost::process;
auto env = boost::this_process::environment();
auto user = env["USER"].to_string();
auto docker = search_path("docker");
system(docker,
args = {"exec", "-d", "-u", user, container.data(), "bash", script, std::forward<Args>(scriptArgs)...});
}
template <typename... Args>
void NativeUtility::runScript(const std::string &script, Args &&...scriptArgs) {
#if __cplusplus >= 201703L
using namespace std;
#elif __cplusplus >= 201402L && defined(__GNUC__)
using namespace std::experimental;
#else
#error "C++14 or later is required"
#endif
#if defined(WIN32) || defined(ANDROID)
LOG(info) << "DockerUtility::runScript() not supported on windows or android.";
#else
auto askpass = filesystem::current_path().parent_path() / "Askpass/Askpassd.AppImage";
auto env = boost::this_process::environment();
env["SUDO_ASKPASS"] = askpass;
auto bash = boost::process::search_path("bash");
boost::process::system(bash, boost::process::args = {script, std::forward<Args>(scriptArgs)...}, env);
#endif
}
#endif // __PROCESSUTILITY_INL__