-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandRunner.hpp
More file actions
81 lines (67 loc) · 2.66 KB
/
Copy pathCommandRunner.hpp
File metadata and controls
81 lines (67 loc) · 2.66 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
** EPITECH PROJECT, 2023
** Raytracer
** File description:
** CommandRunner
*/
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <sstream>
#ifndef COMMANDRUNNER_HPP_
#define COMMANDRUNNER_HPP_
class CommandRunner {
public:
class RunError {
public:
class CommandNotFound : public std::exception {
public:
CommandNotFound(const std::string &cmd) : _message("Cannot find command " + cmd) {}
const char *what() const noexcept override {return _message.c_str(); }
private:
std::string _message;
};
class CommandFailed : public std::exception {
public:
CommandFailed(const std::string &cmd) : _message("Command " + cmd + " failed.") {}
const char *what() const noexcept override {return _message.c_str(); }
private:
std::string _message;
};
};
static std::string run(std::string cmd) {
std::string result;
char buffer[128];
std::string command = getCommand(cmd);
if (!isCommandExist(command))
throw CommandRunner::RunError::CommandNotFound(command);
std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe)
throw CommandRunner::RunError::CommandFailed(command);
while (true) {
size_t n = std::fread(buffer, sizeof(char), sizeof(buffer), pipe.get());
if (n > 0) {
result.append(buffer, n);
} else if (std::feof(pipe.get())) {
break;
} else if (std::ferror(pipe.get())) {
throw CommandRunner::RunError::CommandFailed(command);
}
}
return result;
}
static bool isCommandExist(const std::string &cmd) {
std::string command = "command -v " + cmd + " >/dev/null 2>&1";
return std::system(command.c_str()) == 0;
}
private:
static std::string getCommand(const std::string &cmd) {
std::istringstream stream(cmd);
std::string result;
stream >> std::ws >> result;
return result;
}
};
#endif /* !COMMANDRUNNER_HPP_ */