diff --git a/src/server_env_manager.cpp b/src/server_env_manager.cpp index f4e644b..e6f21fb 100644 --- a/src/server_env_manager.cpp +++ b/src/server_env_manager.cpp @@ -172,10 +172,13 @@ bool server_env_manager::run_remote_template_command(const std::string &service_ bool server_env_manager::run_remote_template_command_and_capture_output(const std::string &service_name, const std::string &command, std::vector args, std::string &output, bool silent) const { - sCommand scommand = construct_standard_template_run_cmd(service_name, command, args, silent); + sCommand scommand = construct_standard_template_run_cmd(service_name, command, args, false); if (scommand.get_command_to_run().empty()) return false; - return execute_ssh_command(get_SSH_INFO(), scommand, cMode::CaptureOutput, &output); + + cMode mode = (silent ? cMode::Silent : cMode::None); + mode |= cMode::CaptureOutput; + return execute_ssh_command(get_SSH_INFO(), scommand, mode, &output); } diff --git a/src/utils/execute.cpp b/src/utils/execute.cpp index 62a6788..c4c4b87 100644 --- a/src/utils/execute.cpp +++ b/src/utils/execute.cpp @@ -68,8 +68,9 @@ bool execute_local_command(const sCommand& command, bool silent, bool safe) { return ok; } -bool execute_local_command_and_capture_output(const sCommand& command, std::string &output, bool silent, bool safe) +bool execute_local_command_and_capture_output(const sCommand& command, std::string * output, bool silent, bool safe) { + ASSERT_MSG(output != nullptr, "Output string must be provided"); if (command.get_command_to_run().empty()) return false; cStyle style = safe ? cStyle::Safe : cStyle::Raw; @@ -80,7 +81,7 @@ bool execute_local_command_and_capture_output(const sCommand& command, std::stri } char buffer[128]; while (fgets(buffer, sizeof(buffer), pipe) != nullptr) { - output += buffer; + (*output) += buffer; } int ret = pclose(pipe); return EXITSTATUSCHECK(ret); @@ -90,19 +91,19 @@ bool execute_local_command_and_capture_output(const sCommand& command, std::stri bool execute_local_command(const sCommand & command, cMode mode, std::string * output /* = nullptr */) { - if (mode & cMode::Interactive) { - ASSERT_MSG((mode & cMode::CaptureOutput), "Interactive mode and capture output mode cannot be used together"); + if (hasFlag(mode, cMode::Interactive)) { + ASSERT_MSG(! hasFlag(mode, cMode::CaptureOutput), "Interactive mode and capture output mode cannot be used together"); ASSERT_MSG(output == nullptr, "Interactive mode and an output string cannot be used together"); - ASSERT_MSG(! (mode & cMode::SafeCommand), "Interactive mode and safe command mode cannot be used together"); - return execute_local_command_interactive(command, mode & cMode::Silent); + ASSERT_MSG(! hasFlag(mode, cMode::SafeCommand), "Interactive mode and safe command mode cannot be used together"); + return execute_local_command_interactive(command, hasFlag(mode, cMode::Silent)); } - if (mode & cMode::CaptureOutput) { + if (hasFlag(mode, cMode::CaptureOutput)) { ASSERT_MSG(output != nullptr, "Capture output mode requires an output string to be provided"); - return execute_local_command_and_capture_output(command, *output, mode & cMode::Silent, mode & cMode::SafeCommand); + return execute_local_command_and_capture_output(command, output, hasFlag(mode, cMode::Silent), hasFlag(mode, cMode::SafeCommand)); } - return execute_local_command(command, mode & cMode::Silent, mode & cMode::SafeCommand); + return execute_local_command(command, hasFlag(mode, cMode::Silent), hasFlag(mode, cMode::SafeCommand)); } bool execute_ssh_command(const sSSHInfo &ssh_info, const sCommand &command, cMode mode, std::string *output) @@ -110,13 +111,14 @@ bool execute_ssh_command(const sSSHInfo &ssh_info, const sCommand &command, cMod if (command.get_command_to_run().empty()) return false; - ASSERT_MSG(!(mode & cMode::Interactive && mode & cMode::SafeCommand), "Safe command mode must not be used with Interactive mode"); + ASSERT_MSG(!(hasFlag(mode, cMode::Interactive) && hasFlag(mode, cMode::SafeCommand)), "Safe command mode must not be used with Interactive mode"); + ASSERT_MSG(!(hasFlag(mode, cMode::CaptureOutput) && output == nullptr), "Capture output mode must be used with an output string"); std::stringstream ssh_cmd; - ssh_cmd << "ssh -p " << ssh_info.port << " " << (mode & cMode::Interactive ? "-tt " : "") + ssh_cmd << "ssh -p " << ssh_info.port << " " << (hasFlag(mode, cMode::Interactive) ? "-tt " : "") << ssh_info.user << "@" << ssh_info.host; - sCommand ssh_command(ssh_cmd.str() + " bash -c " + quote(escapequotes(command.construct_cmd(mode & cMode::SafeCommand ? cStyle::Safe : cStyle::Raw)))); + sCommand ssh_command(ssh_cmd.str() + " bash -c " + quote(escapequotes(command.construct_cmd(hasFlag(mode, cMode::SafeCommand) ? cStyle::Safe : cStyle::Raw)))); bool rval = execute_local_command(ssh_command, mode, output); diff --git a/src/utils/execute.hpp b/src/utils/execute.hpp index ee95699..d2a3eed 100644 --- a/src/utils/execute.hpp +++ b/src/utils/execute.hpp @@ -66,12 +66,16 @@ class sCommand { // Bitwise AND operator for cMode -inline bool operator&(cMode lhs, cMode rhs) { - return ( - static_cast(lhs) & static_cast(rhs) != 0 +inline cMode operator&(cMode lhs, cMode rhs) { + return static_cast( + static_cast(lhs) & static_cast(rhs) ); } +inline bool hasFlag(cMode mode, cMode flag) { + return (mode & flag) == flag; +} + inline cMode operator-(cMode lhs, cMode rhs) { return static_cast( static_cast(lhs) & ~static_cast(rhs) @@ -82,6 +86,10 @@ inline cMode operator|(cMode lhs, cMode rhs) { return static_cast(static_cast(lhs) | static_cast(rhs)); } +inline cMode operator|=(cMode & lhs, cMode rhs) { + return lhs = lhs | rhs; +} + } // namespace dropshell