blob: 01e504bef1afe7fc03ccafaaf65e1d87c58e6dc1 [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átb79f3ca2020-02-04 15:56:01 +010015#if defined(SYSREPO_CLI)
Václav Kubernát6415b822018-08-22 17:40:01 +020016#include "sysrepo_access.hpp"
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010017#define PROGRAM_NAME "sysrepo-cli"
Václav Kubernát715c85c2020-04-14 01:46:08 +020018static const auto usage = R"(CLI interface to sysrepo
19
20Usage:
21 sysrepo-cli [-d <datastore>]
22 sysrepo-cli (-h | --help)
23 sysrepo-cli --version
24
25Options:
26 -d <datastore> can be "running" or "startup" [default: running])";
Václav Kubernát74487df2020-06-04 01:29:28 +020027#elif defined(YANG_CLI)
28#include <boost/spirit/home/x3.hpp>
Václav Kubernát619e6542020-06-29 14:13:43 +020029#include <filesystem>
Václav Kubernát74487df2020-06-04 01:29:28 +020030#include "yang_access.hpp"
31#define PROGRAM_NAME "yang-cli"
32static const auto usage = R"(CLI interface for creating local YANG data instances
33
Václav Kubernát619e6542020-06-29 14:13:43 +020034 The <schema_file_or_module_name> argument is treated as a file name if a file
35 with such a path exists, otherwise it's treated as a module name. Search dirs
36 will be used to find a schema for that module.
37
Václav Kubernát74487df2020-06-04 01:29:28 +020038Usage:
Václav Kubernát28cf3362020-06-29 17:52:51 +020039 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 +020040 yang-cli (-h | --help)
41 yang-cli --version
42
43Options:
44 -s <search_dir> Set search for schema lookup
Václav Kubernát548cb192020-06-26 14:00:42 +020045 -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 +020046 -i <data_file> File to import data from
47 --configonly Disable editing of operational data)";
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010048#else
49#error "Unknown CLI backend"
50#endif
Jan Kundrátdc2b0722018-03-02 14:13:37 +010051
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010052const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history";
53
Jan Kundrátdc2b0722018-03-02 14:13:37 +010054int main(int argc, char* argv[])
55{
56 auto args = docopt::docopt(usage,
57 {argv + 1, argv + argc},
58 true,
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010059 PROGRAM_NAME " " NETCONF_CLI_VERSION,
Jan Kundrátdc2b0722018-03-02 14:13:37 +010060 true);
Václav Kubernát28cf3362020-06-29 17:52:51 +020061 WritableOps writableOps = WritableOps::No;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020062
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010063#if defined(SYSREPO_CLI)
Václav Kubernát715c85c2020-04-14 01:46:08 +020064 auto datastoreType = Datastore::Running;
65 if (const auto& ds = args["-d"]) {
66 if (ds.asString() == "startup") {
67 datastoreType = Datastore::Startup;
68 } else if (ds.asString() == "running") {
69 datastoreType = Datastore::Running;
70 } else {
71 std::cerr << PROGRAM_NAME << ": unknown datastore: " << ds.asString() << "\n";
72 return 1;
73 }
74 }
75 SysrepoAccess datastore(PROGRAM_NAME, datastoreType);
76 std::cout << "Connected to sysrepo [datastore: " << (datastoreType == Datastore::Startup ? "startup" : "running") << "]" << std::endl;
Václav Kubernát74487df2020-06-04 01:29:28 +020077#elif defined(YANG_CLI)
78 YangAccess datastore;
Václav Kubernát28cf3362020-06-29 17:52:51 +020079 if (args["--configonly"].asBool()) {
80 writableOps = WritableOps::No;
81 } else {
82 writableOps = WritableOps::Yes;
83 std::cout << "ops is writable" << std::endl;
84 }
Václav Kubernát74487df2020-06-04 01:29:28 +020085 if (const auto& search_dir = args["-s"]) {
86 datastore.addSchemaDir(search_dir.asString());
87 }
Václav Kubernát619e6542020-06-29 14:13:43 +020088 for (const auto& schemaFile : args["<schema_file_or_module_name>"].asStringList()) {
89 if (std::filesystem::exists(schemaFile)) {
90 datastore.addSchemaFile(schemaFile);
91 } else if (schemaFile.find('/') == std::string::npos) { // Module names cannot have a slash
92 datastore.loadModule(schemaFile);
93 } else {
94 std::cerr << "Cannot load YANG module " << schemaFile << "\n";
95 }
Václav Kubernát74487df2020-06-04 01:29:28 +020096 }
97 if (const auto& enableFeatures = args["-e"]) {
98 namespace x3 = boost::spirit::x3;
99 auto grammar = +(x3::char_-":") >> ":" >> +(x3::char_-":");
100 for (const auto& enableFeature : enableFeatures.asStringList()) {
101 std::pair<std::string, std::string> parsed;
102 auto it = enableFeature.begin();
103 auto res = x3::parse(it, enableFeature.cend(), grammar, parsed);
104 if (!res || it != enableFeature.cend()) {
105 std::cerr << "Error parsing feature enable flags: " << enableFeature << "\n";
106 return 1;
107 }
108 try {
109 datastore.enableFeature(parsed.first, parsed.second);
110 } catch (std::runtime_error& ex) {
111 std::cerr << ex.what() << "\n";
112 return 1;
113 }
114
115 }
116 }
Václav Kubernát548cb192020-06-26 14:00:42 +0200117 if (const auto& dataFiles = args["-i"]) {
118 for (const auto& dataFile : dataFiles.asStringList()) {
119 datastore.addDataFile(dataFile);
120 }
121 }
Václav Kubernátb79f3ca2020-02-04 15:56:01 +0100122#else
123#error "Unknown CLI backend"
124#endif
125
Václav Kubernát43908fb2020-01-02 19:05:51 +0100126 auto dataQuery = std::make_shared<DataQuery>(datastore);
Václav Kubernát28cf3362020-06-29 17:52:51 +0200127 Parser parser(datastore.schema(), writableOps, dataQuery);
Václav Kubernát395d92c2020-01-24 12:18:18 +0100128
129 using replxx::Replxx;
130
131 Replxx lineEditor;
132
Václav Kubernát48637292020-07-08 16:54:13 +0200133 lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), [&lineEditor] (const auto& code) {
134 return lineEditor.invoke(Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, code);
135 });
136 lineEditor.bind_key(Replxx::KEY::control('W'), [&lineEditor] (const auto& code) {
137 return lineEditor.invoke(Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, code);
138 });
Václav Kubernát395d92c2020-01-24 12:18:18 +0100139
140 lineEditor.set_word_break_characters("\t _[]/:'\"=-%");
141
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100142 lineEditor.set_completion_callback([&parser](const std::string& input, int& context) {
Václav Kubernáta395d332019-02-13 16:49:20 +0100143 std::stringstream stream;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100144 auto completions = parser.completeCommand(input, stream);
Václav Kubernáta395d332019-02-13 16:49:20 +0100145
Jan Kundrát8d8efe82019-10-18 10:21:36 +0200146 std::vector<replxx::Replxx::Completion> res;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100147 std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res));
148 context = completions.m_contextLength;
Václav Kubernáta395d332019-02-13 16:49:20 +0100149 return res;
150 });
Václav Kubernát2b684612018-08-09 18:55:24 +0200151
Václav Kubernát435706e2019-02-20 18:05:59 +0100152 std::optional<std::string> historyFile;
153 if (auto xdgHome = getenv("XDG_DATA_HOME")) {
154 historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME;
155 } else if (auto home = getenv("HOME")) {
156 historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME;
157 }
158
159 if (historyFile)
160 lineEditor.history_load(historyFile.value());
161
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200162 while (true) {
Václav Kubernáta395d332019-02-13 16:49:20 +0100163 auto line = lineEditor.input(parser.currentNode() + "> ");
164 if (!line) {
Václav Kubernát82bf1312019-11-05 11:19:26 +0100165 // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN.
166 // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program.
167 // I have no idea why replxx uses errno for this.
168 if (errno == EAGAIN) {
169 continue;
170 } else {
171 break;
172 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100173 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200174
Jan Kundráte3877022018-09-05 15:32:09 +0200175 std::locale C_locale("C");
Václav Kubernáta395d332019-02-13 16:49:20 +0100176 std::string_view view{line};
177 if (std::all_of(view.begin(), view.end(),
Jan Kundráte3877022018-09-05 15:32:09 +0200178 [C_locale](const auto c) { return std::isspace(c, C_locale);})) {
179 continue;
180 }
181
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200182 try {
Václav Kubernát5b80e522019-01-25 12:17:03 +0100183 command_ cmd = parser.parseCommand(line, std::cout);
Václav Kubernát6415b822018-08-22 17:40:01 +0200184 boost::apply_visitor(Interpreter(parser, datastore), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200185 } catch (InvalidCommandException& ex) {
186 std::cerr << ex.what() << std::endl;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200187 } catch (DatastoreException& ex) {
188 std::cerr << ex.what() << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200189 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100190
Václav Kubernáta395d332019-02-13 16:49:20 +0100191 lineEditor.history_add(line);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200192 }
193
Václav Kubernát435706e2019-02-20 18:05:59 +0100194 if (historyFile)
195 lineEditor.history_save(historyFile.value());
196
Jan Kundrátdc2b0722018-03-02 14:13:37 +0100197 return 0;
198}