blob: 4fe798f92aca7c6dc9c9b241aacc341eea88a67c [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át2b684612018-08-09 18:55:24 +02009#include <experimental/filesystem>
Václav Kubernát624a8872018-03-02 17:28:47 +010010#include <iostream>
Václav Kubernáta395d332019-02-13 16:49:20 +010011#include <replxx.hxx>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010012#include "NETCONF_CLI_VERSION.h"
Václav Kubernát96344a12018-05-28 16:33:39 +020013#include "interpreter.hpp"
Václav Kubernát6415b822018-08-22 17:40:01 +020014#include "sysrepo_access.hpp"
Václav Kubernát2b684612018-08-09 18:55:24 +020015#include "yang_schema.hpp"
Jan Kundrátdc2b0722018-03-02 14:13:37 +010016
Václav Kubernát624a8872018-03-02 17:28:47 +010017
Jan Kundrátdc2b0722018-03-02 14:13:37 +010018static const char usage[] =
Václav Kubernát624a8872018-03-02 17:28:47 +010019 R"(CLI interface to remote NETCONF hosts
Jan Kundrátdc2b0722018-03-02 14:13:37 +010020
21Usage:
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020022 netconf-cli
Jan Kundrátdc2b0722018-03-02 14:13:37 +010023 netconf-cli (-h | --help)
24 netconf-cli --version
25)";
26
Václav Kubernát624a8872018-03-02 17:28:47 +010027
Jan Kundrátdc2b0722018-03-02 14:13:37 +010028int main(int argc, char* argv[])
29{
30 auto args = docopt::docopt(usage,
31 {argv + 1, argv + argc},
32 true,
33 "netconf-cli " NETCONF_CLI_VERSION,
34 true);
Václav Kubernát624a8872018-03-02 17:28:47 +010035 std::cout << "Welcome to netconf-cli" << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020036
Václav Kubernát6415b822018-08-22 17:40:01 +020037 SysrepoAccess datastore("netconf-cli");
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020038 Parser parser(datastore.schema());
Václav Kubernáta395d332019-02-13 16:49:20 +010039 replxx::Replxx lineEditor;
40 lineEditor.set_completion_callback([&parser](const std::string& input, int&) {
41 std::stringstream stream;
42 auto completionsSet = parser.completeCommand(input, stream);
43
44 std::vector<std::string> res;
45 std::transform(completionsSet.begin(), completionsSet.end(), std::back_inserter(res),
46 [input](auto it) { return it; });
47 return res;
48 });
49 lineEditor.set_word_break_characters(" '/[");
Václav Kubernát2b684612018-08-09 18:55:24 +020050
Václav Kubernátff2c9f62018-05-16 20:26:31 +020051 while (true) {
Václav Kubernáta395d332019-02-13 16:49:20 +010052 auto line = lineEditor.input(parser.currentNode() + "> ");
53 if (!line) {
Václav Kubernátff2c9f62018-05-16 20:26:31 +020054 break;
Václav Kubernát5b80e522019-01-25 12:17:03 +010055 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +020056
Jan Kundráte3877022018-09-05 15:32:09 +020057 std::locale C_locale("C");
Václav Kubernáta395d332019-02-13 16:49:20 +010058 std::string_view view{line};
59 if (std::all_of(view.begin(), view.end(),
Jan Kundráte3877022018-09-05 15:32:09 +020060 [C_locale](const auto c) { return std::isspace(c, C_locale);})) {
61 continue;
62 }
63
Václav Kubernátff2c9f62018-05-16 20:26:31 +020064 try {
Václav Kubernát5b80e522019-01-25 12:17:03 +010065 command_ cmd = parser.parseCommand(line, std::cout);
Václav Kubernát6415b822018-08-22 17:40:01 +020066 boost::apply_visitor(Interpreter(parser, datastore), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020067 } catch (InvalidCommandException& ex) {
68 std::cerr << ex.what() << std::endl;
69 }
Václav Kubernát5b80e522019-01-25 12:17:03 +010070
Václav Kubernáta395d332019-02-13 16:49:20 +010071 lineEditor.history_add(line);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020072 }
73
Jan Kundrátdc2b0722018-03-02 14:13:37 +010074 return 0;
75}