This commit is contained in:
@@ -17,18 +17,19 @@ namespace dropshell {
|
||||
bool Runner::run(const nlohmann::json& run_json) {
|
||||
std::string command;
|
||||
std::vector<std::string> args;
|
||||
std::string working_dir;
|
||||
std::map<std::string, std::string> env;
|
||||
bool silent, interactive;
|
||||
|
||||
if (!parse_json(run_json, command, args, env, silent, interactive)) {
|
||||
if (!parse_json(run_json, command, args, working_dir, env, silent, interactive)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int exit_code;
|
||||
if (run_json.contains("ssh")) {
|
||||
exit_code = execute_ssh(run_json["ssh"], command, args, env, silent, interactive);
|
||||
exit_code = execute_ssh(run_json["ssh"], command, args, working_dir, env, silent, interactive);
|
||||
} else {
|
||||
exit_code = execute_local(command, args, env, silent, interactive);
|
||||
exit_code = execute_local(command, args, working_dir, env, silent, interactive);
|
||||
}
|
||||
|
||||
return exit_code == 0;
|
||||
@@ -37,18 +38,19 @@ bool Runner::run(const nlohmann::json& run_json) {
|
||||
bool Runner::run(const nlohmann::json& run_json, std::string& output) {
|
||||
std::string command;
|
||||
std::vector<std::string> args;
|
||||
std::string working_dir;
|
||||
std::map<std::string, std::string> env;
|
||||
bool silent, interactive;
|
||||
|
||||
if (!parse_json(run_json, command, args, env, silent, interactive)) {
|
||||
if (!parse_json(run_json, command, args, working_dir, env, silent, interactive)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int exit_code;
|
||||
if (run_json.contains("ssh")) {
|
||||
exit_code = execute_ssh(run_json["ssh"], command, args, env, silent, interactive, &output, true);
|
||||
exit_code = execute_ssh(run_json["ssh"], command, args, working_dir, env, silent, interactive, &output, true);
|
||||
} else {
|
||||
exit_code = execute_local(command, args, env, silent, interactive, &output, true);
|
||||
exit_code = execute_local(command, args, working_dir, env, silent, interactive, &output, true);
|
||||
}
|
||||
|
||||
return exit_code == 0;
|
||||
@@ -58,6 +60,7 @@ bool Runner::parse_json(
|
||||
const nlohmann::json& run_json,
|
||||
std::string& command,
|
||||
std::vector<std::string>& args,
|
||||
std::string& working_dir,
|
||||
std::map<std::string, std::string>& env,
|
||||
bool& silent,
|
||||
bool& interactive
|
||||
@@ -86,6 +89,16 @@ bool Runner::parse_json(
|
||||
}
|
||||
}
|
||||
|
||||
// Working directory is optional
|
||||
working_dir = "";
|
||||
if (run_json.contains("working_directory")) {
|
||||
if (!run_json["working_directory"].is_string()) {
|
||||
std::cerr << "Error: 'working_directory' field must be a string" << std::endl;
|
||||
return false;
|
||||
}
|
||||
working_dir = run_json["working_directory"];
|
||||
}
|
||||
|
||||
// Environment variables are optional
|
||||
env.clear();
|
||||
if (run_json.contains("env")) {
|
||||
@@ -138,6 +151,7 @@ bool Runner::parse_json(
|
||||
int Runner::execute_local(
|
||||
const std::string& command,
|
||||
const std::vector<std::string>& args,
|
||||
const std::string& working_dir,
|
||||
const std::map<std::string, std::string>& env,
|
||||
bool silent,
|
||||
bool interactive,
|
||||
@@ -178,6 +192,14 @@ int Runner::execute_local(
|
||||
}
|
||||
}
|
||||
|
||||
// Change to working directory if specified
|
||||
if (!working_dir.empty()) {
|
||||
if (chdir(working_dir.c_str()) != 0) {
|
||||
std::cerr << "Error changing to directory " << working_dir << ": " << strerror(errno) << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Set environment variables
|
||||
for (const auto& [key, value] : env) {
|
||||
setenv(key.c_str(), value.c_str(), 1);
|
||||
@@ -267,6 +289,7 @@ int Runner::execute_ssh(
|
||||
const nlohmann::json& ssh_config,
|
||||
const std::string& command,
|
||||
const std::vector<std::string>& args,
|
||||
const std::string& working_dir,
|
||||
const std::map<std::string, std::string>& env,
|
||||
bool silent,
|
||||
bool interactive,
|
||||
@@ -369,6 +392,11 @@ int Runner::execute_ssh(
|
||||
cmd_stream << "export " << key << "=\"" << value << "\"; ";
|
||||
}
|
||||
|
||||
// Add cd command if working_directory is specified
|
||||
if (!working_dir.empty()) {
|
||||
cmd_stream << "cd \"" << working_dir << "\" && ";
|
||||
}
|
||||
|
||||
// Add command and args
|
||||
cmd_stream << command;
|
||||
for (const auto& arg : args) {
|
||||
|
Reference in New Issue
Block a user