blob: c142850753afc4593841d14aa30f5dd113814519 [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átb79f3ca2020-02-04 15:56:01 +010027#else
28#error "Unknown CLI backend"
29#endif
Jan Kundrátdc2b0722018-03-02 14:13:37 +010030
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010031const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history";
32
Jan Kundrátdc2b0722018-03-02 14:13:37 +010033int main(int argc, char* argv[])
34{
35 auto args = docopt::docopt(usage,
36 {argv + 1, argv + argc},
37 true,
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010038 PROGRAM_NAME " " NETCONF_CLI_VERSION,
Jan Kundrátdc2b0722018-03-02 14:13:37 +010039 true);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020040
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010041#if defined(SYSREPO_CLI)
Václav Kubernát715c85c2020-04-14 01:46:08 +020042 auto datastoreType = Datastore::Running;
43 if (const auto& ds = args["-d"]) {
44 if (ds.asString() == "startup") {
45 datastoreType = Datastore::Startup;
46 } else if (ds.asString() == "running") {
47 datastoreType = Datastore::Running;
48 } else {
49 std::cerr << PROGRAM_NAME << ": unknown datastore: " << ds.asString() << "\n";
50 return 1;
51 }
52 }
53 SysrepoAccess datastore(PROGRAM_NAME, datastoreType);
54 std::cout << "Connected to sysrepo [datastore: " << (datastoreType == Datastore::Startup ? "startup" : "running") << "]" << std::endl;
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010055#else
56#error "Unknown CLI backend"
57#endif
58
Václav Kubernát43908fb2020-01-02 19:05:51 +010059 auto dataQuery = std::make_shared<DataQuery>(datastore);
60 Parser parser(datastore.schema(), dataQuery);
Václav Kubernát395d92c2020-01-24 12:18:18 +010061
62 using replxx::Replxx;
63
64 Replxx lineEditor;
65
66 lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), std::bind(&Replxx::invoke, &lineEditor, Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, std::placeholders::_1));
67 lineEditor.bind_key(Replxx::KEY::control('W'), std::bind(&Replxx::invoke, &lineEditor, Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, std::placeholders::_1));
68
69 lineEditor.set_word_break_characters("\t _[]/:'\"=-%");
70
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010071 lineEditor.set_completion_callback([&parser](const std::string& input, int& context) {
Václav Kubernáta395d332019-02-13 16:49:20 +010072 std::stringstream stream;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010073 auto completions = parser.completeCommand(input, stream);
Václav Kubernáta395d332019-02-13 16:49:20 +010074
Jan Kundrát8d8efe82019-10-18 10:21:36 +020075 std::vector<replxx::Replxx::Completion> res;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010076 std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res));
77 context = completions.m_contextLength;
Václav Kubernáta395d332019-02-13 16:49:20 +010078 return res;
79 });
Václav Kubernát2b684612018-08-09 18:55:24 +020080
Václav Kubernát435706e2019-02-20 18:05:59 +010081 std::optional<std::string> historyFile;
82 if (auto xdgHome = getenv("XDG_DATA_HOME")) {
83 historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME;
84 } else if (auto home = getenv("HOME")) {
85 historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME;
86 }
87
88 if (historyFile)
89 lineEditor.history_load(historyFile.value());
90
Václav Kubernátff2c9f62018-05-16 20:26:31 +020091 while (true) {
Václav Kubernáta395d332019-02-13 16:49:20 +010092 auto line = lineEditor.input(parser.currentNode() + "> ");
93 if (!line) {
Václav Kubernát82bf1312019-11-05 11:19:26 +010094 // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN.
95 // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program.
96 // I have no idea why replxx uses errno for this.
97 if (errno == EAGAIN) {
98 continue;
99 } else {
100 break;
101 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100102 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200103
Jan Kundráte3877022018-09-05 15:32:09 +0200104 std::locale C_locale("C");
Václav Kubernáta395d332019-02-13 16:49:20 +0100105 std::string_view view{line};
106 if (std::all_of(view.begin(), view.end(),
Jan Kundráte3877022018-09-05 15:32:09 +0200107 [C_locale](const auto c) { return std::isspace(c, C_locale);})) {
108 continue;
109 }
110
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200111 try {
Václav Kubernát5b80e522019-01-25 12:17:03 +0100112 command_ cmd = parser.parseCommand(line, std::cout);
Václav Kubernát6415b822018-08-22 17:40:01 +0200113 boost::apply_visitor(Interpreter(parser, datastore), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200114 } catch (InvalidCommandException& ex) {
115 std::cerr << ex.what() << std::endl;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200116 } catch (DatastoreException& ex) {
117 std::cerr << ex.what() << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200118 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100119
Václav Kubernáta395d332019-02-13 16:49:20 +0100120 lineEditor.history_add(line);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200121 }
122
Václav Kubernát435706e2019-02-20 18:05:59 +0100123 if (historyFile)
124 lineEditor.history_save(historyFile.value());
125
Jan Kundrátdc2b0722018-03-02 14:13:37 +0100126 return 0;
127}