blob: 1f62f78fff6d6ff26c3f9e051de0de112856073c [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átb79f3ca2020-02-04 15:56:01 +010016#if defined(SYSREPO_CLI)
Václav Kubernát6415b822018-08-22 17:40:01 +020017#include "sysrepo_access.hpp"
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010018#define PROGRAM_NAME "sysrepo-cli"
Václav Kubernát715c85c2020-04-14 01:46:08 +020019static const auto usage = R"(CLI interface to sysrepo
20
21Usage:
22 sysrepo-cli [-d <datastore>]
23 sysrepo-cli (-h | --help)
24 sysrepo-cli --version
25
26Options:
27 -d <datastore> can be "running" or "startup" [default: running])";
Václav Kubernát74487df2020-06-04 01:29:28 +020028#elif defined(YANG_CLI)
29#include <boost/spirit/home/x3.hpp>
Václav Kubernát619e6542020-06-29 14:13:43 +020030#include <filesystem>
Václav Kubernát74487df2020-06-04 01:29:28 +020031#include "yang_access.hpp"
32#define PROGRAM_NAME "yang-cli"
33static const auto usage = R"(CLI interface for creating local YANG data instances
34
Václav Kubernát619e6542020-06-29 14:13:43 +020035 The <schema_file_or_module_name> argument is treated as a file name if a file
36 with such a path exists, otherwise it's treated as a module name. Search dirs
37 will be used to find a schema for that module.
38
Václav Kubernát74487df2020-06-04 01:29:28 +020039Usage:
Václav Kubernát28cf3362020-06-29 17:52:51 +020040 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 +020041 yang-cli (-h | --help)
42 yang-cli --version
43
44Options:
45 -s <search_dir> Set search for schema lookup
Václav Kubernát548cb192020-06-26 14:00:42 +020046 -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 +020047 -i <data_file> File to import data from
48 --configonly Disable editing of operational data)";
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010049#else
50#error "Unknown CLI backend"
51#endif
Jan Kundrátdc2b0722018-03-02 14:13:37 +010052
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010053const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history";
54
Jan Kundrátdc2b0722018-03-02 14:13:37 +010055int main(int argc, char* argv[])
56{
57 auto args = docopt::docopt(usage,
58 {argv + 1, argv + argc},
59 true,
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010060 PROGRAM_NAME " " NETCONF_CLI_VERSION,
Jan Kundrátdc2b0722018-03-02 14:13:37 +010061 true);
Václav Kubernát28cf3362020-06-29 17:52:51 +020062 WritableOps writableOps = WritableOps::No;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020063
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010064#if defined(SYSREPO_CLI)
Václav Kubernát715c85c2020-04-14 01:46:08 +020065 auto datastoreType = Datastore::Running;
66 if (const auto& ds = args["-d"]) {
67 if (ds.asString() == "startup") {
68 datastoreType = Datastore::Startup;
69 } else if (ds.asString() == "running") {
70 datastoreType = Datastore::Running;
71 } else {
72 std::cerr << PROGRAM_NAME << ": unknown datastore: " << ds.asString() << "\n";
73 return 1;
74 }
75 }
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020076 auto datastore = std::make_shared<SysrepoAccess>(PROGRAM_NAME, datastoreType);
Václav Kubernát715c85c2020-04-14 01:46:08 +020077 std::cout << "Connected to sysrepo [datastore: " << (datastoreType == Datastore::Startup ? "startup" : "running") << "]" << std::endl;
Václav Kubernát74487df2020-06-04 01:29:28 +020078#elif defined(YANG_CLI)
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020079 auto datastore = std::make_shared<YangAccess>();
Václav Kubernát28cf3362020-06-29 17:52:51 +020080 if (args["--configonly"].asBool()) {
81 writableOps = WritableOps::No;
82 } else {
83 writableOps = WritableOps::Yes;
84 std::cout << "ops is writable" << std::endl;
85 }
Václav Kubernát74487df2020-06-04 01:29:28 +020086 if (const auto& search_dir = args["-s"]) {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020087 datastore->addSchemaDir(search_dir.asString());
Václav Kubernát74487df2020-06-04 01:29:28 +020088 }
Václav Kubernát619e6542020-06-29 14:13:43 +020089 for (const auto& schemaFile : args["<schema_file_or_module_name>"].asStringList()) {
90 if (std::filesystem::exists(schemaFile)) {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020091 datastore->addSchemaFile(schemaFile);
Václav Kubernát619e6542020-06-29 14:13:43 +020092 } else if (schemaFile.find('/') == std::string::npos) { // Module names cannot have a slash
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020093 datastore->loadModule(schemaFile);
Václav Kubernát619e6542020-06-29 14:13:43 +020094 } else {
95 std::cerr << "Cannot load YANG module " << schemaFile << "\n";
96 }
Václav Kubernát74487df2020-06-04 01:29:28 +020097 }
98 if (const auto& enableFeatures = args["-e"]) {
99 namespace x3 = boost::spirit::x3;
100 auto grammar = +(x3::char_-":") >> ":" >> +(x3::char_-":");
101 for (const auto& enableFeature : enableFeatures.asStringList()) {
102 std::pair<std::string, std::string> parsed;
103 auto it = enableFeature.begin();
104 auto res = x3::parse(it, enableFeature.cend(), grammar, parsed);
105 if (!res || it != enableFeature.cend()) {
106 std::cerr << "Error parsing feature enable flags: " << enableFeature << "\n";
107 return 1;
108 }
109 try {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200110 datastore->enableFeature(parsed.first, parsed.second);
Václav Kubernát74487df2020-06-04 01:29:28 +0200111 } catch (std::runtime_error& ex) {
112 std::cerr << ex.what() << "\n";
113 return 1;
114 }
115
116 }
117 }
Václav Kubernát548cb192020-06-26 14:00:42 +0200118 if (const auto& dataFiles = args["-i"]) {
119 for (const auto& dataFile : dataFiles.asStringList()) {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200120 datastore->addDataFile(dataFile);
Václav Kubernát548cb192020-06-26 14:00:42 +0200121 }
122 }
Václav Kubernátb79f3ca2020-02-04 15:56:01 +0100123#else
124#error "Unknown CLI backend"
125#endif
126
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200127 ProxyDatastore proxyDatastore(datastore);
128 auto dataQuery = std::make_shared<DataQuery>(*datastore);
129 Parser parser(datastore->schema(), writableOps, dataQuery);
Václav Kubernát395d92c2020-01-24 12:18:18 +0100130
131 using replxx::Replxx;
132
133 Replxx lineEditor;
134
Václav Kubernát48637292020-07-08 16:54:13 +0200135 lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), [&lineEditor] (const auto& code) {
136 return lineEditor.invoke(Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, code);
137 });
138 lineEditor.bind_key(Replxx::KEY::control('W'), [&lineEditor] (const auto& code) {
139 return lineEditor.invoke(Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, code);
140 });
Václav Kubernát395d92c2020-01-24 12:18:18 +0100141
142 lineEditor.set_word_break_characters("\t _[]/:'\"=-%");
143
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100144 lineEditor.set_completion_callback([&parser](const std::string& input, int& context) {
Václav Kubernáta395d332019-02-13 16:49:20 +0100145 std::stringstream stream;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100146 auto completions = parser.completeCommand(input, stream);
Václav Kubernáta395d332019-02-13 16:49:20 +0100147
Jan Kundrát8d8efe82019-10-18 10:21:36 +0200148 std::vector<replxx::Replxx::Completion> res;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100149 std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res));
150 context = completions.m_contextLength;
Václav Kubernáta395d332019-02-13 16:49:20 +0100151 return res;
152 });
Václav Kubernát2b684612018-08-09 18:55:24 +0200153
Václav Kubernát435706e2019-02-20 18:05:59 +0100154 std::optional<std::string> historyFile;
155 if (auto xdgHome = getenv("XDG_DATA_HOME")) {
156 historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME;
157 } else if (auto home = getenv("HOME")) {
158 historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME;
159 }
160
Václav Kubernát3a433232020-07-08 17:52:50 +0200161 if (historyFile) {
Václav Kubernát435706e2019-02-20 18:05:59 +0100162 lineEditor.history_load(historyFile.value());
Václav Kubernát3a433232020-07-08 17:52:50 +0200163 }
Václav Kubernát435706e2019-02-20 18:05:59 +0100164
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200165 while (true) {
Václav Kubernáta395d332019-02-13 16:49:20 +0100166 auto line = lineEditor.input(parser.currentNode() + "> ");
167 if (!line) {
Václav Kubernát82bf1312019-11-05 11:19:26 +0100168 // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN.
169 // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program.
170 // I have no idea why replxx uses errno for this.
171 if (errno == EAGAIN) {
172 continue;
173 } else {
174 break;
175 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100176 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200177
Jan Kundráte3877022018-09-05 15:32:09 +0200178 std::locale C_locale("C");
Václav Kubernáta395d332019-02-13 16:49:20 +0100179 std::string_view view{line};
180 if (std::all_of(view.begin(), view.end(),
Jan Kundráte3877022018-09-05 15:32:09 +0200181 [C_locale](const auto c) { return std::isspace(c, C_locale);})) {
182 continue;
183 }
184
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200185 try {
Václav Kubernát5b80e522019-01-25 12:17:03 +0100186 command_ cmd = parser.parseCommand(line, std::cout);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200187 boost::apply_visitor(Interpreter(parser, proxyDatastore), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200188 } catch (InvalidCommandException& ex) {
189 std::cerr << ex.what() << std::endl;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200190 } catch (DatastoreException& ex) {
191 std::cerr << ex.what() << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200192 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100193
Václav Kubernáta395d332019-02-13 16:49:20 +0100194 lineEditor.history_add(line);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200195 }
196
Václav Kubernát3a433232020-07-08 17:52:50 +0200197 if (historyFile) {
Václav Kubernát435706e2019-02-20 18:05:59 +0100198 lineEditor.history_save(historyFile.value());
Václav Kubernát3a433232020-07-08 17:52:50 +0200199 }
Václav Kubernát435706e2019-02-20 18:05:59 +0100200
Jan Kundrátdc2b0722018-03-02 14:13:37 +0100201 return 0;
202}