-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli.cpp
More file actions
322 lines (288 loc) · 12 KB
/
cli.cpp
File metadata and controls
322 lines (288 loc) · 12 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
* Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
*
* Written by Václav Kubernát <[email protected]>
*
*/
#include <atomic>
#include <boost/algorithm/string.hpp>
#include <docopt.h>
#include <iostream>
#include <optional>
#include <replxx.hxx>
#include <sstream>
#include "NETCONF_CLI_VERSION.h"
#include "interpreter.hpp"
#include "proxy_datastore.hpp"
#include "yang_schema.hpp"
#if defined(SYSREPO_CLI)
#include "sysrepo_access.hpp"
#include <sysrepo-cpp/utils/utils.hpp>
#define PROGRAM_NAME "sysrepo-cli"
static const auto usage = R"(CLI interface to sysrepo
Usage:
sysrepo-cli [-d <datastore_target>]
sysrepo-cli (-h | --help)
sysrepo-cli --version
Options:
-d <datastore_target> can be "running", "startup" or "operational" [default: operational])";
#elif defined(YANG_CLI)
#include <boost/spirit/home/x3.hpp>
#include <filesystem>
#include "yang_access.hpp"
#define PROGRAM_NAME "yang-cli"
static const auto usage = R"(CLI interface for creating local YANG data instances
The <schema_file_or_module_name> argument is treated as a file name if a file
with such a path exists, otherwise it's treated as a module name. Search dirs
will be used to find a schema for that module.
Usage:
yang-cli [--configonly] [--ignore-unknown-data] [-s <search_dir>]... [-e enable_features]... [-i data_file]... <schema_file_or_module_name>...
yang-cli (-h | --help)
yang-cli --version
Options:
-s <search_dir> Search in these directories for YANG schema files. This option can be supplied more than once.
-e <enable_features> Feature to enable after modules are loaded. This option can be supplied more than once. Format: <module_name>:<feature>
-i <data_file> File to import data from
--configonly Disable editing of operational data
--ignore-unknown-data Silently ignore data with no available schema file)";
#elif defined(NETCONF_CLI)
// FIXME: improve usage
static const auto usage = R"(CLI interface for NETCONF
Usage:
netconf-cli [-v] [-d <datastore_target>] [-p <port>] <host>
netconf-cli [-v] [-d <datastore_target>] --socket <path>
netconf-cli (-h | --help)
netconf-cli --version
Options:
-v enable verbose mode
-p <port> port number [default: 830]
-d <datastore_target> can be "running", "startup" or "operational" [default: operational])";
#include "cli-netconf.hpp"
#include "netconf_access.hpp"
#define PROGRAM_NAME "netconf-cli"
// FIXME: this should be replaced by C++20 std::jthread at some point
struct PoorMansJThread {
~PoorMansJThread()
{
if (thread.joinable()) {
thread.join();
}
}
std::thread thread;
};
#else
#error "Unknown CLI backend"
#endif
const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history";
int main(int argc, char* argv[])
{
auto args = docopt::docopt(usage,
{argv + 1, argv + argc},
true,
PROGRAM_NAME " " NETCONF_CLI_VERSION,
true);
WritableOps writableOps = WritableOps::No;
using replxx::Replxx;
Replxx lineEditor;
std::atomic<int> backendReturnCode = 0;
// For some reason, GCC10 still needs [[maybe_unused]] because of conditional compilation...
[[maybe_unused]] auto datastoreTarget = DatastoreTarget::Operational;
if (const auto& ds = args["-d"]) {
if (ds.asString() == "startup") {
datastoreTarget = DatastoreTarget::Startup;
} else if (ds.asString() == "running") {
datastoreTarget = DatastoreTarget::Running;
} else if (ds.asString() == "operational") {
datastoreTarget = DatastoreTarget::Operational;
} else {
std::cerr << PROGRAM_NAME << ": unknown datastore target: " << ds.asString() << "\n";
return 1;
}
}
auto datastoreTargetString = args["-d"] ? args["-d"].asString() : std::string("operational");
#if defined(SYSREPO_CLI)
sysrepo::setLogLevelStderr(sysrepo::LogLevel::Warning);
auto datastore = std::make_shared<SysrepoAccess>();
std::cout << "Connected to sysrepo [datastore target: " << datastoreTargetString << "]" << std::endl;
#elif defined(YANG_CLI)
auto datastore = std::make_shared<YangAccess>();
if (args["--configonly"].asBool()) {
writableOps = WritableOps::No;
} else {
writableOps = WritableOps::Yes;
std::cout << "ops is writable" << std::endl;
}
for (const auto& dir : args["-s"].asStringList()) {
datastore->addSchemaDir(dir);
}
for (const auto& schemaFile : args["<schema_file_or_module_name>"].asStringList()) {
if (auto path = std::filesystem::canonical(schemaFile); std::filesystem::exists(path)) {
if (std::filesystem::is_regular_file(path)) {
datastore->addSchemaDir(path.parent_path());
}
datastore->addSchemaFile(schemaFile);
} else if (schemaFile.find('/') == std::string::npos) { // Module names cannot have a slash
datastore->loadModule(schemaFile);
} else {
std::cerr << "Cannot load YANG module " << schemaFile << "\n";
}
}
if (const auto& enableFeatures = args["-e"]) {
namespace x3 = boost::spirit::x3;
std::map<std::string, std::vector<std::string>> toEnable;
auto grammar = +(x3::char_-":") >> ":" >> +(x3::char_-":");
for (const auto& enableFeature : enableFeatures.asStringList()) {
std::pair<std::string, std::string> parsed;
auto it = enableFeature.begin();
auto res = x3::parse(it, enableFeature.cend(), grammar, parsed);
if (!res || it != enableFeature.cend()) {
std::cerr << "Error parsing feature enable flags: " << enableFeature << "\n";
return 1;
}
toEnable[parsed.first].emplace_back(parsed.second);
}
try {
for (const auto& [moduleName, features] : toEnable) {
datastore->setEnabledFeatures(moduleName, features);
}
} catch (std::runtime_error& ex) {
std::cerr << ex.what() << "\n";
return 1;
}
}
auto strict = args.at("--ignore-unknown-data").asBool() ? StrictDataParsing::No : StrictDataParsing::Yes;
if (const auto& dataFiles = args["-i"]) {
for (const auto& dataFile : dataFiles.asStringList()) {
datastore->addDataFile(dataFile, strict);
}
}
#elif defined(NETCONF_CLI)
auto verbose = args.at("-v").asBool();
if (verbose) {
NetconfAccess::setNcLogLevel(libnetconf::LogLevel::Debug);
}
SshProcess process;
PoorMansJThread processWatcher;
std::shared_ptr<NetconfAccess> datastore;
if (args.at("--socket").asBool()) {
try {
datastore = std::make_shared<NetconfAccess>(args.at("<path>").asString());
} catch (std::runtime_error& ex) {
std::cerr << "UNIX socket connection failed: " << ex.what() << std::endl;
return 1;
}
} else {
try {
process = sshProcess(args.at("<host>").asString(), args.at("-p").asString());
processWatcher.thread = std::thread{std::thread{[&process, &lineEditor, &backendReturnCode] () {
process.process.wait();
backendReturnCode = process.process.exit_code();
// CTRL-U clears from the cursor to the start of the line
// CTRL-K clears from the cursor to the end of the line
// CTRL-D send EOF
lineEditor.emulate_key_press(replxx::Replxx::KEY::control('U'));
lineEditor.emulate_key_press(replxx::Replxx::KEY::control('K'));
lineEditor.emulate_key_press(replxx::Replxx::KEY::control('D'));
}}};
datastore = std::make_shared<NetconfAccess>(process.std_out.native_source(), process.std_in.native_sink());
} catch (std::runtime_error& ex) {
std::cerr << "SSH connection failed: " << ex.what() << std::endl;
return 1;
}
}
std::cout << "Connected via NETCONF [datastore target: " << datastoreTargetString << "]" << std::endl;
#else
#error "Unknown CLI backend"
#endif
datastore->setTarget(datastoreTarget);
#if defined(SYSREPO_CLI) || defined(NETCONF_CLI)
auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) {
return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema()));
};
#elif defined(YANG_CLI)
auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>&) {
return nullptr;
};
#endif
ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore);
auto dataQuery = std::make_shared<DataQuery>(*datastore);
Parser parser(datastore->schema(), writableOps, dataQuery);
lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), [&lineEditor](const auto& code) {
return lineEditor.invoke(Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, code);
});
lineEditor.bind_key(Replxx::KEY::control('W'), [&lineEditor](const auto& code) {
return lineEditor.invoke(Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, code);
});
lineEditor.set_word_break_characters("\t _[]/:'\"=-%");
lineEditor.set_completion_callback([&parser](const std::string& input, int& context) {
std::stringstream stream;
Completions completions;
try {
completions = parser.completeCommand(input, stream);
} catch (std::exception& ex) {
std::cerr << "Error while completing: " << ex.what() << "\n";
}
std::vector<replxx::Replxx::Completion> res;
std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res));
context = completions.m_contextLength;
return res;
});
std::optional<std::string> historyFile;
if (auto xdgHome = getenv("XDG_DATA_HOME")) {
historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME;
} else if (auto home = getenv("HOME")) {
historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME;
}
if (historyFile) {
lineEditor.history_load(historyFile.value());
}
while (backendReturnCode == 0) {
auto fullContextPath = parser.currentNode();
std::string prompt;
if (auto activeRpcPath = proxyDatastore.inputDatastorePath()) {
auto rpcPrefixLength = activeRpcPath->size();
prompt = "(prepare: " + *activeRpcPath + ") " + fullContextPath.substr(rpcPrefixLength);
} else {
prompt = fullContextPath;
}
prompt += "> ";
auto line = lineEditor.input(prompt);
if (!line) {
// If user pressed CTRL-C to abort the line, errno gets set to EAGAIN.
// If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program.
// I have no idea why replxx uses errno for this.
if (errno == EAGAIN) {
continue;
} else {
break;
}
}
std::locale C_locale("C");
std::string_view view{line};
if (std::all_of(view.begin(), view.end(),
[C_locale](const auto c) { return std::isspace(c, C_locale);})) {
continue;
}
try {
command_ cmd = parser.parseCommand(line, std::cout);
boost::apply_visitor(Interpreter(parser, proxyDatastore), cmd);
if (cmd.type() == typeid(quit_)) {
break;
}
} catch (InvalidCommandException& ex) {
std::cerr << ex.what() << std::endl;
} catch (DatastoreException& ex) {
std::cerr << ex.what() << std::endl;
} catch (std::runtime_error& ex) {
std::cerr << ex.what() << std::endl;
} catch (std::logic_error& ex) {
std::cerr << ex.what() << std::endl;
}
lineEditor.history_add(line);
}
if (historyFile) {
lineEditor.history_save(historyFile.value());
}
return backendReturnCode;
}