Your Name b5bc7b611d
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 21s
Runner.
2025-05-10 12:43:25 +12:00

98 lines
3.2 KiB
C++

#ifndef DROPSHELL_RUNNER_H
#define DROPSHELL_RUNNER_H
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
#include <map>
namespace dropshell {
/**
* Runner library for executing commands locally or remotely via SSH
*/
class Runner {
public:
/**
* Execute a command according to the specification in the JSON
* @param run_json JSON specification for the command
* @return true if command executed successfully, false otherwise
*/
static bool run(const nlohmann::json& run_json);
/**
* Execute a command and capture its output
* @param run_json JSON specification for the command
* @param output Reference to string where output will be stored
* @return true if command executed successfully, false otherwise
*/
static bool run(const nlohmann::json& run_json, std::string& output);
private:
/**
* Execute a command locally
* @param command Command to execute
* @param args Command arguments
* @param env Environment variables
* @param silent Whether to suppress output
* @param interactive Whether to enable interactive mode
* @param output Reference to string where output will be stored (if capturing)
* @param capture_output Whether to capture output
* @return exit code of the command, or -1 if execution failed
*/
static int execute_local(
const std::string& command,
const std::vector<std::string>& args,
const std::map<std::string, std::string>& env,
bool silent,
bool interactive,
std::string* output = nullptr,
bool capture_output = false
);
/**
* Execute a command remotely via SSH
* @param ssh_config SSH configuration
* @param command Command to execute
* @param args Command arguments
* @param env Environment variables
* @param silent Whether to suppress output
* @param interactive Whether to enable interactive mode
* @param output Reference to string where output will be stored (if capturing)
* @param capture_output Whether to capture output
* @return exit code of the command, or -1 if execution failed
*/
static int execute_ssh(
const nlohmann::json& ssh_config,
const std::string& command,
const std::vector<std::string>& args,
const std::map<std::string, std::string>& env,
bool silent,
bool interactive,
std::string* output = nullptr,
bool capture_output = false
);
/**
* Parse command specification from JSON
* @param run_json JSON specification
* @param command Output parameter for command
* @param args Output parameter for command arguments
* @param env Output parameter for environment variables
* @param silent Output parameter for silent option
* @param interactive Output parameter for interactive option
* @return true if parsing was successful, false otherwise
*/
static bool parse_json(
const nlohmann::json& run_json,
std::string& command,
std::vector<std::string>& args,
std::map<std::string, std::string>& env,
bool& silent,
bool& interactive
);
};
} // namespace dropshell
#endif // DROPSHELL_RUNNER_H