blob: 607115e7b85d3559a68fe3b6085c6456129d8fee [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>
Václav Kubernát624a8872018-03-02 17:28:47 +010013#include "CParser.hpp"
14#include "CTree.hpp"
Jan Kundrátdc2b0722018-03-02 14:13:37 +010015#include "NETCONF_CLI_VERSION.h"
16
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át624a8872018-03-02 17:28:47 +010022 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 +010027namespace x3 = boost::spirit::x3;
28namespace ascii = boost::spirit::x3::ascii;
29
30using Cmd = std::vector<std::string>;
31using x3::alpha;
32using x3::lit;
33using x3::char_;
34using x3::_attr;
35using x3::lexeme;
36using ascii::space;
37
Václav Kubernát2a9e1792018-05-28 12:53:48 +020038void interpret(cd_ command, CParser& tree)
Václav Kubernát624a8872018-03-02 17:28:47 +010039{
Václav Kubernátff2c9f62018-05-16 20:26:31 +020040 tree.changeNode(command.m_path);
Václav Kubernát624a8872018-03-02 17:28:47 +010041}
42
Jan Kundrátdc2b0722018-03-02 14:13:37 +010043int main(int argc, char* argv[])
44{
45 auto args = docopt::docopt(usage,
46 {argv + 1, argv + argc},
47 true,
48 "netconf-cli " NETCONF_CLI_VERSION,
49 true);
Václav Kubernát624a8872018-03-02 17:28:47 +010050 std::cout << "Welcome to netconf-cli" << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020051
52 CTree tree;
53 tree.addContainer("", "a");
54 tree.addContainer("", "b");
55 tree.addContainer("a", "a2");
56 tree.addContainer("b", "b2");
57 tree.addContainer("a/a2", "a3");
58 tree.addContainer("b/b2", "b3");
59 tree.addList("", "list", {"number"});
60 tree.addContainer("list", "contInList");
61 tree.addList("", "twoKeyList", {"number", "name"});
62 CParser parser(tree);
63
64 while (true) {
Václav Kubernát2a9e1792018-05-28 12:53:48 +020065 std::cout << parser.currentNode() << ">";
Václav Kubernátff2c9f62018-05-16 20:26:31 +020066 std::string input;
67 std::getline(std::cin, input);
68 if (std::cin.eof())
69 break;
70
71 try {
72 cd_ command = parser.parseCommand(input, std::cout);
Václav Kubernát2a9e1792018-05-28 12:53:48 +020073 interpret(command, parser);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020074 } catch (InvalidCommandException& ex) {
75 std::cerr << ex.what() << std::endl;
76 }
77 }
78
Jan Kundrátdc2b0722018-03-02 14:13:37 +010079 return 0;
80}