Trying new approach

This commit is contained in:
Your Name
2025-05-18 13:07:09 +12:00
parent 828171c977
commit 314a5fe96a
6 changed files with 24 additions and 52 deletions

View File

@ -56,36 +56,9 @@ namespace dropshell
}
}
// ----------------------------------------------------------------------------------------------------------
// execute_local_command_and_capture_output
// ----------------------------------------------------------------------------------------------------------
bool execute_local_command_and_capture_output(const sCommand &command, std::string *output)
{
ASSERT(output != nullptr, "Output string must be provided");
if (command.get_command_to_run().empty())
return false;
std::string full_cmd = command.construct_cmd(localpath::agent()) + " 2>&1";
FILE *pipe = popen(full_cmd.c_str(), "r");
if (!pipe)
{
return false;
}
char buffer[128];
while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
{
(*output) += buffer;
}
int ret = pclose(pipe);
return EXITSTATUSCHECK(ret);
}
// ----------------------------------------------------------------------------------------------------------
// execute_local_command
// ----------------------------------------------------------------------------------------------------------
bool execute_local_command(std::string command, std::string *output, cMode mode)
{
return execute_local_command("", command, {}, output, mode);
}
bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> &env_vars, std::string *output, cMode mode)
{
@ -99,32 +72,32 @@ namespace dropshell
return execute_local_command_interactive(command);
}
if (hasFlag(mode, cMode::CaptureOutput))
{
ASSERT(output != nullptr, "Capture output mode requires an output string to be provided");
ASSERT(!hasFlag(mode, cMode::Silent), "Silent mode is not allowed with capture output mode");
return execute_local_command_and_capture_output(command, output);
}
if (command.get_command_to_run().empty())
return false;
bool silent = hasFlag(mode, cMode::Silent);
std::string full_cmd = command.construct_cmd(localpath::agent()) + " 2>&1" + (silent ? " > /dev/null" : "");
int ret=0;
{
SwitchColour sc(sColour::DEBUG, std::cerr);
ret = system(full_cmd.c_str());
}
std::string full_cmd = command.construct_cmd(localpath::agent()) + (hasFlag(mode, cMode::CaptureOutput) ? " 2>&1" : ""); // capture both stdout and stderr
bool ok = EXITSTATUSCHECK(ret);
if (!ok && !silent)
FILE *pipe = popen(full_cmd.c_str(), "r");
if (!pipe)
{
PrintError("Error: Failed to execute command: ");
PrintError(full_cmd);
return false;
}
return ok;
char buffer[128];
while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
{
if (output != nullptr)
(*output) += buffer;
if (!silent)
{
std::cerr << buffer;
}
}
int ret = pclose(pipe);
return EXITSTATUSCHECK(ret);
}
// ----------------------------------------------------------------------------------------------------------