blob: 4f4328a751f9d6b509270541b98d6f491e3a0239 [file] [log] [blame]
Jan Kundrátdc2b0722018-03-02 14:13:37 +01001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
Jan Kundráta2740442018-03-22 16:56:43 +01003 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
Jan Kundrátdc2b0722018-03-02 14:13:37 +01004 *
Václav Kubernát624a8872018-03-02 17:28:47 +01005 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
Jan Kundrátdc2b0722018-03-02 14:13:37 +01006 *
7*/
Jan Kundrátdc2b0722018-03-02 14:13:37 +01008#include <docopt.h>
Václav Kubernát624a8872018-03-02 17:28:47 +01009#include <iostream>
Václav Kubernát435706e2019-02-20 18:05:59 +010010#include <optional>
Václav Kubernáta395d332019-02-13 16:49:20 +010011#include <replxx.hxx>
Václav Kubernát90de9502019-11-20 17:19:44 +010012#include <sstream>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010013#include "NETCONF_CLI_VERSION.h"
Václav Kubernát96344a12018-05-28 16:33:39 +020014#include "interpreter.hpp"
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020015#include "proxy_datastore.hpp"
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010016#include "yang_schema.hpp"
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010017#if defined(SYSREPO_CLI)
Václav Kubernát6415b822018-08-22 17:40:01 +020018#include "sysrepo_access.hpp"
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010019#define PROGRAM_NAME "sysrepo-cli"
Václav Kubernát715c85c2020-04-14 01:46:08 +020020static const auto usage = R"(CLI interface to sysrepo
21
22Usage:
23 sysrepo-cli [-d <datastore>]
24 sysrepo-cli (-h | --help)
25 sysrepo-cli --version
26
27Options:
28 -d <datastore> can be "running" or "startup" [default: running])";
Václav Kubernát74487df2020-06-04 01:29:28 +020029#elif defined(YANG_CLI)
30#include <boost/spirit/home/x3.hpp>
Václav Kubernát619e6542020-06-29 14:13:43 +020031#include <filesystem>
Václav Kubernát74487df2020-06-04 01:29:28 +020032#include "yang_access.hpp"
33#define PROGRAM_NAME "yang-cli"
34static const auto usage = R"(CLI interface for creating local YANG data instances
35
Václav Kubernát619e6542020-06-29 14:13:43 +020036 The <schema_file_or_module_name> argument is treated as a file name if a file
37 with such a path exists, otherwise it's treated as a module name. Search dirs
38 will be used to find a schema for that module.
39
Václav Kubernát74487df2020-06-04 01:29:28 +020040Usage:
Václav Kubernát28cf3362020-06-29 17:52:51 +020041 yang-cli [--configonly] [-s <search_dir>] [-e enable_features]... [-i data_file]... <schema_file_or_module_name>...
Václav Kubernát74487df2020-06-04 01:29:28 +020042 yang-cli (-h | --help)
43 yang-cli --version
44
45Options:
46 -s <search_dir> Set search for schema lookup
Václav Kubernát548cb192020-06-26 14:00:42 +020047 -e <enable_features> Feature to enable after modules are loaded. This option can be supplied more than once. Format: <module_name>:<feature>
Václav Kubernát28cf3362020-06-29 17:52:51 +020048 -i <data_file> File to import data from
49 --configonly Disable editing of operational data)";
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010050#elif defined(NETCONF_CLI)
51// FIXME: improve usage
52static const auto usage = R"(CLI interface for NETCONF
53
54Usage:
55 netconf-cli [-v] [-p <port>] <host>
56 netconf-cli (-h | --help)
57 netconf-cli --version
58
59Options:
60 -v enable verbose mode
61 -p <port> port number [default: 830]
62)";
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010063#include "cli-netconf.hpp"
Václav Kubernátb4e5b182020-11-16 19:55:09 +010064#include "netconf_access.hpp"
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010065#define PROGRAM_NAME "netconf-access"
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010066#else
67#error "Unknown CLI backend"
68#endif
Jan Kundrátdc2b0722018-03-02 14:13:37 +010069
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010070const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history";
71
Jan Kundrátdc2b0722018-03-02 14:13:37 +010072int main(int argc, char* argv[])
73{
74 auto args = docopt::docopt(usage,
75 {argv + 1, argv + argc},
76 true,
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010077 PROGRAM_NAME " " NETCONF_CLI_VERSION,
Jan Kundrátdc2b0722018-03-02 14:13:37 +010078 true);
Václav Kubernát28cf3362020-06-29 17:52:51 +020079 WritableOps writableOps = WritableOps::No;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020080
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010081#if defined(SYSREPO_CLI)
Václav Kubernát715c85c2020-04-14 01:46:08 +020082 auto datastoreType = Datastore::Running;
83 if (const auto& ds = args["-d"]) {
84 if (ds.asString() == "startup") {
85 datastoreType = Datastore::Startup;
86 } else if (ds.asString() == "running") {
87 datastoreType = Datastore::Running;
88 } else {
Václav Kubernátb4e5b182020-11-16 19:55:09 +010089 std::cerr << PROGRAM_NAME << ": unknown datastore: " << ds.asString() << "\n";
Václav Kubernát715c85c2020-04-14 01:46:08 +020090 return 1;
91 }
92 }
Václav Kubernát654303f2020-07-31 13:16:54 +020093 auto datastore = std::make_shared<SysrepoAccess>(datastoreType);
Václav Kubernát715c85c2020-04-14 01:46:08 +020094 std::cout << "Connected to sysrepo [datastore: " << (datastoreType == Datastore::Startup ? "startup" : "running") << "]" << std::endl;
Václav Kubernát74487df2020-06-04 01:29:28 +020095#elif defined(YANG_CLI)
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020096 auto datastore = std::make_shared<YangAccess>();
Václav Kubernát28cf3362020-06-29 17:52:51 +020097 if (args["--configonly"].asBool()) {
98 writableOps = WritableOps::No;
99 } else {
100 writableOps = WritableOps::Yes;
101 std::cout << "ops is writable" << std::endl;
102 }
Václav Kubernát74487df2020-06-04 01:29:28 +0200103 if (const auto& search_dir = args["-s"]) {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200104 datastore->addSchemaDir(search_dir.asString());
Václav Kubernát74487df2020-06-04 01:29:28 +0200105 }
Václav Kubernát619e6542020-06-29 14:13:43 +0200106 for (const auto& schemaFile : args["<schema_file_or_module_name>"].asStringList()) {
107 if (std::filesystem::exists(schemaFile)) {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200108 datastore->addSchemaFile(schemaFile);
Václav Kubernát619e6542020-06-29 14:13:43 +0200109 } else if (schemaFile.find('/') == std::string::npos) { // Module names cannot have a slash
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200110 datastore->loadModule(schemaFile);
Václav Kubernát619e6542020-06-29 14:13:43 +0200111 } else {
112 std::cerr << "Cannot load YANG module " << schemaFile << "\n";
113 }
Václav Kubernát74487df2020-06-04 01:29:28 +0200114 }
115 if (const auto& enableFeatures = args["-e"]) {
116 namespace x3 = boost::spirit::x3;
117 auto grammar = +(x3::char_-":") >> ":" >> +(x3::char_-":");
118 for (const auto& enableFeature : enableFeatures.asStringList()) {
119 std::pair<std::string, std::string> parsed;
120 auto it = enableFeature.begin();
121 auto res = x3::parse(it, enableFeature.cend(), grammar, parsed);
122 if (!res || it != enableFeature.cend()) {
123 std::cerr << "Error parsing feature enable flags: " << enableFeature << "\n";
124 return 1;
125 }
126 try {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200127 datastore->enableFeature(parsed.first, parsed.second);
Václav Kubernát74487df2020-06-04 01:29:28 +0200128 } catch (std::runtime_error& ex) {
129 std::cerr << ex.what() << "\n";
130 return 1;
131 }
Václav Kubernát74487df2020-06-04 01:29:28 +0200132 }
133 }
Václav Kubernát548cb192020-06-26 14:00:42 +0200134 if (const auto& dataFiles = args["-i"]) {
135 for (const auto& dataFile : dataFiles.asStringList()) {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200136 datastore->addDataFile(dataFile);
Václav Kubernát548cb192020-06-26 14:00:42 +0200137 }
138 }
Václav Kubernáte2e15ee2020-02-05 17:38:13 +0100139#elif defined(NETCONF_CLI)
140 auto verbose = args.at("-v").asBool();
141 if (verbose) {
142 NetconfAccess::setNcLogLevel(NC_VERB_DEBUG);
143 }
144
145 SshProcess process;
146 std::shared_ptr<NetconfAccess> datastore;
147
148 try {
149 process = sshProcess(args.at("<host>").asString(), args.at("-p").asString());
150 datastore = std::make_shared<NetconfAccess>(process.std_out.native_source(), process.std_in.native_sink());
151 } catch (std::runtime_error& ex) {
152 std::cerr << "SSH connection failed: " << ex.what() << "\n";
153 return 1;
154 }
Václav Kubernátb79f3ca2020-02-04 15:56:01 +0100155#else
156#error "Unknown CLI backend"
157#endif
158
Václav Kubernáte2e15ee2020-02-05 17:38:13 +0100159#if defined(SYSREPO_CLI) || defined(NETCONF_CLI)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200160 auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) {
161 return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema()));
162 };
163#elif defined(YANG_CLI)
164 auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>&) {
165 return nullptr;
166 };
167#endif
168
169 ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200170 auto dataQuery = std::make_shared<DataQuery>(*datastore);
171 Parser parser(datastore->schema(), writableOps, dataQuery);
Václav Kubernát395d92c2020-01-24 12:18:18 +0100172
173 using replxx::Replxx;
174
175 Replxx lineEditor;
176
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100177 lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), [&lineEditor](const auto& code) {
Václav Kubernát48637292020-07-08 16:54:13 +0200178 return lineEditor.invoke(Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, code);
179 });
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100180 lineEditor.bind_key(Replxx::KEY::control('W'), [&lineEditor](const auto& code) {
Václav Kubernát48637292020-07-08 16:54:13 +0200181 return lineEditor.invoke(Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, code);
182 });
Václav Kubernát395d92c2020-01-24 12:18:18 +0100183
184 lineEditor.set_word_break_characters("\t _[]/:'\"=-%");
185
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100186 lineEditor.set_completion_callback([&parser](const std::string& input, int& context) {
Václav Kubernáta395d332019-02-13 16:49:20 +0100187 std::stringstream stream;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100188 auto completions = parser.completeCommand(input, stream);
Václav Kubernáta395d332019-02-13 16:49:20 +0100189
Jan Kundrát8d8efe82019-10-18 10:21:36 +0200190 std::vector<replxx::Replxx::Completion> res;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100191 std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res));
192 context = completions.m_contextLength;
Václav Kubernáta395d332019-02-13 16:49:20 +0100193 return res;
194 });
Václav Kubernát2b684612018-08-09 18:55:24 +0200195
Václav Kubernát435706e2019-02-20 18:05:59 +0100196 std::optional<std::string> historyFile;
197 if (auto xdgHome = getenv("XDG_DATA_HOME")) {
198 historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME;
199 } else if (auto home = getenv("HOME")) {
200 historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME;
201 }
202
Václav Kubernát3a433232020-07-08 17:52:50 +0200203 if (historyFile) {
Václav Kubernát435706e2019-02-20 18:05:59 +0100204 lineEditor.history_load(historyFile.value());
Václav Kubernát3a433232020-07-08 17:52:50 +0200205 }
Václav Kubernát435706e2019-02-20 18:05:59 +0100206
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200207 while (true) {
Václav Kubernáta395d332019-02-13 16:49:20 +0100208 auto line = lineEditor.input(parser.currentNode() + "> ");
209 if (!line) {
Václav Kubernát82bf1312019-11-05 11:19:26 +0100210 // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN.
211 // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program.
212 // I have no idea why replxx uses errno for this.
213 if (errno == EAGAIN) {
214 continue;
215 } else {
216 break;
217 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100218 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200219
Jan Kundráte3877022018-09-05 15:32:09 +0200220 std::locale C_locale("C");
Václav Kubernáta395d332019-02-13 16:49:20 +0100221 std::string_view view{line};
222 if (std::all_of(view.begin(), view.end(),
Jan Kundráte3877022018-09-05 15:32:09 +0200223 [C_locale](const auto c) { return std::isspace(c, C_locale);})) {
224 continue;
225 }
226
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200227 try {
Václav Kubernát5b80e522019-01-25 12:17:03 +0100228 command_ cmd = parser.parseCommand(line, std::cout);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200229 boost::apply_visitor(Interpreter(parser, proxyDatastore), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200230 } catch (InvalidCommandException& ex) {
231 std::cerr << ex.what() << std::endl;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200232 } catch (DatastoreException& ex) {
233 std::cerr << ex.what() << std::endl;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200234 } catch (std::runtime_error& ex) {
235 std::cerr << ex.what() << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200236 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100237
Václav Kubernáta395d332019-02-13 16:49:20 +0100238 lineEditor.history_add(line);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200239 }
240
Václav Kubernát3a433232020-07-08 17:52:50 +0200241 if (historyFile) {
Václav Kubernát435706e2019-02-20 18:05:59 +0100242 lineEditor.history_save(historyFile.value());
Václav Kubernát3a433232020-07-08 17:52:50 +0200243 }
Václav Kubernát435706e2019-02-20 18:05:59 +0100244
Jan Kundrátdc2b0722018-03-02 14:13:37 +0100245 return 0;
246}