Kylin/Universal/ProcessUtility.inl
2023-07-25 10:40:14 +08:00

45 lines
1.5 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 (defined __arm__) || (defined __aarch64__)
#include <experimental/filesystem>
#else
#include <filesystem>
#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 (defined __arm__) || (defined __aarch64__)
using namespace std::experimental;
#else
using namespace std;
#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__