diff --git a/source/src/commands/edit.cpp b/source/src/commands/edit.cpp index 9554068..a452906 100644 --- a/source/src/commands/edit.cpp +++ b/source/src/commands/edit.cpp @@ -75,7 +75,7 @@ bool edit_file(const std::string &file_path, bool has_bb64) std::cout << "Editing file: " << file_path << std::endl; if (has_bb64) { - return execute_local_command(editor_cmd, nullptr, cMode::Interactive); + return execute_local_command("", editor_cmd, {}, nullptr, cMode::Interactive); } else { // might not have bb64 at this early stage. Direct edit. diff --git a/source/src/commands/install.cpp b/source/src/commands/install.cpp index 041b57c..75ace8a 100644 --- a/source/src/commands/install.cpp +++ b/source/src/commands/install.cpp @@ -312,7 +312,7 @@ namespace dropshell quote(agent_path) + " " + quote("$(id -u " + server_env.get_SSH_USER() + "):$(id -g " + server_env.get_SSH_USER() + ")") + "'"; //std::cout << "Executing: " << remote_cmd << std::endl; - if (!execute_local_command(remote_cmd, nullptr, cMode::Silent)) + if (!execute_local_command("", remote_cmd, {}, nullptr, cMode::Silent)) std::cerr << "Failed to download bb64 to " << agent_path << " on remote server." << std::endl; else std::cout << "Downloaded bb64 to " << agent_path << " on remote server." << std::endl; diff --git a/source/src/commands/shared_commands.cpp b/source/src/commands/shared_commands.cpp index bfe478f..68aefae 100644 --- a/source/src/commands/shared_commands.cpp +++ b/source/src/commands/shared_commands.cpp @@ -62,7 +62,7 @@ namespace dropshell quote(local_path + "/") + " " + quote(server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" + remote_path + "/"); - return execute_local_command(rsync_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults)); + return execute_local_command("", rsync_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults)); } // ------------------------------------------------------------------------------------------------ diff --git a/source/src/service_runner.cpp b/source/src/service_runner.cpp index 7196691..1a43d10 100644 --- a/source/src/service_runner.cpp +++ b/source/src/service_runner.cpp @@ -147,7 +147,7 @@ bool service_runner::fullnuke() } std::string rm_cmd = "rm -rf " + quote(local_service_path); - if (!execute_local_command(rm_cmd, nullptr, cMode::Silent)) { + if (!execute_local_command("", rm_cmd, {}, nullptr, cMode::Silent)) { std::cerr << "Failed to remove service directory" << std::endl; return false; } @@ -267,13 +267,13 @@ bool service_runner::interactive_ssh_service() bool service_runner::scp_file_to_remote(const std::string &local_path, const std::string &remote_path, bool silent) { std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + quote(local_path) + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + (silent ? " > /dev/null 2>&1" : ""); - return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults)); + return execute_local_command("", scp_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults)); } bool service_runner::scp_file_from_remote(const std::string &remote_path, const std::string &local_path, bool silent) { std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + " " + quote(local_path) + (silent ? " > /dev/null 2>&1" : ""); - return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults)); + return execute_local_command("", scp_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults)); } bool service_runner::restore(std::string backup_file, bool silent) diff --git a/source/src/utils/execute.cpp b/source/src/utils/execute.cpp index 144e0f2..1cfa2f7 100644 --- a/source/src/utils/execute.cpp +++ b/source/src/utils/execute.cpp @@ -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 &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); } // ---------------------------------------------------------------------------------------------------------- diff --git a/source/src/utils/execute.hpp b/source/src/utils/execute.hpp index 9785054..353ab32 100644 --- a/source/src/utils/execute.hpp +++ b/source/src/utils/execute.hpp @@ -31,7 +31,6 @@ typedef struct sSSHInfo { std::string server_ID; // dropshell name for server. } sSSHInfo; -bool execute_local_command(std::string command, std::string * output = nullptr, cMode mode = cMode::Defaults); bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map & env_vars, std::string * output = nullptr, cMode mode = cMode::Defaults); bool execute_ssh_command(const sSSHInfo & ssh_info, const sCommand & remote_command, cMode mode = cMode::Defaults, std::string * output = nullptr);