diff --git a/src/service_runner.cpp b/src/service_runner.cpp index 73ddb73..aaa9f4c 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -171,7 +171,7 @@ bool service_runner::fullnuke() } std::string rm_cmd = "rm -rf " + quote(local_service_path); - if (!execute_local_command(rm_cmd, cMode::Silent)) { + if (!execute_local_command(rm_cmd, nullptr, cMode::Silent)) { std::cerr << "Failed to remove service directory" << std::endl; return false; } @@ -442,7 +442,7 @@ bool service_runner::edit_file(const std::string &file_path) } std::cout << "Editing file: " << file_path << std::endl; - return execute_local_command(editor_cmd, cMode::Interactive | cMode::RawCommand); + return execute_local_command(editor_cmd, nullptr, cMode::Interactive | cMode::RawCommand); } bool service_runner::interactive_ssh_service() @@ -472,13 +472,13 @@ void service_runner::edit_service_config() 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, (silent ? cMode::Silent : cMode::None) + cMode::RawCommand); + return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::None) + cMode::RawCommand); } 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, (silent ? cMode::Silent : cMode::None) + cMode::RawCommand); + return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::None) + cMode::RawCommand); } bool service_runner::rsync_tree_to_remote(const std::string &local_path, const std::string &remote_path, bool silent) @@ -489,7 +489,7 @@ bool service_runner::rsync_tree_to_remote(const std::string &local_path, const s quote(local_path + "/") + " "+ quote(mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + remote_path + "/"); - return execute_local_command(rsync_cmd, (silent ? cMode::Silent : cMode::None) + cMode::RawCommand); + return execute_local_command(rsync_cmd, nullptr, (silent ? cMode::Silent : cMode::None) + cMode::RawCommand); } bool service_runner::restore(std::string backup_file, bool silent) diff --git a/src/utils/execute.cpp b/src/utils/execute.cpp index 7179b9d..d7c847d 100644 --- a/src/utils/execute.cpp +++ b/src/utils/execute.cpp @@ -18,7 +18,7 @@ bool EXITSTATUSCHECK(int ret) { } namespace dropshell { -bool execute_local_command_interactive(const sCommand &command, bool silent) +bool execute_local_command_interactive(const sCommand &command) { if (command.get_command_to_run().empty()) return false; @@ -43,14 +43,12 @@ bool execute_local_command_interactive(const sCommand &command, bool silent) } } -bool execute_local_command_and_capture_output(const sCommand& command, std::string * output, cMode mode) +bool execute_local_command_and_capture_output(const sCommand& command, std::string * output) { ASSERT(output != nullptr, "Output string must be provided"); - ASSERT(is_raw(mode), "Capture output mode requires raw command mode"); - ASSERT(!hasFlag(mode, cMode::Silent), "Silent mode is not allowed with capture output mode"); if (command.get_command_to_run().empty()) return false; - cStyle style = getStyle(mode); + cStyle style = cStyle::Raw; std::string full_cmd = command.construct_cmd(style) + " 2>&1"; FILE *pipe = popen(full_cmd.c_str(), "r"); if (!pipe) { @@ -64,16 +62,16 @@ bool execute_local_command_and_capture_output(const sCommand& command, std::stri return EXITSTATUSCHECK(ret); } - - -bool execute_local_command(const sCommand & command, cMode mode, std::string * output /* = nullptr */) +bool execute_local_command(const sCommand & command, std::string * output /* = nullptr */, cMode mode /* = cMode::None */) { + ASSERT(hasFlag(mode, cMode::RawCommand), "Raw command mode is required for local command execution"); + if (hasFlag(mode, cMode::Interactive)) { ASSERT(! hasFlag(mode, cMode::CaptureOutput), "Interactive mode and capture output mode cannot be used together"); ASSERT(output == nullptr, "Interactive mode and an output string cannot be used together"); ASSERT(is_raw(mode), "Interactive mode requires raw command mode"); - return execute_local_command_interactive(command, hasFlag(mode, cMode::Silent)); + return execute_local_command_interactive(command); } if (hasFlag(mode, cMode::CaptureOutput)) { @@ -81,13 +79,14 @@ bool execute_local_command(const sCommand & command, cMode mode, std::string * o ASSERT(is_raw(mode), "Capture output mode requires raw command mode"); ASSERT(!hasFlag(mode, cMode::Silent), "Silent mode is not allowed with capture output mode"); - return execute_local_command_and_capture_output(command, output, mode); + return execute_local_command_and_capture_output(command, output); } if (command.get_command_to_run().empty()) return false; - cStyle style = getStyle(mode); - std::string full_cmd = command.construct_cmd(style) + " 2>&1" + (hasFlag(mode, cMode::Silent) ? " > /dev/null" : ""); + cStyle style = cStyle::Raw; + bool silent = hasFlag(mode, cMode::Silent); + std::string full_cmd = command.construct_cmd(style) + " 2>&1" + (silent ? " > /dev/null" : ""); int ret = system(full_cmd.c_str()); bool ok = EXITSTATUSCHECK(ret); @@ -115,8 +114,8 @@ bool execute_ssh_command(const sSSHInfo &ssh_info, const sCommand &command, cMod cmdstr = "bash -c " + halfquote(cmdstr); sCommand ssh_command(ssh_cmd.str() + " " + cmdstr); - cMode localmode = mode - cMode::Silent + cMode::RawCommand; - bool rval = execute_local_command(ssh_command, localmode, output); + cMode localmode = mode + cMode::RawCommand; + bool rval = execute_local_command(ssh_command, output, localmode); if (!rval) { std::cerr <