diff --git a/runner/runner.cpp b/runner/runner.cpp index efa57de..c64b4ba 100644 --- a/runner/runner.cpp +++ b/runner/runner.cpp @@ -9,11 +9,38 @@ #include #include #include +#include +#include namespace runner { namespace { +// String trimming functions +void ltrim(std::string& s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { + return !std::isspace(ch); + })); +} + +void rtrim(std::string& s) { + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { + return !std::isspace(ch); + }).base(), s.end()); +} + +void trim(std::string& s) { + ltrim(s); + rtrim(s); +} + +// Safe version that handles nullptr +void trim(std::string* s) { + if (s) { + trim(*s); + } +} + // Add working_dir to the forward declaration ssh_session ssh_connect_and_auth(const sSSHInfo* sshinfo, const std::map& env, std::string* error); std::string ssh_build_remote_command(const std::string& command, const std::vector& args, const std::string& working_dir, const std::map& env); @@ -192,9 +219,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string std::string final_cmd = cmd_with_env.str(); - // Debug: Show the command being executed - std::cerr << "SSH exec command: " << final_cmd << std::endl; - int rc = ssh_channel_request_exec(channel, final_cmd.c_str()); if (rc != SSH_OK) { std::string error = std::string("Failed to exec remote command: ") + ssh_get_error(session); @@ -210,7 +234,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string // Read from stdout while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) { - std::cerr << "Read " << nbytes << " bytes from stdout" << std::endl; oss.write(buffer, nbytes); } if (nbytes < 0) { @@ -219,7 +242,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string // Read from stderr while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 1)) > 0) { - std::cerr << "Read " << nbytes << " bytes from stderr" << std::endl; oss.write(buffer, nbytes); } if (nbytes < 0) { @@ -233,7 +255,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string // Read from stdout while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) { - std::cerr << "Read " << nbytes << " bytes from stdout (writing to fd 1)" << std::endl; write(1, buffer, nbytes); } if (nbytes < 0) { @@ -242,7 +263,6 @@ int ssh_exec_command(ssh_session session, ssh_channel channel, const std::string // Read from stderr while ((nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 1)) > 0) { - std::cerr << "Read " << nbytes << " bytes from stderr (writing to fd 2)" << std::endl; write(2, buffer, nbytes); } if (nbytes < 0) { @@ -378,9 +398,12 @@ int execute_cmd( ssh_channel_free(channel); ssh_disconnect(session); ssh_free(session); + if (output) trim(output); return ret; } else { - return local_execute_cmd(command, args, working_dir, env, silent, interactive, output); + int ret=local_execute_cmd(command, args, working_dir, env, silent, interactive, output); + if (output) trim(output); + return ret; } } diff --git a/runner/runner_demo.cpp b/runner/runner_demo.cpp index fa39d86..ef56caf 100644 --- a/runner/runner_demo.cpp +++ b/runner/runner_demo.cpp @@ -11,6 +11,16 @@ #include #include +// Simple testing framework +#define TESTRESULT(condition, description) \ + do { \ + std::cout << (condition ? "\033[32m[PASS]\033[0m " : "\033[31m[FAIL]\033[0m ") << description; \ + if (!(condition)) { \ + std::cout << " - Expected condition to be true"; \ + } \ + std::cout << std::endl; \ + } while(0) + // using json = nlohmann::json; // std::string base64_decode(const std::string& encoded) { @@ -82,20 +92,22 @@ void test_ssh() { std::vector args = {"-c", "ls -l && echo \"$WHATSUP\""}; std::string working_dir = "/home"; std::map env = {{"WHATSUP", "Waaaaattttsssuuuppppp!"}}; - bool silent = false; + bool silent = true; bool interactive = false; runner::sSSHInfo ssh; ssh.host = "10.10.10.13"; ssh.user = "katie"; ssh.port = "22"; - runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh); + std::string output; + int ret = runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh, &output); + TESTRESULT(ret == 0, "SSH test (ls and echo)."); } void test_env() { std::string command = "bash"; // Simplify the command to avoid nested quoting issues - std::vector args = {"-c", "env | grep WHATSUP && echo The value is: $WHATSUP"}; + std::vector args = {"-c", "echo $WHATSUP"}; std::string working_dir = "/home"; std::map env = {{"WHATSUP", "Waaaaattttsssuuuppppp!"}}; bool silent = false; @@ -104,27 +116,17 @@ void test_env() { ssh.host = "10.10.10.13"; ssh.user = "katie"; ssh.port = "22"; - - std::cerr << "Running test_env with SSH to " << ssh.user << "@" << ssh.host << std::endl; // Capture output explicitly std::string output; int ret = runner::execute_cmd(command, args, working_dir, env, silent, interactive, &ssh, &output); - - std::cerr << "Command returned code: " << ret << std::endl; - std::cerr << "Output length: " << output.length() << " bytes" << std::endl; - - // Print the actual output - std::cout << "=== BEGIN OUTPUT ===" << std::endl; - std::cout << output; - if (!output.empty() && output[output.length()-1] != '\n') { - std::cout << std::endl; - } - std::cout << "=== END OUTPUT ===" << std::endl; + + TESTRESULT(output == "Waaaaattttsssuuuppppp!","Whatsupppp test (remote env)."); } int main(int argc, char* argv[]) { + test_ssh(); test_env(); }