Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
287 lines (269 loc) · 11.4 KB
/
Copy pathmain.cpp
File metadata and controls
287 lines (269 loc) · 11.4 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// mtest: the engine's command line (docs/commands.md). The commands and their options are the
// catalog's (catalog()): each command is a subcommand and each of its parameters an option, so the
// CLI, the service and the MCP tools take the same parameters and give one answer (dispatch()).
// `--json` prints the data instead of the text.
import std;
import mcpplibs.cmdline;
import mcpplibs.mtest.engine.model;
import mcpplibs.mtest.engine.commands;
import mcpplibs.mtest.engine.service;
namespace cmdline = mcpplibs::cmdline;
using namespace mcpplibs::mtest::engine;
namespace {
// The parameter a command's first word gives; the words after it are programs (a selection's)
// or, for `ci`, pull requests.
constexpr std::array<std::pair<std::string_view, std::string_view>, 7> first_word = {{
{"quarantine", "action"},
{"store", "action"},
{"coverage", "action"},
{"analyze", "metric"},
{"ci", "action"},
{"explain", "case"},
{"history", "case"},
}};
// A command of the command line that is another's with one parameter given.
struct shorthand {
std::string_view name, of, key, value;
};
constexpr std::array<shorthand, 2> shorthands = {{
{"hot", "analyze", "metric", "hot"},
{"path", "analyze", "metric", "path"},
}};
// Parameters whose value on the command line is a command line, split into words.
constexpr std::array<std::string_view, 1> command_lines = {"run_under"};
std::string option_of(std::string key) {
std::ranges::replace(key, '_', '-');
return key;
}
// The option of a property inside an object parameter (`report.junit`): its own name, unless that
// is the global `--json`, which becomes `--report-json`.
std::string nested_option(std::string_view parent, std::string_view key) {
return key == "json" ? option_of(std::format("{}_{}", parent, key))
: option_of(std::string(key));
}
std::string help_of(const json& schema) {
std::string help = schema.value("description", "");
if (auto e = schema.find("enum"); e != schema.end()) {
std::string values;
for (const auto& v : *e)
values += (values.empty() ? "" : ", ") + v.get<std::string>();
help += (help.empty() ? "" : ": ") + values;
}
return help;
}
void add_option(cmdline::App& app, const std::string& name, const json& schema) {
const auto type = schema.value("type", "string");
auto option = cmdline::Option(name).help(help_of(schema));
if (type != "boolean")
option = option.takes_value().value_name(type == "integer" ? "N" : "VALUE");
if (type == "array" || type == "object")
option = option.multiple();
app = std::move(app).option(std::move(option));
}
cmdline::App command(std::string_view name, const json& tool) {
auto app = cmdline::App(name).description(tool.value("description", ""));
for (const auto& [command, key] : first_word)
if (command == name)
app = std::move(app).arg(cmdline::Arg(key));
const auto& properties = tool["inputSchema"]["properties"];
for (auto it = properties.begin(); it != properties.end(); ++it) {
if (it.key() == "argv")
continue; // the words after `--`
if (auto nested = it.value().find("properties"); nested != it.value().end()) {
for (auto n = nested->begin(); n != nested->end(); ++n)
add_option(app, nested_option(it.key(), n.key()), n.value());
continue;
}
add_option(app, option_of(it.key()), it.value());
}
return app;
}
const json* tool_named(const json& tools, std::string_view name) {
for (const auto& t : tools)
if (t["name"] == name)
return &t;
return nullptr;
}
cmdline::App application(const json& tools) {
auto app = cmdline::App("mtest")
.version("0.8.5")
.description("run tests of any language, and ask about their runs")
.option(cmdline::Option("json").global().help("print the answer's data"));
for (const auto& t : tools)
app = std::move(app).subcommand(command(t["name"].get<std::string>(), t));
for (const auto& s : shorthands)
if (const auto* of = tool_named(tools, s.of))
app = std::move(app).subcommand(
command(s.name, {{"description", std::format("{} {}", s.of, s.value)},
{"inputSchema", (*of)["inputSchema"]}}));
// The step `git bisect run` repeats, with bisect's parameters.
if (const auto* bisect = tool_named(tools, "bisect"))
app = std::move(app).subcommand(
command("bisect-step", {{"description", "one step of bisect (git bisect run)"},
{"inputSchema", (*bisect)["inputSchema"]}}));
app = std::move(app)
.subcommand(
cmdline::App("serve").description("JSON-RPC and MCP on the standard streams"))
.subcommand(cmdline::App("version").description("the engine's version"));
return app;
}
// A value of the command line as its schema's type; a usage error when it is not one.
std::expected<json, std::string> typed(const std::string& option, const json& schema,
const std::string& value) {
const auto type = schema.value("type", "string");
if (type == "integer") {
std::int64_t v = 0;
auto [end, ec] = std::from_chars(value.data(), value.data() + value.size(), v);
if (ec != std::errc{} || end != value.data() + value.size())
return std::unexpected(std::format("--{} takes a number", option));
return v;
}
if (type == "number") {
double v = 0;
auto [end, ec] = std::from_chars(value.data(), value.data() + value.size(), v);
if (ec != std::errc{} || end != value.data() + value.size())
return std::unexpected(std::format("--{} takes a number", option));
return v;
}
if (auto e = schema.find("enum");
e != schema.end() && std::ranges::find(*e, json(value)) == e->end())
return std::unexpected(std::format("--{} takes one of: {}", option, help_of(schema)));
return value;
}
// One parameter from its option, into `params[key]`.
std::expected<void, std::string> take(const cmdline::ParsedArgs& parsed, const std::string& key,
const std::string& option, const json& schema, json& params) {
const auto given = parsed.option_or_empty(option);
if (!given.is_set())
return {};
const auto type = schema.value("type", "string");
if (type == "boolean") {
params[key] = true;
return {};
}
if (type == "object") { // NAME=VALUE pairs
for (const auto& pair : given.values) {
const auto eq = pair.find('=');
if (eq == std::string::npos)
return std::unexpected(std::format("--{} takes NAME=VALUE", option));
params[key][pair.substr(0, eq)] = pair.substr(eq + 1);
}
return {};
}
if (type == "array") {
const bool words = std::ranges::contains(command_lines, key);
const json items = schema.value("items", json::object());
for (const auto& value : given.values) {
if (!words) {
auto v = typed(option, items, value);
if (!v)
return std::unexpected(v.error());
params[key].push_back(*v);
continue;
}
for (auto word : std::views::split(value, ' '))
if (!word.empty())
params[key].push_back(std::string(word.begin(), word.end()));
}
return {};
}
auto v = typed(option, schema, given.value());
if (!v)
return std::unexpected(v.error());
params[key] = *v;
return {};
}
// The parameters of a command from what the command line gave.
std::expected<json, std::string> parameters(std::string_view name, const json& tool,
const cmdline::ParsedArgs& parsed) {
json params = json::object();
const auto& properties = tool["inputSchema"]["properties"];
for (auto it = properties.begin(); it != properties.end(); ++it) {
if (auto nested = it.value().find("properties"); nested != it.value().end()) {
for (auto n = nested->begin(); n != nested->end(); ++n) {
json inner = params.value(it.key(), json::object());
auto r = take(parsed, n.key(), nested_option(it.key(), n.key()), n.value(), inner);
if (!r)
return std::unexpected(r.error());
if (!inner.empty())
params[it.key()] = inner;
}
continue;
}
if (auto r = take(parsed, it.key(), option_of(it.key()), it.value(), params); !r)
return std::unexpected(r.error());
}
std::vector<std::string> words = parsed.positionals;
for (const auto& [command, key] : first_word)
if (command == name && !words.empty()) {
auto v = typed(std::string(key), properties[std::string(key)], words.front());
if (!v)
return std::unexpected(v.error());
params[std::string(key)] = *v;
words.erase(words.begin());
}
for (auto& w : words)
params[name == "ci" ? "prs" : "programs"].push_back(w);
return params;
}
} // namespace
int main(int argc, char** argv) {
// The words after `--` are one program and its arguments, not the command line's.
cmdline::Argv words(argv, argv + argc);
std::vector<std::string> program;
if (auto dash = std::ranges::find(words, "--"); dash != words.end()) {
program.assign(dash + 1, words.end());
words.erase(dash, words.end());
}
const json tools = catalog()["tools"];
const auto app = application(tools);
if (words.size() < 2 || words[1] == "help") {
app.print_help();
return words.size() < 2 ? 2 : 0;
}
auto parsed = app.parse_from(words);
if (!parsed) {
if (parsed.error().is_error()) {
std::println(std::cerr, "mtest: {}", parsed.error().message);
return 2;
}
return 0; // --help or --version, printed
}
if (!parsed->has_subcommand()) {
std::println(std::cerr, "mtest: no command {} (mtest help)", parsed->positional(0));
return 2;
}
const std::string name(parsed->subcommand_name());
const auto& sub = parsed->subcommand()->get();
if (name == "version") {
std::println("mtest 0.8.5");
return 0;
}
const auto cwd = std::filesystem::current_path();
if (name == "serve")
return serve(cwd);
std::string dispatched = name;
json fixed = json::object();
for (const auto& s : shorthands)
if (s.name == name) {
dispatched = s.of;
fixed[std::string(s.key)] = s.value;
}
const json* tool = tool_named(tools, name == "bisect-step" ? "bisect" : dispatched);
auto params = parameters(name, *tool, sub);
if (!params) {
std::println(std::cerr, "mtest: {}", params.error());
return 2;
}
params->update(fixed);
if (!program.empty())
(*params)["argv"] = program;
auto a = dispatch(cwd, name == "bisect-step" ? name : dispatched, *params, {});
if (parsed->is_flag_set("json"))
std::println("{}", dumped(a.data, 2));
else if (a.status == 2)
std::print(std::cerr, "{}", a.text);
else
std::print("{}", a.text);
return a.status;
}