forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_command.h
More file actions
62 lines (57 loc) · 1.8 KB
/
run_command.h
File metadata and controls
62 lines (57 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef NODEGIT_RUN_CMD_H
#define NODEGIT_RUN_CMD_H
#include <iostream>
#include <cstdio>
#include <iostream>
#include <stdexcept>
#include <string>
#include <array>
#include "cmd.h"
namespace nodegit {
// TODO:
// This is just a first testing version to run commands in MacOS, Linux and Win32
namespace runcommand {
/**
* \brief Executes a command.
*
* \param cmd object storing all the information needed to execute a command. Result
* of the execution will also be stored here.
* \return true if command execution succeeded; false otherwise.
*/
bool exec(Cmd *cmd) {
// TODO: sanitize path
assert (cmd != nullptr);
std::string cwd = cmd->GetEnv(Cmd::Env::kCWD);
std::string cmdCwd = cwd.empty() ? std::string("") : std::string("cd ").append(cwd).append(" && ");
std::string cmdArgs = cmd->Args();
std::string finalCmdArgs = cmdArgs.empty() ? std::string("") : std::string(" ").append(cmdArgs);
std::string cmdRedirectErr = cmd->GetRedirectStdErr() ? " 2>&1" : std::string("");
std::string finalCmd = cmdCwd + cmd->Command() + finalCmdArgs + cmdRedirectErr;
#ifdef WIN32
auto pipe = _popen(finalCmd.c_str(), "r");
#else
auto pipe = popen(finalCmd.c_str(), "r");
#endif
if (!pipe) {
cmd->errorMsg = "popen() failed!";
return false;
}
std::array<char, 128> buffer {};
while (!feof(pipe)) {
if (fgets(buffer.data(), 128, pipe) != nullptr)
cmd->out += buffer.data();
}
#ifdef WIN32
auto rc = _pclose(pipe);
#else
auto rc = pclose(pipe);
#endif
if (rc != EXIT_SUCCESS) {
cmd->errorMsg = "pclose() failed!";
return false;
}
return true;
}
} // namespace runcommand
} // namespace nodegit
#endif // NODEGIT_RUN_CMD_H