blob: 46650b295cdcc790e33b6f988b6cd7a36f287bf6 [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>
29#include "yang_access.hpp"
30#define PROGRAM_NAME "yang-cli"
31static const auto usage = R"(CLI interface for creating local YANG data instances
32
33Usage:
Václav Kubernát548cb192020-06-26 14:00:42 +020034 yang-cli [-s <search_dir>] [-e enable_features]... [-i data_file]... <schema_file>...
Václav Kubernát74487df2020-06-04 01:29:28 +020035 yang-cli (-h | --help)
36 yang-cli --version
37
38Options:
39 -s <search_dir> Set search for schema lookup
Václav Kubernát548cb192020-06-26 14:00:42 +020040 -e <enable_features> Feature to enable after modules are loaded. This option can be supplied more than once. Format: <module_name>:<feature>
41 -i <data_file> File to import data from)";
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010042#else
43#error "Unknown CLI backend"
44#endif
Jan Kundrátdc2b0722018-03-02 14:13:37 +010045
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010046const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history";
47
Jan Kundrátdc2b0722018-03-02 14:13:37 +010048int main(int argc, char* argv[])
49{
50 auto args = docopt::docopt(usage,
51 {argv + 1, argv + argc},
52 true,
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010053 PROGRAM_NAME " " NETCONF_CLI_VERSION,
Jan Kundrátdc2b0722018-03-02 14:13:37 +010054 true);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020055
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010056#if defined(SYSREPO_CLI)
Václav Kubernát715c85c2020-04-14 01:46:08 +020057 auto datastoreType = Datastore::Running;
58 if (const auto& ds = args["-d"]) {
59 if (ds.asString() == "startup") {
60 datastoreType = Datastore::Startup;
61 } else if (ds.asString() == "running") {
62 datastoreType = Datastore::Running;
63 } else {
64 std::cerr << PROGRAM_NAME << ": unknown datastore: " << ds.asString() << "\n";
65 return 1;
66 }
67 }
68 SysrepoAccess datastore(PROGRAM_NAME, datastoreType);
69 std::cout << "Connected to sysrepo [datastore: " << (datastoreType == Datastore::Startup ? "startup" : "running") << "]" << std::endl;
Václav Kubernát74487df2020-06-04 01:29:28 +020070#elif defined(YANG_CLI)
71 YangAccess datastore;
72 if (const auto& search_dir = args["-s"]) {
73 datastore.addSchemaDir(search_dir.asString());
74 }
75 for (const auto& schemaFile : args["<schema_file>"].asStringList()) {
76 datastore.addSchemaFile(schemaFile);
77 }
78 if (const auto& enableFeatures = args["-e"]) {
79 namespace x3 = boost::spirit::x3;
80 auto grammar = +(x3::char_-":") >> ":" >> +(x3::char_-":");
81 for (const auto& enableFeature : enableFeatures.asStringList()) {
82 std::pair<std::string, std::string> parsed;
83 auto it = enableFeature.begin();
84 auto res = x3::parse(it, enableFeature.cend(), grammar, parsed);
85 if (!res || it != enableFeature.cend()) {
86 std::cerr << "Error parsing feature enable flags: " << enableFeature << "\n";
87 return 1;
88 }
89 try {
90 datastore.enableFeature(parsed.first, parsed.second);
91 } catch (std::runtime_error& ex) {
92 std::cerr << ex.what() << "\n";
93 return 1;
94 }
95
96 }
97 }
Václav Kubernát548cb192020-06-26 14:00:42 +020098 if (const auto& dataFiles = args["-i"]) {
99 for (const auto& dataFile : dataFiles.asStringList()) {
100 datastore.addDataFile(dataFile);
101 }
102 }
Václav Kubernátb79f3ca2020-02-04 15:56:01 +0100103#else
104#error "Unknown CLI backend"
105#endif
106
Václav Kubernát43908fb2020-01-02 19:05:51 +0100107 auto dataQuery = std::make_shared<DataQuery>(datastore);
108 Parser parser(datastore.schema(), dataQuery);
Václav Kubernát395d92c2020-01-24 12:18:18 +0100109
110 using replxx::Replxx;
111
112 Replxx lineEditor;
113
114 lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), std::bind(&Replxx::invoke, &lineEditor, Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, std::placeholders::_1));
115 lineEditor.bind_key(Replxx::KEY::control('W'), std::bind(&Replxx::invoke, &lineEditor, Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, std::placeholders::_1));
116
117 lineEditor.set_word_break_characters("\t _[]/:'\"=-%");
118
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100119 lineEditor.set_completion_callback([&parser](const std::string& input, int& context) {
Václav Kubernáta395d332019-02-13 16:49:20 +0100120 std::stringstream stream;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100121 auto completions = parser.completeCommand(input, stream);
Václav Kubernáta395d332019-02-13 16:49:20 +0100122
Jan Kundrát8d8efe82019-10-18 10:21:36 +0200123 std::vector<replxx::Replxx::Completion> res;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100124 std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res));
125 context = completions.m_contextLength;
Václav Kubernáta395d332019-02-13 16:49:20 +0100126 return res;
127 });
Václav Kubernát2b684612018-08-09 18:55:24 +0200128
Václav Kubernát435706e2019-02-20 18:05:59 +0100129 std::optional<std::string> historyFile;
130 if (auto xdgHome = getenv("XDG_DATA_HOME")) {
131 historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME;
132 } else if (auto home = getenv("HOME")) {
133 historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME;
134 }
135
136 if (historyFile)
137 lineEditor.history_load(historyFile.value());
138
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200139 while (true) {
Václav Kubernáta395d332019-02-13 16:49:20 +0100140 auto line = lineEditor.input(parser.currentNode() + "> ");
141 if (!line) {
Václav Kubernát82bf1312019-11-05 11:19:26 +0100142 // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN.
143 // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program.
144 // I have no idea why replxx uses errno for this.
145 if (errno == EAGAIN) {
146 continue;
147 } else {
148 break;
149 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100150 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200151
Jan Kundráte3877022018-09-05 15:32:09 +0200152 std::locale C_locale("C");
Václav Kubernáta395d332019-02-13 16:49:20 +0100153 std::string_view view{line};
154 if (std::all_of(view.begin(), view.end(),
Jan Kundráte3877022018-09-05 15:32:09 +0200155 [C_locale](const auto c) { return std::isspace(c, C_locale);})) {
156 continue;
157 }
158
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200159 try {
Václav Kubernát5b80e522019-01-25 12:17:03 +0100160 command_ cmd = parser.parseCommand(line, std::cout);
Václav Kubernát6415b822018-08-22 17:40:01 +0200161 boost::apply_visitor(Interpreter(parser, datastore), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200162 } catch (InvalidCommandException& ex) {
163 std::cerr << ex.what() << std::endl;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200164 } catch (DatastoreException& ex) {
165 std::cerr << ex.what() << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200166 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100167
Václav Kubernáta395d332019-02-13 16:49:20 +0100168 lineEditor.history_add(line);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200169 }
170
Václav Kubernát435706e2019-02-20 18:05:59 +0100171 if (historyFile)
172 lineEditor.history_save(historyFile.value());
173
Jan Kundrátdc2b0722018-03-02 14:13:37 +0100174 return 0;
175}