blob: c6e3fce833cc44a79ca4e16f15c1af5b73381c62 [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át6415b822018-08-22 17:40:01 +020015#include "sysrepo_access.hpp"
Jan Kundrátdc2b0722018-03-02 14:13:37 +010016
Václav Kubernát435706e2019-02-20 18:05:59 +010017const auto HISTORY_FILE_NAME = "netconf-cli_history";
Václav Kubernát624a8872018-03-02 17:28:47 +010018
Jan Kundrátdc2b0722018-03-02 14:13:37 +010019static const char usage[] =
Václav Kubernát624a8872018-03-02 17:28:47 +010020 R"(CLI interface to remote NETCONF hosts
Jan Kundrátdc2b0722018-03-02 14:13:37 +010021
22Usage:
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020023 netconf-cli
Jan Kundrátdc2b0722018-03-02 14:13:37 +010024 netconf-cli (-h | --help)
25 netconf-cli --version
26)";
27
Václav Kubernát624a8872018-03-02 17:28:47 +010028
Jan Kundrátdc2b0722018-03-02 14:13:37 +010029int main(int argc, char* argv[])
30{
31 auto args = docopt::docopt(usage,
32 {argv + 1, argv + argc},
33 true,
34 "netconf-cli " NETCONF_CLI_VERSION,
35 true);
Václav Kubernát624a8872018-03-02 17:28:47 +010036 std::cout << "Welcome to netconf-cli" << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020037
Václav Kubernát6415b822018-08-22 17:40:01 +020038 SysrepoAccess datastore("netconf-cli");
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020039 Parser parser(datastore.schema());
Václav Kubernáta395d332019-02-13 16:49:20 +010040 replxx::Replxx lineEditor;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010041 lineEditor.set_completion_callback([&parser](const std::string& input, int& context) {
Václav Kubernáta395d332019-02-13 16:49:20 +010042 std::stringstream stream;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010043 auto completions = parser.completeCommand(input, stream);
Václav Kubernáta395d332019-02-13 16:49:20 +010044
Jan Kundrát8d8efe82019-10-18 10:21:36 +020045 std::vector<replxx::Replxx::Completion> res;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +010046 std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res));
47 context = completions.m_contextLength;
Václav Kubernáta395d332019-02-13 16:49:20 +010048 return res;
49 });
Václav Kubernát2b684612018-08-09 18:55:24 +020050
Václav Kubernát435706e2019-02-20 18:05:59 +010051 std::optional<std::string> historyFile;
52 if (auto xdgHome = getenv("XDG_DATA_HOME")) {
53 historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME;
54 } else if (auto home = getenv("HOME")) {
55 historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME;
56 }
57
58 if (historyFile)
59 lineEditor.history_load(historyFile.value());
60
Václav Kubernátff2c9f62018-05-16 20:26:31 +020061 while (true) {
Václav Kubernáta395d332019-02-13 16:49:20 +010062 auto line = lineEditor.input(parser.currentNode() + "> ");
63 if (!line) {
Václav Kubernát82bf1312019-11-05 11:19:26 +010064 // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN.
65 // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program.
66 // I have no idea why replxx uses errno for this.
67 if (errno == EAGAIN) {
68 continue;
69 } else {
70 break;
71 }
Václav Kubernát5b80e522019-01-25 12:17:03 +010072 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +020073
Jan Kundráte3877022018-09-05 15:32:09 +020074 std::locale C_locale("C");
Václav Kubernáta395d332019-02-13 16:49:20 +010075 std::string_view view{line};
76 if (std::all_of(view.begin(), view.end(),
Jan Kundráte3877022018-09-05 15:32:09 +020077 [C_locale](const auto c) { return std::isspace(c, C_locale);})) {
78 continue;
79 }
80
Václav Kubernátff2c9f62018-05-16 20:26:31 +020081 try {
Václav Kubernát5b80e522019-01-25 12:17:03 +010082 command_ cmd = parser.parseCommand(line, std::cout);
Václav Kubernát6415b822018-08-22 17:40:01 +020083 boost::apply_visitor(Interpreter(parser, datastore), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020084 } catch (InvalidCommandException& ex) {
85 std::cerr << ex.what() << std::endl;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +020086 } catch (DatastoreException& ex) {
87 std::cerr << ex.what() << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020088 }
Václav Kubernát5b80e522019-01-25 12:17:03 +010089
Václav Kubernáta395d332019-02-13 16:49:20 +010090 lineEditor.history_add(line);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020091 }
92
Václav Kubernát435706e2019-02-20 18:05:59 +010093 if (historyFile)
94 lineEditor.history_save(historyFile.value());
95
Jan Kundrátdc2b0722018-03-02 14:13:37 +010096 return 0;
97}