-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
265 lines (227 loc) · 7.64 KB
/
main.cpp
File metadata and controls
265 lines (227 loc) · 7.64 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <cstdlib>
#include <format>
#include <iostream>
#include <map>
#include <memory>
#include <pstream.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
struct RunResult {
std::string error;
};
using CommandOptions = std::map<std::string, std::vector<std::string>>;
class Command;
using CommandsShared =
std::unordered_map<std::string, std::shared_ptr<Command>>;
using Commands = std::unordered_map<std::string, std::unique_ptr<Command>>;
class Command {
public:
Command() = default;
Command(const std::vector<std::string> &aliases) {
this->setAliases(aliases);
}
virtual ~Command() = default;
virtual std::string description() const = 0;
virtual std::string help() const { return this->description(); }
virtual RunResult run(const std::vector<std::string> &args) = 0;
// Add a subcommand with a single alias
void addSubcommand(const std::string &alias,
std::unique_ptr<Command> command) {
command->setAliases({alias});
subcommands[alias] = std::move(command);
}
// Add a subcommand with multiple aliases
void addSubcommand(const std::vector<std::string> &aliases,
std::shared_ptr<Command> command) {
command->setAliases(aliases);
for (const auto &alias : aliases) {
subcommands[alias] = command;
}
}
CommandsShared &getSubcommands() { return subcommands; }
std::vector<std::string> getAliases() const { return aliases; }
protected:
void setAliases(const std::vector<std::string> &aliases) {
this->aliases = aliases;
}
std::vector<std::string> aliases;
CommandsShared subcommands;
static CommandOptions readOptions(const std::vector<std::string> &args,
const std::string &prefixKey) {
int firstOptionIndex = -1;
CommandOptions options;
for (int i = 0; i < (int)args.size(); i++) {
std::string arg = args[i];
if (arg[0] == '-') {
if (firstOptionIndex == -1) {
firstOptionIndex = i;
}
if (arg == "--") {
i++;
options["--"] =
std::vector<std::string>(args.begin() + i, args.end());
break;
}
options[arg] = std::vector<std::string>();
i++;
while (i < (int)args.size() && args[i][0] != '-') {
options[arg].push_back(args[i]);
i++;
}
i--;
}
}
if (firstOptionIndex != -1) {
options[prefixKey] = std::vector<std::string>(
args.begin(), args.begin() + firstOptionIndex);
}
return options;
}
static std::vector<std::string>
readOptionValues(const CommandOptions &options,
const std::vector<std::string> &keyAliases) {
std::cout << "a1";
for (const auto &key : keyAliases) {
std::cout << "a_for_" << key;
auto it = options.find(key);
if (it != options.end()) {
std::cout << "a2";
return it->second;
}
}
std::cout << "a3";
return {};
}
};
class RootCommand : public Command {
public:
std::string description() const override { return ""; }
RunResult run(const std::vector<std::string> &args) override {
if (args.size() == 0) {
std::cerr << "No command provided. Use --help for help.\n";
return {"No command provided"};
}
auto command = subcommands.find(args[0]);
if (command != subcommands.end()) {
std::vector<std::string> subArgs(args.begin() + 1, args.end());
return command->second->run(subArgs);
} else {
std::cerr << "Unknown command: " << args[0] << "\n";
return {"Unknown command"};
}
}
};
class VersionCommand : public Command {
public:
std::string description() const override {
return "Prints the version of the program";
}
RunResult run(const std::vector<std::string> &args) override {
(void)args;
std::cout << "0.0.1\n";
return {""};
}
};
class HelpCommand : public Command {
public:
HelpCommand(const CommandsShared &commands) : commands(commands) {}
std::string description() const override { return "Prints the help message"; }
RunResult run(const std::vector<std::string> &args) override {
(void)args;
std::cout << "Package for running apps with environment variables\n";
std::cout << "Available commands:\n";
std::unordered_set<std::shared_ptr<Command>> uniqueCommands;
for (const auto &[key, command] : commands) {
uniqueCommands.insert(command);
}
for (const auto &command : uniqueCommands) {
const auto &aliases = command->getAliases();
std::string aliasesStr = "";
for (const auto &alias : aliases) {
aliasesStr += alias + (&aliases.back() == &alias ? "" : ", ");
}
// std::cout << std::setw(20) << std::right << aliasesStr
// << command->description() << "\n";
std::cout << std::format(" {:<15} | {}\n", aliasesStr,
command->description());
}
return {""};
}
private:
const CommandsShared &commands;
};
class RunProcessCommand : public Command {
public:
std::string description() const override {
return "Runs a shell command with the configured env variables";
}
RunResult run(const std::vector<std::string> &args) override {
CommandOptions options = readOptions(args, "path");
if (!options.contains("path") || options["path"].empty() ||
options["-e"].size() % 2 != 0) {
std::cerr << "Usage: " << this->getAliases()[0]
<< " <path> [-e (<key> <value>)...] [-- <args>...]\n";
return {"Invalid arguments"};
}
std::string exePath = options["path"][0];
std::vector<std::string> exeArgs = options["--"];
std::unordered_map<std::string, std::string> env;
for (int i = 0; i < (int)options["-e"].size(); i += 2) {
env[options["-e"][i]] = options["-e"][i + 1];
}
// ...
// Run the process
bool DEBUG = std::getenv("DEBUG") != nullptr;
if (DEBUG) {
std::cout << "Running " << exePath << " with env:\n";
for (const auto &entry : env) {
std::cout << entry.first << "=" << entry.second << "\n";
}
std::cout << "Args:\n";
for (const auto &arg : exeArgs) {
std::cout << arg << "\n";
}
}
std::string argsStr = "";
for (const auto &arg : exeArgs) {
argsStr += arg + (&exeArgs.back() == &arg ? "" : " ");
}
if (env.size() > 0) {
for (const auto &entry : env) {
setenv(entry.first.c_str(), entry.second.c_str(), 1);
}
}
const std::string shellCmd = exePath + " " + argsStr;
if (DEBUG)
std::cout << "shellCmd: \"" << shellCmd << "\"\n";
// run a process and create a streambuf that reads its stdout and stderr
redi::ipstream proc(shellCmd,
redi::pstreams::pstdout | redi::pstreams::pstderr);
std::string line;
// read child's stdout
while (std::getline(proc.out(), line))
std::cout << (DEBUG ? "stdout: " : "") << line << '\n';
// if reading stdout stopped at EOF then reset the state:
if (proc.eof() && proc.fail())
proc.clear();
// read child's stderr
while (std::getline(proc.err(), line))
std::cerr << (DEBUG ? "stderr: " : "") << line << '\n';
return {""};
}
};
int main(int argc, char const *argv[]) {
RootCommand root;
root.addSubcommand({"-c", "--command"},
std::make_shared<RunProcessCommand>());
root.addSubcommand({"-v", "--version"}, std::make_shared<VersionCommand>());
root.addSubcommand({"-h", "--help"},
std::make_shared<HelpCommand>(root.getSubcommands()));
std::vector<std::string> args(argv + 1, argv + argc);
const auto &result = root.run(args);
if (result.error.size() > 0) {
return 1;
}
return 0;
}