blob: 98c68cafe774e46238d05bea6daf2f47fdad73e3 [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*/
8
Václav Kubernát624a8872018-03-02 17:28:47 +01009#include <boost/spirit/home/x3.hpp>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010010#include <docopt.h>
Václav Kubernát624a8872018-03-02 17:28:47 +010011#include <iostream>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010012#include <string>
13#include "NETCONF_CLI_VERSION.h"
Václav Kubernát96344a12018-05-28 16:33:39 +020014#include "interpreter.hpp"
Václav Kubernát48fc3832018-05-28 14:21:22 +020015#include "parser.hpp"
16#include "schema.hpp"
Jan Kundrátdc2b0722018-03-02 14:13:37 +010017
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át624a8872018-03-02 17:28:47 +010023 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 +010028namespace x3 = boost::spirit::x3;
29namespace ascii = boost::spirit::x3::ascii;
30
31using Cmd = std::vector<std::string>;
Václav Kubernát624a8872018-03-02 17:28:47 +010032using ascii::space;
Václav Kubernát48fc3832018-05-28 14:21:22 +020033using x3::_attr;
34using x3::alpha;
35using x3::char_;
36using x3::lexeme;
37using x3::lit;
Václav Kubernát624a8872018-03-02 17:28:47 +010038
Václav Kubernát96344a12018-05-28 16:33:39 +020039using command = boost::variant<cd_>;
Václav Kubernát624a8872018-03-02 17:28:47 +010040
Jan Kundrátdc2b0722018-03-02 14:13:37 +010041int main(int argc, char* argv[])
42{
43 auto args = docopt::docopt(usage,
44 {argv + 1, argv + argc},
45 true,
46 "netconf-cli " NETCONF_CLI_VERSION,
47 true);
Václav Kubernát624a8872018-03-02 17:28:47 +010048 std::cout << "Welcome to netconf-cli" << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020049
Václav Kubernát48fc3832018-05-28 14:21:22 +020050 Schema schema;
51 schema.addContainer("", "a");
52 schema.addContainer("", "b");
53 schema.addContainer("a", "a2");
54 schema.addContainer("b", "b2");
55 schema.addContainer("a/a2", "a3");
56 schema.addContainer("b/b2", "b3");
57 schema.addList("", "list", {"number"});
58 schema.addContainer("list", "contInList");
59 schema.addList("", "twoKeyList", {"number", "name"});
60 Parser parser(schema);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020061
62 while (true) {
Václav Kubernátf2e463f2018-05-28 15:51:08 +020063 std::cout << parser.currentNode() << "> ";
Václav Kubernátff2c9f62018-05-16 20:26:31 +020064 std::string input;
65 std::getline(std::cin, input);
66 if (std::cin.eof())
67 break;
68
69 try {
Václav Kubernát96344a12018-05-28 16:33:39 +020070 command cmd = parser.parseCommand(input, std::cout);
71 boost::apply_visitor(Interpreter(parser), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020072 } catch (InvalidCommandException& ex) {
73 std::cerr << ex.what() << std::endl;
74 }
75 }
76
Jan Kundrátdc2b0722018-03-02 14:13:37 +010077 return 0;
78}